package com.google.inject.internal;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import com.google.common.base.Joiner;
import com.google.common.base.Joiner.MapJoiner;
import com.google.common.base.Preconditions;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.inject.BindingAnnotation;
import com.google.inject.Key;
import com.google.inject.ScopeAnnotation;
import com.google.inject.TypeLiteral;
import com.google.inject.internal.util.Classes;
import com.google.inject.name.Named;
import com.google.inject.name.Names;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import javax.inject.Qualifier;
public class Annotations {
public static boolean isMarker(Class<? extends Annotation> annotationType) {
return annotationType.getDeclaredMethods().length == 0;
}
public static boolean isAllDefaultMethods(Class<? extends Annotation> annotationType) {
boolean hasMethods = false;
for (Method m : annotationType.getDeclaredMethods()) {
hasMethods = true;
if (m.getDefaultValue() == null) {
return false;
}
}
return hasMethods;
}
private static final LoadingCache<Class<? extends Annotation>, Annotation> cache =
CacheBuilder.newBuilder()
.weakKeys()
.build(
new CacheLoader<Class<? extends Annotation>, Annotation>() {
@Override
public Annotation load(Class<? extends Annotation> input) {
return generateAnnotationImpl(input);
}
});
public static <T extends Annotation> T generateAnnotation(Class<T> annotationType) {
Preconditions.checkState(
isAllDefaultMethods(annotationType), "%s is not all default methods", annotationType);
return (T) cache.getUnchecked(annotationType);
}
private static <T extends Annotation> T generateAnnotationImpl(final Class<T> annotationType) {
final Map<String, Object> members = resolveMembers(annotationType);
return annotationType.cast(
Proxy.newProxyInstance(
annotationType.getClassLoader(),
new Class<?>[] {annotationType},
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Exception {
String name = method.getName();
if (name.equals("annotationType")) {
return annotationType;
} else if (name.equals("toString")) {
return annotationToString(annotationType, members);
} else if (name.equals("hashCode")) {
return annotationHashCode(annotationType, members);
} else if (name.equals("equals")) {
return annotationEquals(annotationType, members, args[0]);
} else {
return members.get(name);
}
}
}));
}
private static ImmutableMap<String, Object> resolveMembers(
Class<? extends Annotation> annotationType) {
ImmutableMap.Builder<String, Object> result = ImmutableMap.builder();
for (Method method : annotationType.getDeclaredMethods()) {
result.put(method.getName(), method.getDefaultValue());
}
return result.build();
}
private static boolean annotationEquals(
Class<? extends Annotation> type, Map<String, Object> members, Object other)
throws Exception {
if (!type.isInstance(other)) {
return false;
}
for (Method method : type.getDeclaredMethods()) {
String name = method.getName();
if (!Arrays.deepEquals(
new Object[] {method.invoke(other)}, new Object[] {members.get(name)})) {
return false;
}
}
return true;
}
private static int annotationHashCode(
Class<? extends Annotation> type, Map<String, Object> members) throws Exception {
int result = 0;
for (Method method : type.getDeclaredMethods()) {
String name = method.getName();
Object value = members.get(name);
result += (127 * name.hashCode()) ^ (Arrays.deepHashCode(new Object[] {value}) - 31);
}
return result;
}
private static final MapJoiner JOINER = Joiner.on(", ").withKeyValueSeparator("=");
private static String annotationToString(
Class<? extends Annotation> type, Map<String, Object> members) throws Exception {
StringBuilder sb = new StringBuilder().append("@").append(type.getName()).append("(");
JOINER.appendTo(
sb,
Maps.transformValues(
members,
arg -> {
String s = Arrays.deepToString(new Object[] {arg});
return s.substring(1, s.length() - 1);
}));
return sb.append(")").toString();
}
public static boolean isRetainedAtRuntime(Class<? extends Annotation> annotationType) {
Retention retention = annotationType.getAnnotation(Retention.class);
return retention != null && retention.value() == RetentionPolicy.RUNTIME;
}
public static Class<? extends Annotation> findScopeAnnotation(
Errors errors, Class<?> implementation) {
return findScopeAnnotation(errors, implementation.getAnnotations());
}
public static Class<? extends Annotation> findScopeAnnotation(
Errors errors, Annotation[] annotations) {
Class<? extends Annotation> found = null;
for (Annotation annotation : annotations) {
Class<? extends Annotation> annotationType = annotation.annotationType();
if (isScopeAnnotation(annotationType)) {
if (found != null) {
errors.duplicateScopeAnnotations(found, annotationType);
} else {
found = annotationType;
}
}
}
return found;
}
static boolean containsComponentAnnotation(Annotation[] annotations) {
for (Annotation annotation : annotations) {
if (annotation.annotationType().getSimpleName().equals("Component")) {
return true;
}
}
return false;
}
private static final boolean QUOTE_MEMBER_VALUES = determineWhetherToQuote();
public static String memberValueString(String value) {
return QUOTE_MEMBER_VALUES ? "\"" + value + "\"" : value;
}
@Retention(RUNTIME)
private @interface TestAnnotation {
String value();
}
@TestAnnotation("determineWhetherToQuote")
private static boolean determineWhetherToQuote() {
try {
String annotation =
Annotations.class
.getDeclaredMethod("determineWhetherToQuote")
.getAnnotation(TestAnnotation.class)
.toString();
return annotation.contains("\"determineWhetherToQuote\"");
} catch (NoSuchMethodException e) {
throw new AssertionError(e);
}
}
static class AnnotationChecker {
private final Collection<Class<? extends Annotation>> annotationTypes;
private CacheLoader<Class<? extends Annotation>, Boolean> hasAnnotations =
new CacheLoader<Class<? extends Annotation>, Boolean>() {
@Override
public Boolean load(Class<? extends Annotation> annotationType) {
for (Annotation annotation : annotationType.getAnnotations()) {
if (annotationTypes.contains(annotation.annotationType())) {
return true;
}
}
return false;
}
};
final LoadingCache<Class<? extends Annotation>, Boolean> cache =
CacheBuilder.newBuilder().weakKeys().build(hasAnnotations);
AnnotationChecker(Collection<Class<? extends Annotation>> annotationTypes) {
this.annotationTypes = annotationTypes;
}
boolean hasAnnotations(Class<? extends Annotation> annotated) {
return cache.getUnchecked(annotated);
}
}
private static final AnnotationChecker scopeChecker =
new AnnotationChecker(Arrays.asList(ScopeAnnotation.class, javax.inject.Scope.class));
public static boolean isScopeAnnotation(Class<? extends Annotation> annotationType) {
return scopeChecker.hasAnnotations(annotationType);
}
public static void checkForMisplacedScopeAnnotations(
Class<?> type, Object source, Errors errors) {
if (Classes.isConcrete(type)) {
return;
}
Class<? extends Annotation> scopeAnnotation = findScopeAnnotation(errors, type);
if (scopeAnnotation != null
&& !containsComponentAnnotation(type.getAnnotations())) {
errors.withSource(type).scopeAnnotationOnAbstractType(scopeAnnotation, type, source);
}
}
public static Key<?> getKey(
TypeLiteral<?> type, Member member, Annotation[] annotations, Errors errors)
throws ErrorsException {
int numErrorsBefore = errors.size();
Annotation found = findBindingAnnotation(errors, member, annotations);
errors.throwIfNewErrors(numErrorsBefore);
return found == null ? Key.get(type) : Key.get(type, found);
}
public static Annotation findBindingAnnotation(
Errors errors, Member member, Annotation[] annotations) {
Annotation found = null;
for (Annotation annotation : annotations) {
Class<? extends Annotation> annotationType = annotation.annotationType();
if (isBindingAnnotation(annotationType)) {
if (found != null) {
errors.duplicateBindingAnnotations(member, found.annotationType(), annotationType);
} else {
found = annotation;
}
}
}
return found;
}
private static final AnnotationChecker bindingAnnotationChecker =
new AnnotationChecker(Arrays.asList(BindingAnnotation.class, Qualifier.class));
public static boolean isBindingAnnotation(Class<? extends Annotation> annotationType) {
return bindingAnnotationChecker.hasAnnotations(annotationType);
}
public static Annotation canonicalizeIfNamed(Annotation annotation) {
if (annotation instanceof javax.inject.Named) {
return Names.named(((javax.inject.Named) annotation).value());
} else {
return annotation;
}
}
public static Class<? extends Annotation> canonicalizeIfNamed(
Class<? extends Annotation> annotationType) {
if (annotationType == javax.inject.Named.class) {
return Named.class;
} else {
return annotationType;
}
}
public static String nameOf(Key<?> key) {
Annotation annotation = key.getAnnotation();
Class<? extends Annotation> annotationType = key.getAnnotationType();
if (annotation != null && !isMarker(annotationType)) {
return key.getAnnotation().toString();
} else if (key.getAnnotationType() != null) {
return "@" + key.getAnnotationType().getName();
} else {
return "";
}
}
}