Search in sources :

Example 16 with ClassType

use of com.sun.tools.javac.code.Type.ClassType in project checker-framework by typetools.

the class WholeProgramInferenceScenes method getEnclosingClassSymbol.

/**
 * Returns the ClassSymbol of the class encapsulating the node n passed as parameter.
 *
 * <p>If the receiver of field is an instance of "this", the implementation obtains the
 * ClassSymbol by using classTree. Otherwise, the ClassSymbol is from the field's receiver.
 */
// TODO: These methods below could be moved somewhere else.
private ClassSymbol getEnclosingClassSymbol(ClassTree classTree, Node field) {
    Node receiverNode = null;
    if (field instanceof FieldAccessNode) {
        receiverNode = ((FieldAccessNode) field).getReceiver();
    } else if (field instanceof LocalVariableNode) {
        receiverNode = ((LocalVariableNode) field).getReceiver();
    } else {
        ErrorReporter.errorAbort("Unexpected type: " + field.getClass());
    }
    if ((receiverNode == null || receiverNode instanceof ImplicitThisLiteralNode) && classTree != null) {
        return (ClassSymbol) TreeUtils.elementFromTree(classTree);
    }
    TypeMirror type = receiverNode.getType();
    if (type instanceof ClassType) {
        TypeSymbol tsym = ((ClassType) type).asElement();
        return tsym.enclClass();
    }
    return getEnclosingClassSymbol(receiverNode.getTree());
}
Also used : ImplicitThisLiteralNode(org.checkerframework.dataflow.cfg.node.ImplicitThisLiteralNode) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) FieldAccessNode(org.checkerframework.dataflow.cfg.node.FieldAccessNode) ObjectCreationNode(org.checkerframework.dataflow.cfg.node.ObjectCreationNode) ReturnNode(org.checkerframework.dataflow.cfg.node.ReturnNode) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) ImplicitThisLiteralNode(org.checkerframework.dataflow.cfg.node.ImplicitThisLiteralNode) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode) Node(org.checkerframework.dataflow.cfg.node.Node) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) ClassType(com.sun.tools.javac.code.Type.ClassType) FieldAccessNode(org.checkerframework.dataflow.cfg.node.FieldAccessNode) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode)

Example 17 with ClassType

use of com.sun.tools.javac.code.Type.ClassType in project checker-framework by typetools.

the class FlowExpressionParseUtil method getTypeOfEnclosingClass.

/**
 * Returns the type of the inner most enclosing class.Type.noType is returned if no enclosing
 * class is found. This is in contrast to {@link DeclaredType#getEnclosingType()} which returns
 * the type of the inner most instance. If the inner most enclosing class is static this method
 * will return the type of that class where as {@link DeclaredType#getEnclosingType()} will
 * return the type of the inner most enclosing class that is not static.
 *
 * @param type a DeclaredType
 * @return the type of the innermost enclosing class or Type.noType
 */
