while (!Objects.equals(classSymbol.getSuperclass(), Type.noType)) { TypeSymbol parentSymbol = classSymbol.getSuperclass().asElement(); List<Symbol> parentElements = parentSymbol.getEnclosedElements();
private static boolean implementsImmutableInterface(ClassSymbol symbol) { return Streams.concat(symbol.getInterfaces().stream(), Stream.of(symbol.getSuperclass())) .anyMatch(supertype -> supertype.asElement().getAnnotation(Immutable.class) != null); } }
return null; Type superClass = ((ClassSymbol) sym).getSuperclass(); AnnotationInfo superAnnotation = getInheritedAnnotation(superClass.asElement(), state); if (superAnnotation == null) {
if (typeSymbol instanceof ClassSymbol && ((ClassSymbol) typeSymbol).getSuperclass() != null) { if (!overrides || !Object.class.getName().equals(((ClassSymbol) typeSymbol).getSuperclass().toString())) { collectMatchingMethodDeclarationsInType(types, ((ClassSymbol) typeSymbol).getSuperclass().tsym, methodName, methodType, overrides, collector);
if (typeSymbol instanceof ClassSymbol && ((ClassSymbol) typeSymbol).getSuperclass() != null) { if (!scanMemberDeclarationsInType(((ClassSymbol) typeSymbol).getSuperclass().tsym, ignoredTypeNames, scanner)) { return false;
/** * Finds methods by name. */ public static void findMethodDeclarationsInType(TypeSymbol typeSymbol, Collection<String> methodNames, Set<String> ignoredTypeNames, List<MethodSymbol> result) { if (typeSymbol == null) { return; } if (ignoredTypeNames.contains(typeSymbol.getQualifiedName().toString())) { return; } if (typeSymbol.getEnclosedElements() != null) { for (Element element : typeSymbol.getEnclosedElements()) { if ((element instanceof MethodSymbol) && (methodNames.contains(element.getSimpleName().toString()))) { result.add((MethodSymbol) element); } } } if (typeSymbol instanceof ClassSymbol && ((ClassSymbol) typeSymbol).getSuperclass() != null) { findMethodDeclarationsInType(((ClassSymbol) typeSymbol).getSuperclass().tsym, methodNames, ignoredTypeNames, result); } if (result == null) { if (typeSymbol instanceof ClassSymbol && ((ClassSymbol) typeSymbol).getInterfaces() != null) { for (Type t : ((ClassSymbol) typeSymbol).getInterfaces()) { findMethodDeclarationsInType(t.tsym, methodNames, ignoredTypeNames, result); } } } }
/** * Recursively looks up one of the given types in the type hierachy of the given * class. * * @return true if one of the given names is found as a superclass or a * superinterface */ public static boolean hasParent(ClassSymbol clazz, String... qualifiedNamesToFind) { if (clazz == null) { return false; } if (ArrayUtils.contains(qualifiedNamesToFind, clazz.getQualifiedName().toString())) { return true; } if (hasParent((ClassSymbol) clazz.getSuperclass().tsym, qualifiedNamesToFind)) { return true; } for (Type t : clazz.getInterfaces()) { if (hasParent((ClassSymbol) t.tsym, qualifiedNamesToFind)) { return true; } } return false; }
/** * Looks up a class in the given class hierarchy and returns true if found. */ public static boolean isParent(TypeSymbol type, TypeSymbol toFind) { if (!(type instanceof ClassSymbol)) { return false; } ClassSymbol clazz = (ClassSymbol) type; if (clazz.equals(toFind)) { return true; } if (isParent((ClassSymbol) clazz.getSuperclass().tsym, toFind)) { return true; } for (Type t : clazz.getInterfaces()) { if (isParent((ClassSymbol) t.tsym, toFind)) { return true; } } return false; }
public void grabMethodsToBeImplemented(List<MethodSymbol> methods, TypeSymbol type) { if (type == null) { return; } if (isInterface(type)) { for (Symbol s : type.getEnclosedElements()) { if (s instanceof MethodSymbol) { if (!s.isStatic() && !Util.isOverridingBuiltInJavaObjectMethod((MethodSymbol) s)) { methods.add((MethodSymbol) s); } } } } if (type instanceof ClassSymbol) { for (Type t : ((ClassSymbol) type).getInterfaces()) { grabMethodsToBeImplemented(methods, t.tsym); } grabMethodsToBeImplemented(methods, ((ClassSymbol) type).getSuperclass().tsym); } }
/** * Finds the field in the given type that matches the given name. */ public static VarSymbol findFieldDeclaration(ClassSymbol classSymbol, Name name) { if (classSymbol == null) { return null; } Iterator<Symbol> it = classSymbol.members_field.getElementsByName(name, (symbol) -> { return symbol instanceof VarSymbol; }).iterator(); if (it.hasNext()) { return (VarSymbol) it.next(); } else { if (classSymbol.getSuperclass().tsym instanceof ClassSymbol) { return findFieldDeclaration((ClassSymbol) classSymbol.getSuperclass().tsym, name); } else { return null; } } }
/** * Finds the first inner class declaration of the given name in the given class * hierarchy. */ public static ClassSymbol findInnerClassDeclaration(ClassSymbol clazz, String name) { if (clazz == null) { return null; } for (Symbol s : clazz.getEnclosedElements()) { if (s instanceof ClassSymbol && s.getSimpleName().toString().equals(name)) { return (ClassSymbol) s; } } if (clazz.getSuperclass() != null) { return findInnerClassDeclaration((ClassSymbol) clazz.getSuperclass().tsym, name); } return null; }
/** * Grabs the names of all the support interfaces in the class and interface * hierarchy. */ public void grabSupportedInterfaceNames(Set<String> interfaces, TypeSymbol type) { if (type == null) { return; } if (isInterface(type)) { interfaces.add(type.getQualifiedName().toString()); } if (type instanceof ClassSymbol) { for (Type t : ((ClassSymbol) type).getInterfaces()) { grabSupportedInterfaceNames(interfaces, t.tsym); } grabSupportedInterfaceNames(interfaces, ((ClassSymbol) type).getSuperclass().tsym); } }
private void inspectSuperTypes(ClassSymbol clazz, Overload overload, JCMethodDecl method) { if (clazz == null) { return; } Overload superOverload = context.getOverload(clazz, method.sym); if (superOverload != null && superOverload != overload) { superOverload.merge(types, overload); } inspectSuperTypes((ClassSymbol) clazz.getSuperclass().tsym, overload, method); for (Type t : clazz.getInterfaces()) { inspectSuperTypes((ClassSymbol) t.tsym, overload, method); } }
private static int indexOfSuperclass(List<ClassSymbol> symbols, ClassSymbol clazz) { int superClassIndex = symbols.indexOf(clazz.getSuperclass().tsym); // looks up also if any inner class extends a class in the list if (superClassIndex < 0) { for (Symbol s : clazz.getEnclosedElements()) { if (s instanceof ClassSymbol) { return indexOfSuperclass(symbols, ((ClassSymbol) s)); } } } return superClassIndex; }
/** * Grabs the names of all the superclasses. */ public void grabSuperClassNames(Set<String> superClasses, Element type) { if (type == null) { return; } if (type instanceof ClassSymbol) { superClasses.add(((ClassSymbol) type).getQualifiedName().toString()); grabSuperClassNames(superClasses, ((ClassSymbol) type).getSuperclass().tsym); } }
ClassSymbol directSuperClass = (ClassSymbol) enclosingClass.getSuperclass().tsym;
return Description.NO_MATCH; TypeSymbol superClass = classSym.getSuperclass().tsym; if (superClass == null) { return Description.NO_MATCH;
/** * Determines whether a symbol has an annotation of the given type. This includes annotations * inherited from superclasses due to {@code @Inherited}. * * @param annotationClass the binary class name of the annotation (e.g. * "javax.annotation.Nullable", or "some.package.OuterClassName$InnerClassName") * @return true if the symbol is annotated with given type. */ public static boolean hasAnnotation(Symbol sym, String annotationClass, VisitorState state) { if (sym == null) { return false; } // normalize to non-binary names annotationClass = annotationClass.replace('$', '.'); Name annotationName = state.getName(annotationClass); if (hasAttribute(sym, annotationName)) { return true; } if (isInherited(state, annotationClass)) { while (sym instanceof ClassSymbol) { if (hasAttribute(sym, annotationName)) { return true; } sym = ((ClassSymbol) sym).getSuperclass().tsym; } } return false; }
@Override public boolean matches(NewClassTree tree, VisitorState state) { MethodSymbol sym = ASTHelpers.getSymbol(tree); if (sym == null) { return false; } ClassSymbol owner = (ClassSymbol) sym.owner; if (owner == null) { return false; } Type superType = owner.getSuperclass(); if (superType == null) { return false; } Symbol superSymbol = superType.tsym; if (superSymbol == null) { return false; } if (!ASTHelpers.hasDirectAnnotationWithSimpleName(superSymbol, "AutoValue")) { return false; } return true; } };
private static String getClassName(ClassSymbol s) { if (s.isAnonymous()) { return s.getSuperclass().tsym.getSimpleName().toString(); } else { return s.getSimpleName().toString(); } }