use of com.sun.tools.javac.code.Symbol.TypeSymbol in project lombok by rzwitserloot.
the class HandleExtensionMethod method getExtension.
public Extension getExtension(final JavacNode typeNode, final ClassType extensionMethodProviderType) {
List<MethodSymbol> extensionMethods = new ArrayList<MethodSymbol>();
TypeSymbol tsym = extensionMethodProviderType.asElement();
if (tsym != null)
for (Symbol member : tsym.getEnclosedElements()) {
if (member.getKind() != ElementKind.METHOD)
continue;
MethodSymbol method = (MethodSymbol) member;
if ((method.flags() & (STATIC | PUBLIC)) == 0)
continue;
if (method.params().isEmpty())
continue;
extensionMethods.add(method);
}
return new Extension(extensionMethods, tsym);
}
use of com.sun.tools.javac.code.Symbol.TypeSymbol in project error-prone by google.
the class ThreadSafety method getAnnotation.
private AnnotationInfo getAnnotation(Symbol sym, VisitorState state, String annotation, @Nullable Class<? extends Annotation> elementAnnotation) {
if (sym == null) {
return null;
}
Symbol annosym = state.getSymbolFromString(annotation);
Optional<Compound> attr = sym.getAnnotationMirrors().stream().filter(a -> a.getAnnotationType().asElement().equals(annosym)).findAny();
if (attr.isPresent()) {
ImmutableList<String> containerElements = containerOf(state, attr.get());
if (elementAnnotation != null && containerElements.isEmpty()) {
containerElements = sym.getTypeParameters().stream().filter(p -> p.getAnnotation(elementAnnotation) != null).map(p -> p.getSimpleName().toString()).collect(toImmutableList());
}
return AnnotationInfo.create(sym.getQualifiedName().toString(), containerElements);
}
// @ThreadSafe is inherited from supertypes
if (!(sym instanceof ClassSymbol)) {
return null;
}
Type superClass = ((ClassSymbol) sym).getSuperclass();
AnnotationInfo superAnnotation = getInheritedAnnotation(superClass.asElement(), state);
if (superAnnotation == null) {
return null;
}
// If an annotated super-type was found, look for any type arguments to the super-type that
// are in the super-type's containerOf spec, and where the arguments are type parameters
// of the current class.
// E.g. for `Foo<X> extends Super<X>` if `Super<Y>` is annotated
// `@ThreadSafeContainerAnnotation Y`
// then `Foo<X>` is has X implicitly annotated `@ThreadSafeContainerAnnotation X`
ImmutableList.Builder<String> containerOf = ImmutableList.builder();
for (int i = 0; i < superClass.getTypeArguments().size(); i++) {
Type arg = superClass.getTypeArguments().get(i);
TypeVariableSymbol formal = superClass.asElement().getTypeParameters().get(i);
if (!arg.hasTag(TypeTag.TYPEVAR)) {
continue;
}
TypeSymbol argSym = arg.asElement();
if (argSym.owner == sym && superAnnotation.containerOf().contains(formal.getSimpleName().toString())) {
containerOf.add(argSym.getSimpleName().toString());
}
}
return AnnotationInfo.create(superAnnotation.typeName(), containerOf.build());
}
use of com.sun.tools.javac.code.Symbol.TypeSymbol in project error-prone by google.
the class ASTHelpers method findMatchingMethods.
/**
* Finds all methods in any superclass of {@code startClass} with a certain {@code name} that
* match the given {@code predicate}.
*
* @return The (possibly empty) set of methods in any superclass that match {@code predicate} and
* have the given {@code name}.
*/
public static Set<MethodSymbol> findMatchingMethods(Name name, final Predicate<MethodSymbol> predicate, Type startClass, Types types) {
Filter<Symbol> matchesMethodPredicate = sym -> sym instanceof MethodSymbol && predicate.apply((MethodSymbol) sym);
Set<MethodSymbol> matchingMethods = new HashSet<>();
// Iterate over all classes and interfaces that startClass inherits from.
for (Type superClass : types.closure(startClass)) {
// Iterate over all the methods declared in superClass.
TypeSymbol superClassSymbol = superClass.tsym;
Scope superClassSymbols = superClassSymbol.members();
if (superClassSymbols != null) {
// Can be null if superClass is a type variable
for (Symbol symbol : superClassSymbols.getSymbolsByName(name, matchesMethodPredicate, NON_RECURSIVE)) {
// By definition of the filter, we know that the symbol is a MethodSymbol.
matchingMethods.add((MethodSymbol) symbol);
}
}
}
return matchingMethods;
}
use of com.sun.tools.javac.code.Symbol.TypeSymbol 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(), /* checkResult= */
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.TypeSymbol in project error-prone by google.
the class ForOverrideChecker method matchMethodInvocation.
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
MethodSymbol method = ASTHelpers.getSymbol(tree);
if (method == null) {
return Description.NO_MATCH;
}
Type currentClass = getOutermostClass(state);
if (method.isStatic() || method.isConstructor() || currentClass == null) {
return Description.NO_MATCH;
}
// allow super.foo() calls to @ForOverride methods from overriding methods
if (isSuperCall(currentClass, tree, state)) {
MethodTree currentMethod = findDirectMethod(state.getPath());
// currentMethod might be null if we are in a field initializer
if (currentMethod != null) {
// MethodSymbol.overrides doesn't check that names match, so we need to do that first.
if (currentMethod.getName().equals(method.name)) {
MethodSymbol currentMethodSymbol = ASTHelpers.getSymbol(currentMethod);
if (currentMethodSymbol.overrides(method, (TypeSymbol) method.owner, state.getTypes(), /* checkResult= */
true)) {
return Description.NO_MATCH;
}
}
}
}
List<MethodSymbol> overriddenMethods = getOverriddenMethods(state, method);
for (Symbol overriddenMethod : overriddenMethods) {
Type declaringClass = overriddenMethod.outermostClass().asType();
if (!declaringClass.equals(currentClass)) {
String customMessage = MESSAGE_BASE + "must not be invoked directly " + "(except by the declaring class, " + declaringClass + ")";
return buildDescription(tree).setMessage(customMessage).build();
}
}
return Description.NO_MATCH;
}
Aggregations