private static TypeMirror getTypeOfEnclosingClass(DeclaredType type) {
    if (type instanceof ClassType) {
        // enclClass() needs to be called on tsym.owner,
        // otherwise it simply returns tsym.
        Symbol sym = ((ClassType) type).tsym.owner;
        if (sym == null) {
            return Type.noType;
        }
        ClassSymbol cs = sym.enclClass();
        if (cs == null) {
            return Type.noType;
        }
        return cs.asType();
    } else {
        return type.getEnclosingType();
    }
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Symbol(com.sun.tools.javac.code.Symbol) PackageSymbol(com.sun.tools.javac.code.Symbol.PackageSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) ClassType(com.sun.tools.javac.code.Type.ClassType)

Example 18 with ClassType

use of com.sun.tools.javac.code.Type.ClassType in project checker-framework by typetools.

the class FlowExpressionParseUtil method parseSuper.

private static Receiver parseSuper(String s, Types types, FlowExpressionContext context) throws FlowExpressionParseException {
    // super literal
    List<? extends TypeMirror> superTypes = types.directSupertypes(context.receiver.getType());
    // find class supertype
    TypeMirror superType = null;
    for (TypeMirror t : superTypes) {
        // ignore interface types
        if (!(t instanceof ClassType)) {
            continue;
        }
        ClassType tt = (ClassType) t;
        if (!tt.isInterface()) {
            superType = t;
            break;
        }
    }
    if (superType == null) {
        throw constructParserException(s, "super class not found");
    }
    return new ThisReference(superType);
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) ClassType(com.sun.tools.javac.code.Type.ClassType) ThisReference(org.checkerframework.dataflow.analysis.FlowExpressions.ThisReference)

Example 19 with ClassType

use of com.sun.tools.javac.code.Type.ClassType in project lombok by rzwitserloot.

the class JavacResolution method typeToJCTree0.

private static JCExpression typeToJCTree0(Type type, JavacAST ast, boolean allowCompound, boolean allowVoid) throws TypeNotConvertibleException {
    // NB: There's such a thing as maker.Type(type), but this doesn't work very well; it screws up anonymous classes, captures, and adds an extra prefix dot for some reason too.
    // -- so we write our own take on that here.
    JavacTreeMaker maker = ast.getTreeMaker();
    if (CTC_BOT.equals(typeTag(type)))
        return createJavaLangObject(ast);
    if (CTC_VOID.equals(typeTag(type)))
        return allowVoid ? primitiveToJCTree(type.getKind(), maker) : createJavaLangObject(ast);
    if (type.isPrimitive())
        return primitiveToJCTree(type.getKind(), maker);
    if (type.isErroneous())
        throw new TypeNotConvertibleException("Type cannot be resolved");
    TypeSymbol symbol = type.asElement();
    List<Type> generics = type.getTypeArguments();
    JCExpression replacement = null;
    if (symbol == null)
        throw new TypeNotConvertibleException("Null or compound type");
    if (symbol.name.length() == 0) {
        // Anonymous inner class
        if (type instanceof ClassType) {
            List<Type> ifaces = ((ClassType) type).interfaces_field;
            Type supertype = ((ClassType) type).supertype_field;
            if (ifaces != null && ifaces.length() == 1) {
                return typeToJCTree(ifaces.get(0), ast, allowCompound, allowVoid);
            }
            if (supertype != null)
                return typeToJCTree(supertype, ast, allowCompound, allowVoid);
        }
        throw new TypeNotConvertibleException("Anonymous inner class");
    }
    if (type instanceof CapturedType || type instanceof WildcardType) {
        Type lower, upper;
        if (type instanceof WildcardType) {
            upper = ((WildcardType) type).getExtendsBound();
            lower = ((WildcardType) type).getSuperBound();
        } else {
            lower = type.getLowerBound();
            upper = type.getUpperBound();
        }
        if (allowCompound) {
            if (lower == null || CTC_BOT.equals(typeTag(lower))) {
                if (upper == null || upper.toString().equals("java.lang.Object")) {
                    return maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null);
                }
                if (upper.getTypeArguments().contains(type)) {
                    return maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null);
                }
                return maker.Wildcard(maker.TypeBoundKind(BoundKind.EXTENDS), typeToJCTree(upper, ast, false, false));
            } else {
                return maker.Wildcard(maker.TypeBoundKind(BoundKind.SUPER), typeToJCTree(lower, ast, false, false));
            }
        }
        if (upper != null) {
            if (upper.getTypeArguments().contains(type)) {
                return maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null);
            }
            return typeToJCTree(upper, ast, allowCompound, allowVoid);
        }
        return createJavaLangObject(ast);
    }
    String qName;
    if (symbol.isLocal()) {
        qName = symbol.getSimpleName().toString();
    } else if (symbol.type != null && symbol.type.getEnclosingType() != null && typeTag(symbol.type.getEnclosingType()).equals(typeTag("CLASS"))) {
        replacement = typeToJCTree0(type.getEnclosingType(), ast, false, false);
        qName = symbol.getSimpleName().toString();
    } else {
        qName = symbol.getQualifiedName().toString();
    }
    if (qName.isEmpty())
        throw new TypeNotConvertibleException("unknown type");
    if (qName.startsWith("<"))
        throw new TypeNotConvertibleException(qName);
    String[] baseNames = qName.split("\\.");
    int i = 0;
    if (replacement == null) {
        replacement = maker.Ident(ast.toName(baseNames[0]));
        i = 1;
    }
    for (; i < baseNames.length; i++) {
        replacement = maker.Select(replacement, ast.toName(baseNames[i]));
    }
    return genericsToJCTreeNodes(generics, ast, replacement);
}
Also used : ClassType(com.sun.tools.javac.code.Type.ClassType) CapturedType(com.sun.tools.javac.code.Type.CapturedType) ArrayType(com.sun.tools.javac.code.Type.ArrayType) WildcardType(com.sun.tools.javac.code.Type.WildcardType) Type(com.sun.tools.javac.code.Type) WildcardType(com.sun.tools.javac.code.Type.WildcardType) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) CapturedType(com.sun.tools.javac.code.Type.CapturedType) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) ClassType(com.sun.tools.javac.code.Type.ClassType)

