Search in sources :

Example 31 with VarSymbol

use of com.sun.tools.javac.code.Symbol.VarSymbol in project ceylon-compiler by ceylon.

the class JavacMethod method getParameters.

@Override
public List<VariableMirror> getParameters() {
    if (parameters == null) {
        com.sun.tools.javac.util.List<VarSymbol> params = methodSymbol.getParameters();
        List<VariableMirror> ret = new ArrayList<VariableMirror>(params.size());
        for (VarSymbol parameter : params) ret.add(new JavacVariable(parameter));
        parameters = Collections.unmodifiableList(ret);
    }
    return parameters;
}
Also used : VariableMirror(com.redhat.ceylon.model.loader.mirror.VariableMirror) ArrayList(java.util.ArrayList) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Example 32 with VarSymbol

use of com.sun.tools.javac.code.Symbol.VarSymbol in project bazel by bazelbuild.

the class AnnotationUtils method getElementValueEnum.

/**
     * Version that is suitable for Enum elements.
     */
public static <T extends Enum<T>> T getElementValueEnum(AnnotationMirror anno, CharSequence name, Class<T> t, boolean useDefaults) {
    VarSymbol vs = getElementValue(anno, name, VarSymbol.class, useDefaults);
    T value = Enum.valueOf(t, vs.getSimpleName().toString());
    return value;
}
Also used : VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Example 33 with VarSymbol

use of com.sun.tools.javac.code.Symbol.VarSymbol in project error-prone by google.

the class ASTHelpers method enumValues.

/** @return all values of the given enum type, in declaration order. */
public static LinkedHashSet<String> enumValues(TypeSymbol enumType) {
    if (enumType.getKind() != ElementKind.ENUM) {
        throw new IllegalStateException();
    }
    Scope scope = enumType.members();
    Deque<String> values = new ArrayDeque<>();
    for (Symbol sym : scope.getSymbols()) {
        if (sym instanceof VarSymbol) {
            VarSymbol var = (VarSymbol) sym;
            if ((var.flags() & Flags.ENUM) != 0) {
                /**
           * Javac gives us the members backwards, apparently. It's worth making an effort to
           * preserve declaration order because it's useful for diagnostics (e.g. in
           * {@link MissingCasesInEnumSwitch}).
           */
                values.push(sym.name.toString());
            }
        }
    }
    return new LinkedHashSet<>(values);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Scope(com.sun.tools.javac.code.Scope) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) Symbol(com.sun.tools.javac.code.Symbol) PackageSymbol(com.sun.tools.javac.code.Symbol.PackageSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) ArrayDeque(java.util.ArrayDeque)

Example 34 with VarSymbol

use of com.sun.tools.javac.code.Symbol.VarSymbol in project error-prone by google.

the class SimpleDateFormatConstant method matchVariable.

@Override
public Description matchVariable(VariableTree tree, VisitorState state) {
    if (tree.getInitializer() == null) {
        return NO_MATCH;
    }
    VarSymbol sym = ASTHelpers.getSymbol(tree);
    if (sym == null || sym.getKind() != ElementKind.FIELD) {
        return NO_MATCH;
    }
    String name = sym.getSimpleName().toString();
    if (!(sym.isStatic() && sym.getModifiers().contains(Modifier.FINAL))) {
        return NO_MATCH;
    }
    if (!name.equals(name.toUpperCase())) {
        return NO_MATCH;
    }
    if (!isSameType(getType(tree), state.getTypeFromString("java.text.SimpleDateFormat"), state)) {
        return NO_MATCH;
    }
    return buildDescription(tree).addFix(threadLocalFix(tree, state, sym)).addFix(renameVariable(tree, CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, tree.getName().toString()), state)).build();
}
Also used : VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Example 35 with VarSymbol

use of com.sun.tools.javac.code.Symbol.VarSymbol in project error-prone by google.

the class FormatStringAnnotationChecker method matchMethod.

@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
    Type stringType = state.getSymtab().stringType;
    boolean isFormatMethod = ASTHelpers.hasAnnotation(ASTHelpers.getSymbol(tree), FormatMethod.class, state);
    boolean foundFormatString = false;
    boolean foundString = false;
    for (VariableTree param : tree.getParameters()) {
        VarSymbol paramSymbol = ASTHelpers.getSymbol(param);
        boolean isStringParam = ASTHelpers.isSameType(paramSymbol.type, stringType, state);
        if (isStringParam) {
            foundString = true;
        }
        if (ASTHelpers.hasAnnotation(paramSymbol, FormatString.class, state)) {
            if (!isFormatMethod) {
                return buildDescription(tree).setMessage("A parameter can only be annotated @FormatString in a method annotated " + "@FormatMethod: " + param).build();
            }
            if (!isStringParam) {
                return buildDescription(param).setMessage("Only strings can be annotated @FormatString.").build();
            }
            if (foundFormatString) {
                return buildDescription(tree).setMessage("A method cannot have more than one @FormatString parameter.").build();
            }
            foundFormatString = true;
        }
    }
    if (isFormatMethod && !foundString) {
        return buildDescription(tree).setMessage("An @FormatMethod must contain at least one String parameter.").build();
    }
    return Description.NO_MATCH;
}
Also used : Type(com.sun.tools.javac.code.Type) VariableTree(com.sun.source.tree.VariableTree) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Aggregations

VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)45 Symbol (com.sun.tools.javac.code.Symbol)24 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)21 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)16 Type (com.sun.tools.javac.code.Type)12 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)11 ExpressionTree (com.sun.source.tree.ExpressionTree)10 PackageSymbol (com.sun.tools.javac.code.Symbol.PackageSymbol)10 Tree (com.sun.source.tree.Tree)9 IdentifierTree (com.sun.source.tree.IdentifierTree)7 MemberSelectTree (com.sun.source.tree.MemberSelectTree)7 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)7 ClassType (com.sun.tools.javac.code.Type.ClassType)7 ClassTree (com.sun.source.tree.ClassTree)6 NewClassTree (com.sun.source.tree.NewClassTree)6 VariableTree (com.sun.source.tree.VariableTree)6 BlockTree (com.sun.source.tree.BlockTree)5 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)5 EnhancedForLoopTree (com.sun.source.tree.EnhancedForLoopTree)5 ForLoopTree (com.sun.source.tree.ForLoopTree)5