Search in sources :

Example 1 with AfterTemplate

use of com.google.errorprone.refaster.annotation.AfterTemplate 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, /* data= */
                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)1 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 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)1