private static MethodSymbol getMatchingMethod( Type type, Name name, Predicate<MethodSymbol> predicate) { Scope scope = type.tsym.members(); for (Symbol sym : scope.getSymbolsByName(name)) { if (!(sym instanceof MethodSymbol)) { continue; } MethodSymbol methodSymbol = (MethodSymbol) sym; if (predicate.apply(methodSymbol)) { return methodSymbol; } } return null; } }
/** * 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; }
@SuppressWarnings("TypeEquals") @Nullable public static MethodSymbol findSuperMethodInType( MethodSymbol methodSymbol, Type superType, Types types) { if (methodSymbol.isStatic() || superType.equals(methodSymbol.owner.type)) { return null; } Scope scope = superType.tsym.members(); for (Symbol sym : scope.getSymbolsByName(methodSymbol.name)) { if (sym != null && !sym.isStatic() && ((sym.flags() & Flags.SYNTHETIC) == 0) && methodSymbol.overrides( sym, (TypeSymbol) methodSymbol.owner, types, /* checkResult= */ true)) { return (MethodSymbol) sym; } } return null; }
private <T extends Symbol> T getMember( Class<T> type, ElementKind kind, Symbol classSymbol, String name) { if (classSymbol.type == null) { return null; } for (Type t : types.closure(classSymbol.type)) { Scope scope = t.tsym.members(); for (Symbol sym : scope.getSymbolsByName(getName(name))) { if (sym.getKind().equals(kind)) { return type.cast(sym); } } } if (classSymbol.hasOuterInstance()) { T sym = getMember(type, kind, classSymbol.type.getEnclosingType().asElement(), name); if (sym != null) { return sym; } } if (classSymbol.owner != null && classSymbol != classSymbol.owner && classSymbol.owner instanceof Symbol.ClassSymbol) { T sym = getMember(type, kind, classSymbol.owner, name); if (sym != null && sym.isStatic()) { return sym; } } return null; }
/** Check if the method declares or inherits an implementation of .equals() */ public static boolean implementsEquals(Type type, VisitorState state) { Name equalsName = state.getName("equals"); Symbol objectEquals = getOnlyMember(state, state.getSymtab().objectType, "equals"); for (Type sup : state.getTypes().closure(type)) { if (sup.tsym.isInterface()) { continue; } if (ASTHelpers.isSameType(sup, state.getSymtab().objectType, state)) { return false; } Scope scope = sup.tsym.members(); if (scope == null) { continue; } for (Symbol sym : scope.getSymbolsByName(equalsName)) { if (sym.overrides(objectEquals, type.tsym, state.getTypes(), /* checkResult= */ false)) { return true; } } } return false; } }
private static MethodSymbol getMatchingMethod( Type type, Name name, Predicate<MethodSymbol> predicate) { Scope scope = type.tsym.members(); for (Symbol sym : scope.getSymbolsByName(name)) { if (!(sym instanceof MethodSymbol)) { continue; } MethodSymbol methodSymbol = (MethodSymbol) sym; if (predicate.apply(methodSymbol)) { return methodSymbol; } } return null; } }
/** * 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; }
public boolean alreadyDefinedIn(CharSequence name, ExecutableType method, TypeElement enclClass) { Type.MethodType meth = ((Type)method).asMethodType(); ClassSymbol clazz = (ClassSymbol)enclClass; Scope scope = clazz.members(); Name n = names.fromString(name.toString()); for (Symbol sym : scope.getSymbolsByName(n, Scope.LookupKind.NON_RECURSIVE)) { if(sym.type instanceof ExecutableType && types.isSubsignature(meth, (ExecutableType)sym.type)) return true; } return false; }
@Nullable public static MethodSymbol findSuperMethodInType( MethodSymbol methodSymbol, Type superType, Types types) { if (methodSymbol.isStatic() || superType.equals(methodSymbol.owner.type)) { return null; } Scope scope = superType.tsym.members(); for (Symbol sym : scope.getSymbolsByName(methodSymbol.name)) { if (sym != null && !sym.isStatic() && ((sym.flags() & Flags.SYNTHETIC) == 0) && sym.name.contentEquals(methodSymbol.name) && methodSymbol.overrides( sym, (TypeSymbol) methodSymbol.owner, types, /* checkResult= */ true)) { return (MethodSymbol) sym; } } return null; }
public boolean alreadyDefinedIn(CharSequence name, TypeMirror returnType, List<TypeMirror> paramTypes, TypeElement enclClass) { ClassSymbol clazz = (ClassSymbol)enclClass; Scope scope = clazz.members(); Name n = names.fromString(name.toString()); ListBuffer<Type> buff = new ListBuffer<>(); for (TypeMirror tm : paramTypes) { buff.append((Type)tm); } for (Symbol sym : scope.getSymbolsByName(n, Scope.LookupKind.NON_RECURSIVE)) { if(sym.type instanceof ExecutableType && jctypes.containsTypeEquivalent(sym.type.asMethodType().getParameterTypes(), buff.toList()) && jctypes.isSameType(sym.type.asMethodType().getReturnType(), (Type)returnType)) return true; } return false; }
private <T extends Symbol> T getMember( Class<T> type, ElementKind kind, Symbol classSymbol, String name) { if (classSymbol.type == null) { return null; } for (Type t : types.closure(classSymbol.type)) { Scope scope = t.tsym.members(); for (Symbol sym : scope.getSymbolsByName(getName(name))) { if (sym.getKind().equals(kind)) { return type.cast(sym); } } } if (classSymbol.hasOuterInstance()) { T sym = getMember(type, kind, classSymbol.type.getEnclosingType().asElement(), name); if (sym != null) { return sym; } } if (classSymbol.owner != null && classSymbol != classSymbol.owner && classSymbol.owner instanceof Symbol.ClassSymbol) { T sym = getMember(type, kind, classSymbol.owner, name); if (sym != null && sym.isStatic()) { return sym; } } return null; }
/** Check if the method declares or inherits an implementation of .equals() */ public static boolean implementsEquals(Type type, VisitorState state) { Name equalsName = state.getName("equals"); Symbol objectEquals = getOnlyMember(state, state.getSymtab().objectType, "equals"); for (Type sup : state.getTypes().closure(type)) { if (sup.tsym.isInterface()) { continue; } if (ASTHelpers.isSameType(sup, state.getSymtab().objectType, state)) { return false; } Scope scope = sup.tsym.members(); if (scope == null) { continue; } for (Symbol sym : scope.getSymbolsByName(equalsName)) { if (sym.overrides(objectEquals, type.tsym, state.getTypes(), /* checkResult= */ false)) { return true; } } } return false; } }
for (Symbol sym : s.getSymbolsByName(tree.name)) { if (sym != tree.sym && types.isSameType(types.erasure(sym.type), type) && !sym.type.isErroneous() && !type.isErroneous()) {