Search in sources :

Example 11 with ClassSymbol

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

the class ImmutableEnumChecker method matchClass.

@Override
public Description matchClass(ClassTree tree, VisitorState state) {
    ClassSymbol symbol = getSymbol(tree);
    if (symbol == null || !symbol.isEnum()) {
        return NO_MATCH;
    }
    if (ASTHelpers.hasAnnotation(symbol, Immutable.class, state) && !implementsImmutableInterface(symbol)) {
        AnnotationTree annotation = ASTHelpers.getAnnotationWithSimpleName(tree.getModifiers().getAnnotations(), "Immutable");
        if (annotation != null) {
            state.reportMatch(buildDescription(annotation).setMessage(ANNOTATED_ENUM_MESSAGE).addFix(SuggestedFix.delete(annotation)).build());
        } else {
            state.reportMatch(buildDescription(tree).setMessage(ANNOTATED_ENUM_MESSAGE).build());
        }
    }
    Violation info = new ImmutableAnalysis(this, state, "enums should be immutable, and cannot have non-final fields", "enums should only have immutable fields").checkForImmutability(Optional.of(tree), ImmutableSet.of(), getType(tree));
    if (!info.isPresent()) {
        return NO_MATCH;
    }
    String message = "enums should be immutable: " + info.message();
    return buildDescription(tree).setMessage(message).build();
}
Also used : Immutable(com.google.errorprone.annotations.Immutable) Violation(com.google.errorprone.bugpatterns.threadsafety.ImmutableAnalysis.Violation) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) AnnotationTree(com.sun.source.tree.AnnotationTree)

Example 12 with ClassSymbol

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

the class WrongParameterPackage method matchMethod.

@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
    MethodSymbol method = ASTHelpers.getSymbol(tree);
    if (method == null) {
        return Description.NO_MATCH;
    }
    ClassSymbol classSym = method.enclClass();
    if (classSym == null) {
        return Description.NO_MATCH;
    }
    TypeSymbol superClass = classSym.getSuperclass().tsym;
    if (superClass == null) {
        return Description.NO_MATCH;
    }
    for (Symbol s : superClass.members().getSymbols()) {
        if (s.name.contentEquals(method.name) && s.getKind() == ElementKind.METHOD) {
            MethodSymbol supermethod = (MethodSymbol) s;
            // if this method actually overrides the supermethod, then it's correct and not a match.
            if (method.overrides(supermethod, superClass, state.getTypes(), true)) {
                return Description.NO_MATCH;
            }
            // if this doesn't have the right number of parameters, look at other ones.
            if (supermethod.params().size() != method.params().size()) {
                continue;
            }
            for (int x = 0; x < method.params().size(); x++) {
                Type methodParamType = method.params().get(x).type;
                Type supermethodParamType = supermethod.params().get(x).type;
                if (methodParamType.tsym.name.contentEquals(supermethodParamType.tsym.name) && !state.getTypes().isSameType(methodParamType, supermethodParamType)) {
                    this.supermethod = supermethod;
                    return describe(tree, state);
                }
            }
        }
    }
    return Description.NO_MATCH;
}
Also used : Type(com.sun.tools.javac.code.Type) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) Symbol(com.sun.tools.javac.code.Symbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol)

Example 13 with ClassSymbol

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

the class RefasterRuleBuilderScanner method extractRules.

public static Collection<? extends CodeTransformer> extractRules(ClassTree tree, Context context) {
    ClassSymbol sym = ASTHelpers.getSymbol(tree);
    RefasterRuleBuilderScanner scanner = new RefasterRuleBuilderScanner(context);
    // visit abstract methods first
    List<MethodTree> methods = new Ordering<MethodTree>() {

        @Override
        public int compare(MethodTree l, MethodTree r) {
            return Boolean.compare(l.getModifiers().getFlags().contains(Modifier.ABSTRACT), r.getModifiers().getFlags().contains(Modifier.ABSTRACT));
        }
    }.reverse().immutableSortedCopy(Iterables.filter(tree.getMembers(), MethodTree.class));
    scanner.visit(methods, null);
    UTemplater templater = new UTemplater(context);
    List<UType> types = templater.templateTypes(sym.type.getTypeArguments());
    return scanner.createMatchers(Iterables.filter(types, UTypeVar.class), sym.getQualifiedName().toString(), UTemplater.annotationMap(sym));
}
Also used : MethodTree(com.sun.source.tree.MethodTree) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Ordering(com.google.common.collect.Ordering)

Example 14 with ClassSymbol

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

the class UClassType method inline.

@Override
public ClassType inline(Inliner inliner) throws CouldNotResolveImportException {
    ClassSymbol classSymbol = inliner.resolveClass(fullyQualifiedClass());
    boolean isNonStaticInnerClass = classSymbol.owner instanceof ClassSymbol && (classSymbol.flags() & STATIC) == 0;
    Type owner = isNonStaticInnerClass ? classSymbol.owner.type : Type.noType;
    return new ClassType(owner, inliner.<Type>inlineList(typeArguments()), classSymbol);
}
Also used : ClassType(com.sun.tools.javac.code.Type.ClassType) Type(com.sun.tools.javac.code.Type) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) ClassType(com.sun.tools.javac.code.Type.ClassType)

