Search in sources :

Example 96 with Annotation

use of java.lang.annotation.Annotation in project requery by requery.

the class EntityType method typeName.

@Override
public QualifiedName typeName() {
    String entityName = Stream.of(Mirrors.findAnnotationMirror(element(), Entity.class), Mirrors.findAnnotationMirror(element(), javax.persistence.Entity.class)).filter(Optional::isPresent).map(Optional::get).map(mirror -> Mirrors.findAnnotationValue(mirror, "name")).filter(Optional::isPresent).map(Optional::get).map(value -> value.getValue().toString()).filter(name -> !Names.isEmpty(name)).findAny().orElse("");
    Elements elements = processingEnvironment.getElementUtils();
    String packageName = elements.getPackageOf(element()).getQualifiedName().toString();
    // if set in the annotation just use that
    if (!Names.isEmpty(entityName)) {
        return new QualifiedName(packageName, entityName);
    }
    String typeName = element().getSimpleName().toString();
    if (element().getKind().isInterface()) {
        // maybe I<Something> style
        if (typeName.startsWith("I") && Character.isUpperCase(typeName.charAt(1))) {
            entityName = typeName.substring(1);
        } else {
            entityName = typeName + "Entity";
        }
    } else {
        entityName = Names.removeClassPrefixes(typeName);
        if (entityName.equals(typeName)) {
            entityName = typeName + (isImmutable() || isUnimplementable() ? "Type" : "Entity");
        }
    }
    return new QualifiedName(packageName, entityName);
}
Also used : Transient(io.requery.Transient) Table(io.requery.Table) Modifier(javax.lang.model.element.Modifier) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) Elements(javax.lang.model.util.Elements) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Embedded(io.requery.Embedded) Diagnostic(javax.tools.Diagnostic) Map(java.util.Map) MirroredTypeException(javax.lang.model.type.MirroredTypeException) PropertyNameStyle(io.requery.PropertyNameStyle) NestingKind(javax.lang.model.element.NestingKind) ElementFilter(javax.lang.model.util.ElementFilter) LinkedHashSet(java.util.LinkedHashSet) Name(javax.lang.model.element.Name) View(io.requery.View) Entity(io.requery.Entity) ElementKind(javax.lang.model.element.ElementKind) ExecutableElement(javax.lang.model.element.ExecutableElement) Cacheable(javax.persistence.Cacheable) Set(java.util.Set) ReadOnly(io.requery.ReadOnly) Element(javax.lang.model.element.Element) Collectors(java.util.stream.Collectors) TypeKind(javax.lang.model.type.TypeKind) Objects(java.util.Objects) SourceVersion(javax.lang.model.SourceVersion) TypeMirror(javax.lang.model.type.TypeMirror) List(java.util.List) Stream(java.util.stream.Stream) Index(javax.persistence.Index) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment) Annotation(java.lang.annotation.Annotation) Factory(io.requery.Factory) Optional(java.util.Optional) Embeddable(javax.persistence.Embeddable) Optional(java.util.Optional) Elements(javax.lang.model.util.Elements)

Example 97 with Annotation

use of java.lang.annotation.Annotation in project roboguice by roboguice.

the class Annotations method getKey.

/** Gets a key for the given type, member and annotations. */
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);
}
Also used : BindingAnnotation(com.google.inject.BindingAnnotation) ScopeAnnotation(com.google.inject.ScopeAnnotation) Annotation(java.lang.annotation.Annotation)

Example 98 with Annotation

use of java.lang.annotation.Annotation in project roboguice by roboguice.

the class FactoryProvider method createMethodMapping.

