Search in sources :

Example 1 with ImmutableClassToInstanceMap

use of com.google.common.collect.ImmutableClassToInstanceMap in project error-prone by google.

the class UTemplater method annotationMap.

@SuppressWarnings("unchecked")
public static ImmutableClassToInstanceMap<Annotation> annotationMap(Symbol symbol) {
    ImmutableClassToInstanceMap.Builder<Annotation> builder = ImmutableClassToInstanceMap.builder();
    for (Compound compound : symbol.getAnnotationMirrors()) {
        Name qualifiedAnnotationType = ((TypeElement) compound.getAnnotationType().asElement()).getQualifiedName();
        try {
            Class<? extends Annotation> annotationClazz = Class.forName(qualifiedAnnotationType.toString()).asSubclass(Annotation.class);
            builder.put((Class) annotationClazz, AnnotationProxyMaker.generateAnnotation(compound, annotationClazz));
        } catch (ClassNotFoundException e) {
            throw new IllegalArgumentException("Unrecognized annotation type", e);
        }
    }
    return builder.build();
}
Also used : ImmutableClassToInstanceMap(com.google.common.collect.ImmutableClassToInstanceMap) TypeElement(javax.lang.model.element.TypeElement) Compound(com.sun.tools.javac.code.Attribute.Compound) Annotation(java.lang.annotation.Annotation) Name(javax.lang.model.element.Name)

Example 2 with ImmutableClassToInstanceMap

use of com.google.common.collect.ImmutableClassToInstanceMap in project error-prone by google.

the class RefasterRuleBuilderScanner method visitMethod.

@Override
public Void visitMethod(MethodTree tree, Void v) {
    try {
        VisitorState state = new VisitorState(context);
        logger.log(FINE, "Discovered method with name {0}", tree.getName());
        if (ASTHelpers.hasAnnotation(tree, Placeholder.class, state)) {
            checkArgument(tree.getModifiers().getFlags().contains(Modifier.ABSTRACT), "@Placeholder methods are expected to be abstract");
            UTemplater templater = new UTemplater(context);
            ImmutableMap.Builder<UVariableDecl, ImmutableClassToInstanceMap<Annotation>> params = ImmutableMap.builder();
            for (VariableTree param : tree.getParameters()) {
                params.put(templater.visitVariable(param, null), UTemplater.annotationMap(ASTHelpers.getSymbol(param)));
            }
            MethodSymbol sym = ASTHelpers.getSymbol(tree);
            placeholderMethods.put(sym, PlaceholderMethod.create(tree.getName(), templater.template(sym.getReturnType()), params.build(), UTemplater.annotationMap(sym)));
        } else if (ASTHelpers.hasAnnotation(tree, BeforeTemplate.class, state)) {
            checkState(afterTemplates.isEmpty(), "BeforeTemplate must come before AfterTemplate");
            Template<?> template = UTemplater.createTemplate(context, tree);
            beforeTemplates.add(template);
            if (template instanceof BlockTemplate) {
                context.put(UTemplater.REQUIRE_BLOCK_KEY, true);
            }
        } else if (ASTHelpers.hasAnnotation(tree, AfterTemplate.class, state)) {
            afterTemplates.add(UTemplater.createTemplate(context, tree));
        } else if (tree.getModifiers().getFlags().contains(Modifier.ABSTRACT)) {
            throw new IllegalArgumentException("Placeholder methods must have @Placeholder, but abstract method does not: " + tree);
        }
        return null;
    } catch (Throwable t) {
        throw new RuntimeException("Error analysing: " + tree.getName(), t);
    }
}
Also used : BeforeTemplate(com.google.errorprone.refaster.annotation.BeforeTemplate) ImmutableClassToInstanceMap(com.google.common.collect.ImmutableClassToInstanceMap) VariableTree(com.sun.source.tree.VariableTree) ImmutableMap(com.google.common.collect.ImmutableMap) BeforeTemplate(com.google.errorprone.refaster.annotation.BeforeTemplate) AfterTemplate(com.google.errorprone.refaster.annotation.AfterTemplate) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) VisitorState(com.google.errorprone.VisitorState)

Aggregations

ImmutableClassToInstanceMap (com.google.common.collect.ImmutableClassToInstanceMap)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 VisitorState (com.google.errorprone.VisitorState)1 AfterTemplate (com.google.errorprone.refaster.annotation.AfterTemplate)1 BeforeTemplate (com.google.errorprone.refaster.annotation.BeforeTemplate)1 VariableTree (com.sun.source.tree.VariableTree)1 Compound (com.sun.tools.javac.code.Attribute.Compound)1 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)1 Annotation (java.lang.annotation.Annotation)1 Name (javax.lang.model.element.Name)1 TypeElement (javax.lang.model.element.TypeElement)1