Search in sources :

Example 1 with VarSymbol

use of com.sun.tools.javac.code.Symbol.VarSymbol in project j2objc by google.

the class TreeConverter method convertVariableExpression.

private VariableDeclarationExpression convertVariableExpression(JCTree.JCVariableDecl node) {
    VarSymbol var = node.sym;
    boolean isVarargs = (node.sym.flags() & Flags.VARARGS) > 0;
    Type newType = convertType(var.asType(), getPosition(node), isVarargs);
    VariableDeclarationFragment fragment = new VariableDeclarationFragment();
    fragment.setVariableElement(var).setInitializer((Expression) convert(node.getInitializer()));
    return new VariableDeclarationExpression().setType(newType).addFragment(fragment);
}
Also used : ParameterizedType(com.google.devtools.j2objc.ast.ParameterizedType) Type(com.google.devtools.j2objc.ast.Type) SimpleType(com.google.devtools.j2objc.ast.SimpleType) DeclaredType(javax.lang.model.type.DeclaredType) ArrayType(com.google.devtools.j2objc.ast.ArrayType) PrimitiveType(com.google.devtools.j2objc.ast.PrimitiveType) UnionType(com.google.devtools.j2objc.ast.UnionType) ExecutableType(javax.lang.model.type.ExecutableType) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Example 2 with VarSymbol

use of com.sun.tools.javac.code.Symbol.VarSymbol in project j2objc by google.

the class TreeConverter method convertVariableDeclaration.

private TreeNode convertVariableDeclaration(JCTree.JCVariableDecl node) {
    VarSymbol var = node.sym;
    if (var.getKind() == ElementKind.FIELD) {
        FieldDeclaration newNode = new FieldDeclaration(var, (Expression) convert(node.getInitializer()));
        convertBodyDeclaration(node, node.getModifiers(), newNode, var);
        return newNode;
    }
    if (var.getKind() == ElementKind.LOCAL_VARIABLE) {
        return new VariableDeclarationStatement(var, (Expression) convert(node.getInitializer()));
    }
    if (var.getKind() == ElementKind.ENUM_CONSTANT) {
        EnumConstantDeclaration newNode = new EnumConstantDeclaration().setVariableElement(var);
        convertBodyDeclaration(node, node.getModifiers(), newNode, var);
        ClassInstanceCreation init = (ClassInstanceCreation) convert(node.getInitializer());
        TreeUtil.moveList(init.getArguments(), newNode.getArguments());
        if (init.getAnonymousClassDeclaration() != null) {
            newNode.setAnonymousClassDeclaration(TreeUtil.remove(init.getAnonymousClassDeclaration()));
        }
        return newNode.setExecutablePair(init.getExecutablePair()).setVarargsType(init.getVarargsType());
    }
    return convertSingleVariable(node);
}
Also used : ClassInstanceCreation(com.google.devtools.j2objc.ast.ClassInstanceCreation) EnumConstantDeclaration(com.google.devtools.j2objc.ast.EnumConstantDeclaration) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) FieldDeclaration(com.google.devtools.j2objc.ast.FieldDeclaration)

Example 3 with VarSymbol

use of com.sun.tools.javac.code.Symbol.VarSymbol in project j2objc by google.

the class TreeConverter method convertSingleVariable.

private TreeNode convertSingleVariable(JCTree.JCVariableDecl node) {
    VarSymbol var = node.sym;
    SourcePosition pos = getPosition(node);
    boolean isVarargs = (node.sym.flags() & Flags.VARARGS) > 0;
    Type newType = convertType(var.asType(), pos, isVarargs);
    return new SingleVariableDeclaration().setType(newType).setIsVarargs(isVarargs).setAnnotations(convertAnnotations(node.getModifiers())).setVariableElement(var).setInitializer((Expression) convert(node.getInitializer()));
}
Also used : ParameterizedType(com.google.devtools.j2objc.ast.ParameterizedType) Type(com.google.devtools.j2objc.ast.Type) SimpleType(com.google.devtools.j2objc.ast.SimpleType) DeclaredType(javax.lang.model.type.DeclaredType) ArrayType(com.google.devtools.j2objc.ast.ArrayType) PrimitiveType(com.google.devtools.j2objc.ast.PrimitiveType) UnionType(com.google.devtools.j2objc.ast.UnionType) ExecutableType(javax.lang.model.type.ExecutableType) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) SourcePosition(com.google.devtools.j2objc.ast.SourcePosition) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Example 4 with VarSymbol

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

the class TryFailThrowable method hasInitialStringParameter.

private static boolean hasInitialStringParameter(MethodSymbol sym, VisitorState state) {
    Types types = state.getTypes();
    List<VarSymbol> parameters = sym.getParameters();
    return !parameters.isEmpty() && types.isSameType(parameters.get(0).type, state.getSymtab().stringType);
}
Also used : Types(com.sun.tools.javac.code.Types) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Example 5 with VarSymbol

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

the class TypeParameterUnusedInFormals method matchMethod.

@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
    MethodSymbol methodSymbol = ASTHelpers.getSymbol(tree);
    if (methodSymbol == null) {
        return Description.NO_MATCH;
    }
    // Only match methods where the return type is just a type parameter.
    // e.g. the following is OK: <T> List<T> newArrayList();
    TypeVar retType;
    switch(methodSymbol.getReturnType().getKind()) {
        case TYPEVAR:
            retType = (TypeVar) methodSymbol.getReturnType();
            break;
        default:
            return Description.NO_MATCH;
    }
    if (!methodSymbol.equals(retType.tsym.owner)) {
        return Description.NO_MATCH;
    }
    // e.g.: <T extends Enum<T>> T unsafeEnumDeserializer();
    if (retType.bound != null && TypeParameterFinder.visit(retType.bound).contains(retType.tsym)) {
        return Description.NO_MATCH;
    }
    // e.g.: <T> T noop(T t);
    for (VarSymbol formalParam : methodSymbol.getParameters()) {
        if (TypeParameterFinder.visit(formalParam.type).contains(retType.tsym)) {
            return Description.NO_MATCH;
        }
    }
    return describeMatch(tree);
}
Also used : TypeVar(com.sun.tools.javac.code.Type.TypeVar) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Aggregations

VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)68 Symbol (com.sun.tools.javac.code.Symbol)29 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)23 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)22 Type (com.sun.tools.javac.code.Type)20 ExpressionTree (com.sun.source.tree.ExpressionTree)15 Tree (com.sun.source.tree.Tree)15 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)13 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)11 VariableTree (com.sun.source.tree.VariableTree)11 ClassTree (com.sun.source.tree.ClassTree)10 IdentifierTree (com.sun.source.tree.IdentifierTree)10 MemberSelectTree (com.sun.source.tree.MemberSelectTree)10 PackageSymbol (com.sun.tools.javac.code.Symbol.PackageSymbol)10 NewClassTree (com.sun.source.tree.NewClassTree)9 ArrayList (java.util.ArrayList)9 VisitorState (com.google.errorprone.VisitorState)7 MethodTree (com.sun.source.tree.MethodTree)7 TreePath (com.sun.source.util.TreePath)7 ClassType (com.sun.tools.javac.code.Type.ClassType)7