Search in sources :

Example 11 with ClassType

use of com.sun.tools.javac.code.Type.ClassType in project ceylon-compiler by ceylon.

the class Attr method selectSym.

private Symbol selectSym(JCFieldAccess tree, Symbol location, Type site, Env<AttrContext> env, Type pt, int pkind) {
    DiagnosticPosition pos = tree.pos();
    Name name = tree.name;
    switch(site.tag) {
        case PACKAGE:
            return rs.access(rs.findIdentInPackage(env, site.tsym, name, pkind), pos, location, site, name, true);
        case ARRAY:
        case CLASS:
            if (pt.tag == METHOD || pt.tag == FORALL) {
                return rs.resolveQualifiedMethod(pos, env, location, site, name, pt.getParameterTypes(), pt.getTypeArguments());
            } else if (name == names._this || name == names._super) {
                return rs.resolveSelf(pos, env, site.tsym, name);
            } else if (name == names._class) {
                // In this case, we have already made sure in
                // visitSelect that qualifier expression is a type.
                Type t = syms.classType;
                List<Type> typeargs = allowGenerics ? List.of(types.erasure(site)) : List.<Type>nil();
                t = new ClassType(t.getEnclosingType(), typeargs, t.tsym);
                return new VarSymbol(STATIC | PUBLIC | FINAL, names._class, t, site.tsym);
            } else {
                // We are seeing a plain identifier as selector.
                Symbol sym = rs.findIdentInType(env, site, name, pkind);
                if ((pkind & ERRONEOUS) == 0)
                    sym = rs.access(sym, pos, location, site, name, true);
                return sym;
            }
        case WILDCARD:
            throw new AssertionError(tree);
        case TYPEVAR:
            // Normally, site.getUpperBound() shouldn't be null.
            // It should only happen during memberEnter/attribBase
            // when determining the super type which *must* beac
            // done before attributing the type variables.  In
            // other words, we are seeing this illegal program:
            // class B<T> extends A<T.foo> {}
            Symbol sym = (site.getUpperBound() != null) ? selectSym(tree, location, capture(site.getUpperBound()), env, pt, pkind) : null;
            if (sym == null) {
                log.error(pos, "type.var.cant.be.deref");
                return syms.errSymbol;
            } else {
                // Ceylon: relax the rules for private methods in wildcards, damnit, we want the private
                // method to be called, not any subtype's method we can't possibly know about, this is really
                // a lame Java decision.
                Symbol sym2 = (!sourceLanguage.isCeylon() && (sym.flags() & Flags.PRIVATE) != 0) ? rs.new AccessError(env, site, sym) : sym;
                rs.access(sym2, pos, location, site, name, true);
                return sym;
            }
        case ERROR:
            // preserve identifier names through errors
            return types.createErrorType(name, site.tsym, site).tsym;
        default:
            // .class is allowed for these.
            if (name == names._class) {
                // In this case, we have already made sure in Select that
                // qualifier expression is a type.
                Type t = syms.classType;
                Type arg = types.boxedClass(site).type;
                t = new ClassType(t.getEnclosingType(), List.of(arg), t.tsym);
                return new VarSymbol(STATIC | PUBLIC | FINAL, names._class, t, site.tsym);
            } else {
                log.error(pos, "cant.deref", site);
                return syms.errSymbol;
            }
    }
}
Also used : ClassType(com.sun.tools.javac.code.Type.ClassType) MethodType(com.sun.tools.javac.code.Type.MethodType) WildcardType(com.sun.tools.javac.code.Type.WildcardType) Type(com.sun.tools.javac.code.Type) ArrayType(com.sun.tools.javac.code.Type.ArrayType) UnionClassType(com.sun.tools.javac.code.Type.UnionClassType) DiagnosticPosition(com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition) 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) DynamicMethodSymbol(com.sun.tools.javac.code.Symbol.DynamicMethodSymbol) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) OperatorSymbol(com.sun.tools.javac.code.Symbol.OperatorSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) ClassType(com.sun.tools.javac.code.Type.ClassType) UnionClassType(com.sun.tools.javac.code.Type.UnionClassType) Kinds.kindName(com.sun.tools.javac.code.Kinds.kindName) Name(com.sun.tools.javac.util.Name)

Example 12 with ClassType

use of com.sun.tools.javac.code.Type.ClassType in project ceylon-compiler by ceylon.

the class CeylonEnter method resetClassSymbol.

/**
 * This resets a ClassSymbol recursively, for bootstrap
 */
private void resetClassSymbol(ClassSymbol classSymbol) {
    // look for inner classes
    if (classSymbol.members_field != null) {
        for (Symbol member : classSymbol.getEnclosedElements()) {
            if (member instanceof ClassSymbol) {
                resetClassSymbol((ClassSymbol) member);
            }
        }
    }
    // reset its type, we need to keep it
    com.sun.tools.javac.code.Type.ClassType classType = (ClassType) classSymbol.type;
    classType.all_interfaces_field = null;
    classType.interfaces_field = null;
    classType.supertype_field = null;
    classType.typarams_field = null;
    // reset its members and completer
    classSymbol.members_field = null;
    classSymbol.completer = null;
    classSymbol.attributes_field = List.nil();
}
Also used : ClassType(com.sun.tools.javac.code.Type.ClassType) Type(com.redhat.ceylon.model.typechecker.model.Type) ClassType(com.sun.tools.javac.code.Type.ClassType) 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 13 with ClassType

