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();
}
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;
}
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));
}
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);
}
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);
}
}
Aggregations