Example 20 with ClassType

use of com.sun.tools.javac.code.Type.ClassType in project lombok by rzwitserloot.

the class HandleDelegate method addMethodBindings.

public void addMethodBindings(List<MethodSig> signatures, ClassType ct, JavacTypes types, Set<String> banList) throws DelegateRecursion {
    TypeSymbol tsym = ct.asElement();
    if (tsym == null)
        return;
    for (Symbol member : tsym.getEnclosedElements()) {
        for (Compound am : member.getAnnotationMirrors()) {
            String name = null;
            try {
                name = am.type.tsym.flatName().toString();
            } catch (Exception ignore) {
            }
            if ("lombok.Delegate".equals(name) || "lombok.experimental.Delegate".equals(name)) {
                throw new DelegateRecursion(ct.tsym.name.toString(), member.name.toString());
            }
        }
        if (member.getKind() != ElementKind.METHOD)
            continue;
        if (member.isStatic())
            continue;
        if (member.isConstructor())
            continue;
        ExecutableElement exElem = (ExecutableElement) member;
        if (!exElem.getModifiers().contains(Modifier.PUBLIC))
            continue;
        ExecutableType methodType = (ExecutableType) types.asMemberOf(ct, member);
        String sig = printSig(methodType, member.name, types);
        // If add returns false, it was already in there
        if (!banList.add(sig))
            continue;
        boolean isDeprecated = (member.flags() & DEPRECATED) != 0;
        signatures.add(new MethodSig(member.name, methodType, isDeprecated, exElem));
    }
    if (ct.supertype_field instanceof ClassType)
        addMethodBindings(signatures, (ClassType) ct.supertype_field, types, banList);
    if (ct.interfaces_field != null)
        for (Type iface : ct.interfaces_field) {
            if (iface instanceof ClassType)
                addMethodBindings(signatures, (ClassType) iface, types, banList);
        }
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) ClassType(com.sun.tools.javac.code.Type.ClassType) Type(com.sun.tools.javac.code.Type) ExecutableType(javax.lang.model.type.ExecutableType) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) Symbol(com.sun.tools.javac.code.Symbol) ExecutableElement(javax.lang.model.element.ExecutableElement) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) Compound(com.sun.tools.javac.code.Attribute.Compound) ClassType(com.sun.tools.javac.code.Type.ClassType) TypeNotConvertibleException(lombok.javac.JavacResolution.TypeNotConvertibleException)

Aggregations

ClassType (com.sun.tools.javac.code.Type.ClassType)34 Type (com.sun.tools.javac.code.Type)21 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)10 ArrayType (com.sun.tools.javac.code.Type.ArrayType)10 Symbol (com.sun.tools.javac.code.Symbol)9 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)9 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)9 WildcardType (com.sun.tools.javac.code.Type.WildcardType)9 PackageSymbol (com.sun.tools.javac.code.Symbol.PackageSymbol)8 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)7 MethodType (com.sun.tools.javac.code.Type.MethodType)7 UnionClassType (com.sun.tools.javac.code.Type.UnionClassType)7 ArrayList (java.util.ArrayList)5 ClassTree (com.sun.source.tree.ClassTree)4 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)4 Violation (com.google.errorprone.bugpatterns.threadsafety.ThreadSafety.Violation)3 MethodTree (com.sun.source.tree.MethodTree)3 NewClassTree (com.sun.source.tree.NewClassTree)3 DynamicMethodSymbol (com.sun.tools.javac.code.Symbol.DynamicMethodSymbol)3 AttrContext (com.sun.tools.javac.comp.AttrContext)3