Example 15 with ClassSymbol

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

the class Attr method attribClassBody.

/** Finish the attribution of a class. */
private void attribClassBody(Env<AttrContext> env, ClassSymbol c) {
    JCClassDecl tree = (JCClassDecl) env.tree;
    Assert.check(c == tree.sym);
    // Validate annotations
    chk.validateAnnotations(tree.mods.annotations, c);
    // Validate type parameters, supertype and interfaces.
    attribBounds(tree.typarams);
    if (!c.isAnonymous()) {
        //already checked if anonymous
        chk.validate(tree.typarams, env);
        chk.validate(tree.extending, env);
        chk.validate(tree.implementing, env);
    }
    // methods or unimplemented methods of an implemented interface.
    if ((c.flags() & (ABSTRACT | INTERFACE)) == 0) {
        if (!relax)
            chk.checkAllDefined(tree.pos(), c);
    }
    if ((c.flags() & ANNOTATION) != 0) {
        if (tree.implementing.nonEmpty())
            log.error(tree.implementing.head.pos(), "cant.extend.intf.annotation");
        if (tree.typarams.nonEmpty())
            log.error(tree.typarams.head.pos(), "intf.annotation.cant.have.type.params");
    } else {
        // Check that all extended classes and interfaces
        // are compatible (i.e. no two define methods with same arguments
        // yet different return types).  (JLS 8.4.6.3)
        chk.checkCompatibleSupertypes(tree.pos(), c.type);
    }
    // Check that class does not import the same parameterized interface
    // with two different argument lists.
    chk.checkClassBounds(tree.pos(), c.type);
    tree.type = c.type;
    for (List<JCTypeParameter> l = tree.typarams; l.nonEmpty(); l = l.tail) {
        Assert.checkNonNull(env.info.scope.lookup(l.head.name).scope);
    }
    // Check that a generic class doesn't extend Throwable
    if (!sourceLanguage.isCeylon() && !c.type.allparams().isEmpty() && types.isSubtype(c.type, syms.throwableType))
        log.error(tree.extending.pos(), "generic.throwable");
    // Check that all methods which implement some
    // method conform to the method they implement.
    chk.checkImplementations(tree);
    //check that a resource implementing AutoCloseable cannot throw InterruptedException
    checkAutoCloseable(tree.pos(), env, c.type);
    for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
        // Attribute declaration
        attribStat(l.head, env);
        // Make an exception for static constants.
        if (c.owner.kind != PCK && ((c.flags() & STATIC) == 0 || c.name == names.empty) && (TreeInfo.flags(l.head) & (STATIC | INTERFACE)) != 0) {
            Symbol sym = null;
            if (l.head.getTag() == JCTree.VARDEF)
                sym = ((JCVariableDecl) l.head).sym;
            if (sym == null || sym.kind != VAR || ((VarSymbol) sym).getConstValue() == null)
                log.error(l.head.pos(), "icls.cant.have.static.decl", c);
        }
    }
    // Check for cycles among non-initial constructors.
    chk.checkCyclicConstructors(tree);
    // Check for cycles among annotation elements.
    chk.checkNonCyclicElements(tree);
    // Check for proper use of serialVersionUID
    if (env.info.lint.isEnabled(LintCategory.SERIAL) && isSerializable(c) && (c.flags() & Flags.ENUM) == 0 && (c.flags() & ABSTRACT) == 0) {
        checkSerialVersionUID(tree, c);
    }
}
Also used : JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) 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) JCTree(com.sun.tools.javac.tree.JCTree) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Aggregations

ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)49 Symbol (com.sun.tools.javac.code.Symbol)20 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)20 Type (com.sun.tools.javac.code.Type)17 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)10 PackageSymbol (com.sun.tools.javac.code.Symbol.PackageSymbol)9 ClassType (com.sun.tools.javac.code.Type.ClassType)9 ClassTree (com.sun.source.tree.ClassTree)8 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)8 MethodTree (com.sun.source.tree.MethodTree)6 Tree (com.sun.source.tree.Tree)6 ArrayType (com.sun.tools.javac.code.Type.ArrayType)5 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)4 VariableTree (com.sun.source.tree.VariableTree)4 Name (com.sun.tools.javac.util.Name)4 ArrayList (java.util.ArrayList)4 Violation (com.google.errorprone.bugpatterns.threadsafety.ImmutableAnalysis.Violation)3 BlockTree (com.sun.source.tree.BlockTree)3 EnhancedForLoopTree (com.sun.source.tree.EnhancedForLoopTree)3 ForLoopTree (com.sun.source.tree.ForLoopTree)3