Search in sources :

Example 1 with SubContext

use of com.google.errorprone.SubContext in project error-prone by google.

the class UTemplater method createTemplate.

/**
   * Returns a template based on a method. One-line methods starting with a {@code return} statement
   * are guessed to be expression templates, and all other methods are guessed to be block
   * templates.
   */
public static Template<?> createTemplate(Context context, MethodTree decl) {
    MethodSymbol declSym = ASTHelpers.getSymbol(decl);
    ImmutableClassToInstanceMap<Annotation> annotations = UTemplater.annotationMap(declSym);
    ImmutableMap<String, VarSymbol> freeExpressionVars = freeExpressionVariables(decl);
    Context subContext = new SubContext(context);
    final UTemplater templater = new UTemplater(freeExpressionVars, subContext);
    ImmutableMap<String, UType> expressionVarTypes = ImmutableMap.copyOf(Maps.transformValues(freeExpressionVars, new Function<VarSymbol, UType>() {

        @Override
        public UType apply(VarSymbol sym) {
            return templater.template(sym.type);
        }
    }));
    UType genericType = templater.template(declSym.type);
    List<UTypeVar> typeParameters;
    UMethodType methodType;
    if (genericType instanceof UForAll) {
        UForAll forAllType = (UForAll) genericType;
        typeParameters = forAllType.getTypeVars();
        methodType = (UMethodType) forAllType.getQuantifiedType();
    } else if (genericType instanceof UMethodType) {
        typeParameters = ImmutableList.of();
        methodType = (UMethodType) genericType;
    } else {
        throw new IllegalArgumentException("Expected genericType to be either a ForAll or a UMethodType, but was " + genericType);
    }
    List<? extends StatementTree> bodyStatements = decl.getBody().getStatements();
    if (bodyStatements.size() == 1 && Iterables.getOnlyElement(bodyStatements).getKind() == Kind.RETURN && context.get(REQUIRE_BLOCK_KEY) == null) {
        ExpressionTree expression = ((ReturnTree) Iterables.getOnlyElement(bodyStatements)).getExpression();
        return ExpressionTemplate.create(annotations, typeParameters, expressionVarTypes, templater.template(expression), methodType.getReturnType());
    } else {
        List<UStatement> templateStatements = new ArrayList<>();
        for (StatementTree statement : bodyStatements) {
            templateStatements.add(templater.template(statement));
        }
        return BlockTemplate.create(annotations, typeParameters, expressionVarTypes, templateStatements);
    }
}
Also used : Context(com.sun.tools.javac.util.Context) SubContext(com.google.errorprone.SubContext) ArrayList(java.util.ArrayList) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) ReturnTree(com.sun.source.tree.ReturnTree) Annotation(java.lang.annotation.Annotation) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) LabeledStatementTree(com.sun.source.tree.LabeledStatementTree) StatementTree(com.sun.source.tree.StatementTree) EmptyStatementTree(com.sun.source.tree.EmptyStatementTree) Function(com.google.common.base.Function) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) SubContext(com.google.errorprone.SubContext) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 2 with SubContext

use of com.google.errorprone.SubContext in project error-prone by google.

the class RefasterRule method prepareContext.

private Context prepareContext(Context baseContext, JCCompilationUnit compilationUnit) {
    Context context = new SubContext(baseContext);
    if (context.get(JavaFileManager.class) == null) {
        JavacFileManager.preRegister(context);
    }
    context.put(JCCompilationUnit.class, compilationUnit);
    context.put(PackageSymbol.class, compilationUnit.packge);
    context.put(RULE_TYPE_VARS, typeVariables());
    return context;
}
Also used : SubContext(com.google.errorprone.SubContext) Context(com.sun.tools.javac.util.Context) JavaFileManager(javax.tools.JavaFileManager) SubContext(com.google.errorprone.SubContext)

Aggregations

SubContext (com.google.errorprone.SubContext)2 Context (com.sun.tools.javac.util.Context)2 Function (com.google.common.base.Function)1 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)1 EmptyStatementTree (com.sun.source.tree.EmptyStatementTree)1 ExpressionStatementTree (com.sun.source.tree.ExpressionStatementTree)1 ExpressionTree (com.sun.source.tree.ExpressionTree)1 LabeledStatementTree (com.sun.source.tree.LabeledStatementTree)1 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)1 ReturnTree (com.sun.source.tree.ReturnTree)1 StatementTree (com.sun.source.tree.StatementTree)1 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)1 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)1 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 JavaFileManager (javax.tools.JavaFileManager)1