use of com.sun.tools.javac.code.Type.ClassType in project error-prone by google.

the class ImmutableAnalysis method isFieldImmutable.

/** Check a single field for immutability. */
private Violation isFieldImmutable(Optional<Tree> tree, ImmutableSet<String> immutableTyParams, ClassSymbol classSym, ClassType classType, VarSymbol var) {
    if (bugChecker.isSuppressed(var)) {
        return Violation.absent();
    }
    if (ASTHelpers.hasAnnotation(var, LazyInit.class, state)) {
        return Violation.absent();
    }
    if (!var.getModifiers().contains(Modifier.FINAL)) {
        if (tree.isPresent()) {
            // If we have a tree to attach diagnostics to, report the error immediately instead of
            // accumulating the path to the error from the top-level class being checked
            state.reportMatch(BugChecker.buildDescriptionFromChecker(tree.get(), bugChecker).setMessage(nonFinalFieldMessage).addFix(SuggestedFixes.addModifiers(tree.get(), state, Modifier.FINAL)).build());
            return Violation.absent();
        }
        return Violation.of(String.format("'%s' has non-final field '%s'", getPrettyName(classSym), var.getSimpleName()));
    }
    Type varType = state.getTypes().memberType(classType, var);
    Violation info = isImmutableType(immutableTyParams, varType);
    if (info.isPresent()) {
        if (tree.isPresent()) {
            // If we have a tree to attach diagnostics to, report the error immediately instead of
            // accumulating the path to the error from the top-level class being checked
            state.reportMatch(BugChecker.buildDescriptionFromChecker(tree.get(), bugChecker).setMessage(info.plus(mutableFieldMessage).message()).build());
            return Violation.absent();
        }
        return info.plus(String.format("'%s' has field '%s' of type '%s'", getPrettyName(classSym), var.getSimpleName(), varType));
    }
    return Violation.absent();
}
Also used : ClassType(com.sun.tools.javac.code.Type.ClassType) ArrayType(com.sun.tools.javac.code.Type.ArrayType) WildcardType(com.sun.tools.javac.code.Type.WildcardType) Type(com.sun.tools.javac.code.Type)

Example 14 with ClassType

use of com.sun.tools.javac.code.Type.ClassType in project error-prone by google.

the class ImmutableAnalysis method checkForImmutability.

/**
   * Check that an {@code @Immutable}-annotated class:
   *
   * <ul>
   *   <li>does not declare or inherit any mutable fields,
   *   <li>any immutable supertypes are instantiated with immutable type arguments as required by
   *       their containerOf spec, and
   *   <li>any enclosing instances are immutable.
   * </ul>
   *
   * requiring supertypes to be annotated immutable would be too restrictive.
   */
Violation checkForImmutability(Optional<ClassTree> tree, ImmutableSet<String> immutableTyParams, ClassType type) {
    Violation info = areFieldsImmutable(tree, immutableTyParams, type);
    if (info.isPresent()) {
        return info;
    }
    for (Type interfaceType : state.getTypes().interfaces(type)) {
        ImmutableAnnotationInfo interfaceAnnotation = getImmutableAnnotation(interfaceType.tsym);
        if (interfaceAnnotation == null) {
            continue;
        }
        info = immutableInstantiation(immutableTyParams, interfaceAnnotation, interfaceType);
        if (info.isPresent()) {
            return info.plus(String.format("'%s' extends '%s'", getPrettyName(type.tsym), getPrettyName(interfaceType.tsym)));
        }
    }
    info = checkSuper(immutableTyParams, type);
    if (info.isPresent()) {
        return info;
    }
    Type mutableEnclosing = mutableEnclosingInstance(tree, type);
    if (mutableEnclosing != null) {
        return info.plus(String.format("'%s' has mutable enclosing instance '%s'", getPrettyName(type.tsym), mutableEnclosing));
    }
    return Violation.absent();
}
Also used : ClassType(com.sun.tools.javac.code.Type.ClassType) ArrayType(com.sun.tools.javac.code.Type.ArrayType) WildcardType(com.sun.tools.javac.code.Type.WildcardType) Type(com.sun.tools.javac.code.Type)

Example 15 with ClassType

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

the class HandleExtensionMethod method getExtensions.

public List<Extension> getExtensions(final JavacNode typeNode, final List<Object> extensionProviders) {
    List<Extension> extensions = new ArrayList<Extension>();
    for (Object extensionProvider : extensionProviders) {
        if (!(extensionProvider instanceof JCFieldAccess))
            continue;
        JCFieldAccess provider = (JCFieldAccess) extensionProvider;
        if (!("class".equals(provider.name.toString())))
            continue;
        Type providerType = CLASS.resolveMember(typeNode, provider.selected);
        if (providerType == null)
            continue;
        if ((providerType.tsym.flags() & (INTERFACE | ANNOTATION)) != 0)
            continue;
        extensions.add(getExtension(typeNode, (ClassType) providerType));
    }
    return extensions;
}
Also used : ClassType(com.sun.tools.javac.code.Type.ClassType) MethodType(com.sun.tools.javac.code.Type.MethodType) ErrorType(com.sun.tools.javac.code.Type.ErrorType) Type(com.sun.tools.javac.code.Type) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) ArrayList(java.util.ArrayList) ClassType(com.sun.tools.javac.code.Type.ClassType)

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