private static Map<Method, AssistedConstructor<?>> createMethodMapping(TypeLiteral<?> factoryType, TypeLiteral<?> implementationType) {
    List<AssistedConstructor<?>> constructors = Lists.newArrayList();
    for (Constructor<?> constructor : implementationType.getRawType().getDeclaredConstructors()) {
        if (constructor.isAnnotationPresent(AssistedInject.class)) {
            AssistedConstructor<?> assistedConstructor = AssistedConstructor.create(constructor, implementationType.getParameterTypes(constructor));
            constructors.add(assistedConstructor);
        }
    }
    if (constructors.isEmpty()) {
        return ImmutableMap.of();
    }
    Method[] factoryMethods = factoryType.getRawType().getMethods();
    if (constructors.size() != factoryMethods.length) {
        throw newConfigurationException("Constructor mismatch: %s has %s @AssistedInject " + "constructors, factory %s has %s creation methods", implementationType, constructors.size(), factoryType, factoryMethods.length);
    }
    Map<ParameterListKey, AssistedConstructor<?>> paramsToConstructor = Maps.newHashMap();
    for (AssistedConstructor<?> c : constructors) {
        if (paramsToConstructor.containsKey(c.getAssistedParameters())) {
            throw new RuntimeException("Duplicate constructor, " + c);
        }
        paramsToConstructor.put(c.getAssistedParameters(), c);
    }
    Map<Method, AssistedConstructor<?>> result = Maps.newHashMap();
    for (Method method : factoryMethods) {
        if (!method.getReturnType().isAssignableFrom(implementationType.getRawType())) {
            throw newConfigurationException("Return type of method %s is not assignable from %s", method, implementationType);
        }
        List<Type> parameterTypes = Lists.newArrayList();
        for (TypeLiteral<?> parameterType : factoryType.getParameterTypes(method)) {
            parameterTypes.add(parameterType.getType());
        }
        ParameterListKey methodParams = new ParameterListKey(parameterTypes);
        if (!paramsToConstructor.containsKey(methodParams)) {
            throw newConfigurationException("%s has no @AssistInject constructor that takes the " + "@Assisted parameters %s in that order. @AssistInject constructors are %s", implementationType, methodParams, paramsToConstructor.values());
        }
        method.getParameterAnnotations();
        for (Annotation[] parameterAnnotations : method.getParameterAnnotations()) {
            for (Annotation parameterAnnotation : parameterAnnotations) {
                if (parameterAnnotation.annotationType() == Assisted.class) {
                    throw newConfigurationException("Factory method %s has an @Assisted parameter, which " + "is incompatible with the deprecated @AssistedInject annotation. Please replace " + "@AssistedInject with @Inject on the %s constructor.", method, implementationType);
                }
            }
        }
        AssistedConstructor<?> matchingConstructor = paramsToConstructor.remove(methodParams);
        result.put(method, matchingConstructor);
    }
    return result;
}
Also used : Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) Type(java.lang.reflect.Type)

Example 99 with Annotation

use of java.lang.annotation.Annotation in project roboguice by roboguice.

the class Parameter method getBindingAnnotation.

/**
   * Returns the unique binding annotation from the specified list, or
   * {@code null} if there are none.
   *
   * @throws IllegalStateException if multiple binding annotations exist.
   */
private Annotation getBindingAnnotation(Annotation[] annotations) {
    Annotation bindingAnnotation = null;
    for (Annotation annotation : annotations) {
        if (Annotations.isBindingAnnotation(annotation.annotationType())) {
            checkArgument(bindingAnnotation == null, "Parameter has multiple binding annotations: %s and %s", bindingAnnotation, annotation);
            bindingAnnotation = annotation;
        }
    }
    return bindingAnnotation;
}
Also used : Annotation(java.lang.annotation.Annotation)

Example 100 with Annotation

use of java.lang.annotation.Annotation in project android-saripaar by ragunathjawahar.

the class Validator method getSaripaarAnnotatedFields.

private List<Field> getSaripaarAnnotatedFields(final Class<?> controllerClass) {
    Set<Class<? extends Annotation>> saripaarAnnotations = SARIPAAR_REGISTRY.getRegisteredAnnotations();
    List<Field> annotatedFields = new ArrayList<Field>();
    List<Field> controllerViewFields = getControllerViewFields(controllerClass);
    for (int i = 0, n = controllerViewFields.size(); i < n; i++) {
        Field field = controllerViewFields.get(i);
        if (isSaripaarAnnotatedField(field, saripaarAnnotations)) {
            annotatedFields.add(field);
        }
    }
    // Sort
    SaripaarFieldsComparator comparator = new SaripaarFieldsComparator();
    Collections.sort(annotatedFields, comparator);
    mOrderedFields = annotatedFields.size() == 1 ? annotatedFields.get(0).getAnnotation(Order.class) != null : annotatedFields.size() != 0 && comparator.areOrderedFields();
    return annotatedFields;
}
Also used : Order(com.mobsandgeeks.saripaar.annotation.Order) Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) Annotation(java.lang.annotation.Annotation)

Aggregations

Annotation (java.lang.annotation.Annotation)707 Method (java.lang.reflect.Method)171 ArrayList (java.util.ArrayList)99 Field (java.lang.reflect.Field)76 Test (org.junit.Test)66 Type (java.lang.reflect.Type)64 HashMap (java.util.HashMap)54 HashSet (java.util.HashSet)52 Map (java.util.Map)35 ParameterizedType (java.lang.reflect.ParameterizedType)34 List (java.util.List)30 Set (java.util.Set)27 InvocationTargetException (java.lang.reflect.InvocationTargetException)22 IOException (java.io.IOException)20 BindingAnnotation (com.google.inject.BindingAnnotation)17 AbstractModule (com.google.inject.AbstractModule)16 TypeElement (javax.lang.model.element.TypeElement)15 Injector (com.google.inject.Injector)14 MediaType (okhttp3.MediaType)14 AnnotatedElement (java.lang.reflect.AnnotatedElement)13