EMMA Coverage Report (generated Wed Oct 26 13:47:17 EDT 2011)
[all classes][org.jclouds.rest.internal]

COVERAGE SUMMARY FOR SOURCE FILE [SeedAnnotationCache.java]

nameclass, %method, %block, %line, %
SeedAnnotationCache.java100% (1/1)80%  (4/5)79%  (203/257)79%  (28.6/36)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class SeedAnnotationCache100% (1/1)80%  (4/5)79%  (203/257)79%  (28.6/36)
bindConstant (Method): void 0%   (0/1)0%   (0/23)0%   (0/4)
isConstantDeclaration (Method): boolean 100% (1/1)50%  (6/12)50%  (0.5/1)
load (Class): Boolean 100% (1/1)87%  (168/193)88%  (22.1/25)
SeedAnnotationCache (Injector, Multimap): void 100% (1/1)100% (12/12)100% (5/5)
isHttpMethod (Method): boolean 100% (1/1)100% (17/17)100% (1/1)

1/**
2 * Licensed to jclouds, Inc. (jclouds) under one or more
3 * contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  jclouds licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied.  See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.jclouds.rest.internal;
20 
21import static com.google.common.collect.Sets.difference;
22import static org.jclouds.rest.internal.RestAnnotationProcessor.delegationMap;
23import static org.jclouds.rest.internal.RestAnnotationProcessor.getHttpMethods;
24import static org.jclouds.rest.internal.RestAnnotationProcessor.methodToIndexOfParamToBinderParamAnnotation;
25import static org.jclouds.rest.internal.RestAnnotationProcessor.methodToIndexOfParamToEndpointAnnotations;
26import static org.jclouds.rest.internal.RestAnnotationProcessor.methodToIndexOfParamToEndpointParamAnnotations;
27import static org.jclouds.rest.internal.RestAnnotationProcessor.methodToIndexOfParamToFormParamAnnotations;
28import static org.jclouds.rest.internal.RestAnnotationProcessor.methodToIndexOfParamToHeaderParamAnnotations;
29import static org.jclouds.rest.internal.RestAnnotationProcessor.methodToIndexOfParamToMatrixParamAnnotations;
30import static org.jclouds.rest.internal.RestAnnotationProcessor.methodToIndexOfParamToParamParserAnnotations;
31import static org.jclouds.rest.internal.RestAnnotationProcessor.methodToIndexOfParamToPartParamAnnotations;
32import static org.jclouds.rest.internal.RestAnnotationProcessor.methodToIndexOfParamToPathParamAnnotations;
33import static org.jclouds.rest.internal.RestAnnotationProcessor.methodToIndexOfParamToPostParamAnnotations;
34import static org.jclouds.rest.internal.RestAnnotationProcessor.methodToIndexOfParamToQueryParamAnnotations;
35import static org.jclouds.rest.internal.RestAnnotationProcessor.methodToIndexOfParamToWrapWithAnnotation;
36import static org.jclouds.rest.internal.RestAnnotationProcessor.methodToIndexesOfOptions;
37 
38import java.lang.reflect.Method;
39import java.util.concurrent.ExecutionException;
40 
41import javax.annotation.Resource;
42import javax.inject.Named;
43import javax.inject.Singleton;
44import javax.ws.rs.Path;
45import javax.ws.rs.PathParam;
46 
47import org.jclouds.http.HttpRequest;
48import org.jclouds.logging.Logger;
49import org.jclouds.rest.annotations.Delegate;
50import org.jclouds.rest.internal.RestAnnotationProcessor.MethodKey;
51 
52import com.google.common.cache.CacheLoader;
53import com.google.common.collect.ImmutableSet;
54import com.google.common.collect.Multimap;
55import com.google.inject.Inject;
56import com.google.inject.Injector;
57import com.google.inject.Key;
58import com.google.inject.Provides;
59 
60/**
61 * seeds the annotation cache
62 * 
63 * @author Adrian Cole
64 */
65 
66@Singleton
67public class SeedAnnotationCache extends CacheLoader<Class<?>, Boolean> {
68   @Resource
69   protected Logger logger = Logger.NULL;
70 
71   protected final Multimap<String, String> constants;
72   protected final Injector injector;
73 
74   @Inject
75   public SeedAnnotationCache(Injector injector, @Named("CONSTANTS") Multimap<String, String> constants) {
76      this.injector = injector;
77      this.constants = constants;
78   }
79 
80   public void bindConstant(Method method) {
81      String key = method.getAnnotation(PathParam.class).value();
82      String value = injector.getInstance(Key.get(String.class, method.getAnnotation(Named.class)));
83      constants.put(key, value);
84   }
85 
86   @Override
87   public Boolean load(Class<?> declaring) throws ExecutionException {
88      for (Method method : difference(ImmutableSet.copyOf(declaring.getMethods()), ImmutableSet.copyOf(Object.class
89               .getMethods()))) {
90         if (isHttpMethod(method) || method.isAnnotationPresent(Delegate.class)) {
91            for (int index = 0; index < method.getParameterTypes().length; index++) {
92               methodToIndexOfParamToBinderParamAnnotation.get(method).get(index);
93               methodToIndexOfParamToWrapWithAnnotation.get(method).get(index);
94               methodToIndexOfParamToHeaderParamAnnotations.get(method).get(index);
95               methodToIndexOfParamToMatrixParamAnnotations.get(method).get(index);
96               methodToIndexOfParamToFormParamAnnotations.get(method).get(index);
97               methodToIndexOfParamToQueryParamAnnotations.get(method).get(index);
98               methodToIndexOfParamToEndpointAnnotations.get(method).get(index);
99               methodToIndexOfParamToEndpointParamAnnotations.get(method).get(index);
100               methodToIndexOfParamToPathParamAnnotations.get(method).get(index);
101               methodToIndexOfParamToPostParamAnnotations.get(method).get(index);
102               methodToIndexOfParamToParamParserAnnotations.get(method).get(index);
103               methodToIndexOfParamToPartParamAnnotations.get(method).get(index);
104               methodToIndexesOfOptions.get(method);
105            }
106            delegationMap.put(new MethodKey(method), method);
107         } else if (isConstantDeclaration(method)) {
108            bindConstant(method);
109         } else if (!method.getDeclaringClass().equals(declaring)) {
110            logger.trace("skipping potentially overridden method %s", method);
111         } else if (method.isAnnotationPresent(Provides.class)) {
112            logger.trace("skipping provider method %s", method);
113         } else {
114            logger.trace("Method is not annotated as either http or constant: %s", method);
115         }
116      }
117      return true;
118   }
119 
120   public static boolean isHttpMethod(Method method) {
121      return method.isAnnotationPresent(Path.class) || getHttpMethods(method) != null
122               || ImmutableSet.copyOf(method.getParameterTypes()).contains(HttpRequest.class);
123   }
124 
125   public static boolean isConstantDeclaration(Method method) {
126      return method.isAnnotationPresent(PathParam.class) && method.isAnnotationPresent(Named.class);
127   }
128}

[all classes][org.jclouds.rest.internal]
EMMA 2.0.5312 (C) Vladimir Roubtsov