Search in sources :

Example 1 with MissingTypeException

use of com.google.auto.value.processor.MissingTypes.MissingTypeException in project auto by google.

the class TypeSimplifier method ambiguousNames.

private static Set<String> ambiguousNames(Types typeUtils, Set<TypeMirror> types) {
    Set<String> ambiguous = new HashSet<>();
    Map<String, Name> simpleNamesToQualifiedNames = new HashMap<>();
    for (TypeMirror type : types) {
        if (type.getKind() == TypeKind.ERROR) {
            throw new MissingTypeException(MoreTypes.asError(type));
        }
        String simpleName = typeUtils.asElement(type).getSimpleName().toString();
        /*
       * Compare by qualified names, because in Eclipse JDT, if Java 8 type annotations are used,
       * the same (unannotated) type may appear multiple times in the Set<TypeMirror>.
       * TODO(emcmanus): investigate further, because this might cause problems elsewhere.
       */
        Name qualifiedName = ((QualifiedNameable) typeUtils.asElement(type)).getQualifiedName();
        Name previous = simpleNamesToQualifiedNames.put(simpleName, qualifiedName);
        if (previous != null && !previous.equals(qualifiedName)) {
            ambiguous.add(simpleName);
        }
    }
    return ambiguous;
}
Also used : MissingTypeException(com.google.auto.value.processor.MissingTypes.MissingTypeException) HashMap(java.util.HashMap) TypeMirror(javax.lang.model.type.TypeMirror) QualifiedNameable(javax.lang.model.element.QualifiedNameable) HashSet(java.util.HashSet) Name(javax.lang.model.element.Name)

Example 2 with MissingTypeException

use of com.google.auto.value.processor.MissingTypes.MissingTypeException in project auto by google.

the class AutoBuilderProcessor method findOfClassValue.

private TypeElement findOfClassValue(AnnotationMirror autoBuilderAnnotation) {
    AnnotationValue ofClassValue = AnnotationMirrors.getAnnotationValue(autoBuilderAnnotation, "ofClass");
    Object value = ofClassValue.getValue();
    if (value instanceof TypeMirror) {
        TypeMirror ofClassType = (TypeMirror) value;
        switch(ofClassType.getKind()) {
            case DECLARED:
                return MoreTypes.asTypeElement(ofClassType);
            case ERROR:
                throw new MissingTypeException(MoreTypes.asError(ofClassType));
            default:
                break;
        }
    }
    throw new MissingTypeException(null);
}
Also used : MissingTypeException(com.google.auto.value.processor.MissingTypes.MissingTypeException) TypeMirror(javax.lang.model.type.TypeMirror) AnnotationValue(javax.lang.model.element.AnnotationValue)

Example 3 with MissingTypeException

use of com.google.auto.value.processor.MissingTypes.MissingTypeException in project auto by google.

the class AutoValueishProcessor method process.

@Override
public final boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    if (annotationType == null) {
        // This should not happen. If the annotation type is not found, how did the processor get
        // triggered?
        processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Did not process @" + annotationClassName + " because the annotation class was not found");
        return false;
    }
    List<TypeElement> deferredTypes = deferredTypeNames.stream().map(name -> elementUtils().getTypeElement(name)).collect(toList());
    if (roundEnv.processingOver()) {
        // was in deferredTypes.
        for (TypeElement type : deferredTypes) {
            errorReporter.reportError(type, "[%sUndefined] Did not generate @%s class for %s because it references" + " undefined types", simpleAnnotationName, simpleAnnotationName, type.getQualifiedName());
        }
        return false;
    }
    Collection<? extends Element> annotatedElements = roundEnv.getElementsAnnotatedWith(annotationType);
    List<TypeElement> types = new ImmutableList.Builder<TypeElement>().addAll(deferredTypes).addAll(ElementFilter.typesIn(annotatedElements)).build();
    deferredTypeNames.clear();
    for (TypeElement type : types) {
        try {
            validateType(type);
            processType(type);
        } catch (AbortProcessingException e) {
        // We abandoned this type; continue with the next.
        } catch (MissingTypeException e) {
            // We abandoned this type, but only because we needed another type that it references and
            // that other type was missing. It is possible that the missing type will be generated by
            // further annotation processing, so we will try again on the next round (perhaps failing
            // again and adding it back to the list).
            addDeferredType(type);
        } catch (RuntimeException e) {
            String trace = Throwables.getStackTraceAsString(e);
            errorReporter.reportError(type, "[%sException] @%s processor threw an exception: %s", simpleAnnotationName, simpleAnnotationName, trace);
            throw e;
        }
    }
    // never claim annotation, because who knows what other processors want?
    return false;
}
Also used : Arrays(java.util.Arrays) AbstractProcessor(javax.annotation.processing.AbstractProcessor) Modifier(javax.lang.model.element.Modifier) Inherited(java.lang.annotation.Inherited) TypeElement(javax.lang.model.element.TypeElement) Elements(javax.lang.model.util.Elements) MoreStreams.toImmutableSet(com.google.auto.common.MoreStreams.toImmutableSet) Diagnostic(javax.tools.Diagnostic) Sets.union(com.google.common.collect.Sets.union) Map(java.util.Map) MoreElements.isAnnotationPresent(com.google.auto.common.MoreElements.isAnnotationPresent) Collectors.toSet(java.util.stream.Collectors.toSet) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) EnumMap(java.util.EnumMap) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) Element(javax.lang.model.element.Element) Types(javax.lang.model.util.Types) Collectors.joining(java.util.stream.Collectors.joining) Serializable(java.io.Serializable) TypeKind(javax.lang.model.type.TypeKind) AnnotationMirrors.getAnnotationValue(com.google.auto.common.AnnotationMirrors.getAnnotationValue) JavaFileObject(javax.tools.JavaFileObject) SourceVersion(javax.lang.model.SourceVersion) List(java.util.List) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) Writer(java.io.Writer) Optional(java.util.Optional) AnnotationValue(javax.lang.model.element.AnnotationValue) GeneratedAnnotations.generatedAnnotation(com.google.auto.common.GeneratedAnnotations.generatedAnnotation) IntStream(java.util.stream.IntStream) MoreTypes(com.google.auto.common.MoreTypes) COPY_ANNOTATIONS_NAME(com.google.auto.value.processor.ClassNames.COPY_ANNOTATIONS_NAME) VariableElement(javax.lang.model.element.VariableElement) MissingTypeException(com.google.auto.value.processor.MissingTypes.MissingTypeException) OptionalInt(java.util.OptionalInt) ArrayList(java.util.ArrayList) AUTO_VALUE_PACKAGE_NAME(com.google.auto.value.processor.ClassNames.AUTO_VALUE_PACKAGE_NAME) ImmutableBiMap(com.google.common.collect.ImmutableBiMap) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Collectors.toCollection(java.util.stream.Collectors.toCollection) MoreElements.getPackage(com.google.auto.common.MoreElements.getPackage) ImmutableList(com.google.common.collect.ImmutableList) DeclaredType(javax.lang.model.type.DeclaredType) QualifiedNameable(javax.lang.model.element.QualifiedNameable) ElementFilter(javax.lang.model.util.ElementFilter) ElementFilter.constructorsIn(javax.lang.model.util.ElementFilter.constructorsIn) Name(javax.lang.model.element.Name) SimpleAnnotationValueVisitor8(javax.lang.model.util.SimpleAnnotationValueVisitor8) MoreElements(com.google.auto.common.MoreElements) ElementKind(javax.lang.model.element.ElementKind) ExecutableElement(javax.lang.model.element.ExecutableElement) Throwables(com.google.common.base.Throwables) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) IOException(java.io.IOException) Target(java.lang.annotation.Target) ElementType(java.lang.annotation.ElementType) Visibility(com.google.auto.common.Visibility) AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeParameterElement(javax.lang.model.element.TypeParameterElement) MoreStreams.toImmutableList(com.google.auto.common.MoreStreams.toImmutableList) Collectors.toList(java.util.stream.Collectors.toList) TypeMirror(javax.lang.model.type.TypeMirror) RoundEnvironment(javax.annotation.processing.RoundEnvironment) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment) MissingTypeException(com.google.auto.value.processor.MissingTypes.MissingTypeException) TypeElement(javax.lang.model.element.TypeElement) ImmutableList(com.google.common.collect.ImmutableList) MoreStreams.toImmutableList(com.google.auto.common.MoreStreams.toImmutableList)

Example 4 with MissingTypeException

use of com.google.auto.value.processor.MissingTypes.MissingTypeException in project auto by google.

the class AutoOneOfProcessor method mirrorForKindType.

private DeclaredType mirrorForKindType(TypeElement autoOneOfType) {
    // The annotation is guaranteed to be present by the contract of Processor#process
    AnnotationMirror oneOfAnnotation = getAnnotationMirror(autoOneOfType, AUTO_ONE_OF_NAME).get();
    AnnotationValue kindValue = AnnotationMirrors.getAnnotationValue(oneOfAnnotation, "value");
    Object value = kindValue.getValue();
    if (value instanceof TypeMirror) {
        TypeMirror kindType = (TypeMirror) value;
        switch(kindType.getKind()) {
            case DECLARED:
                return MoreTypes.asDeclared(kindType);
            case ERROR:
                throw new MissingTypeException(MoreTypes.asError(kindType));
            default:
                break;
        }
    }
    throw new MissingTypeException(null);
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) MissingTypeException(com.google.auto.value.processor.MissingTypes.MissingTypeException) TypeMirror(javax.lang.model.type.TypeMirror) AnnotationValue(javax.lang.model.element.AnnotationValue)

Aggregations

MissingTypeException (com.google.auto.value.processor.MissingTypes.MissingTypeException)4 TypeMirror (javax.lang.model.type.TypeMirror)4 AnnotationValue (javax.lang.model.element.AnnotationValue)3 HashSet (java.util.HashSet)2 AnnotationMirror (javax.lang.model.element.AnnotationMirror)2 AnnotationMirrors.getAnnotationValue (com.google.auto.common.AnnotationMirrors.getAnnotationValue)1 GeneratedAnnotations.generatedAnnotation (com.google.auto.common.GeneratedAnnotations.generatedAnnotation)1 MoreElements (com.google.auto.common.MoreElements)1 MoreElements.getPackage (com.google.auto.common.MoreElements.getPackage)1 MoreElements.isAnnotationPresent (com.google.auto.common.MoreElements.isAnnotationPresent)1 MoreStreams.toImmutableList (com.google.auto.common.MoreStreams.toImmutableList)1 MoreStreams.toImmutableSet (com.google.auto.common.MoreStreams.toImmutableSet)1 MoreTypes (com.google.auto.common.MoreTypes)1 Visibility (com.google.auto.common.Visibility)1 AUTO_VALUE_PACKAGE_NAME (com.google.auto.value.processor.ClassNames.AUTO_VALUE_PACKAGE_NAME)1 COPY_ANNOTATIONS_NAME (com.google.auto.value.processor.ClassNames.COPY_ANNOTATIONS_NAME)1 Throwables (com.google.common.base.Throwables)1 ImmutableBiMap (com.google.common.collect.ImmutableBiMap)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)1