@Deprecated public SymbolReference<ResolvedTypeDeclaration> solveType(String name, TypeSolver typeSolver) { if (this.wrappedNode.getName().getId().equals(name)) { return SymbolReference.solved(this); } SymbolReference<ResolvedTypeDeclaration> ref = javaParserTypeAdapter.solveType(name, typeSolver); if (ref.isSolved()) { return ref; } String prefix = wrappedNode.getName() + "."; if (name.startsWith(prefix) && name.length() > prefix.length()) { return new JavaParserClassDeclaration(this.wrappedNode, typeSolver).solveType(name.substring(prefix.length()), typeSolver); } return getContext().getParent().solveType(name, typeSolver); }
@Override public List<ResolvedFieldDeclaration> getAllFields() { List<ResolvedFieldDeclaration> fields = javaParserTypeAdapter.getFieldsForDeclaredVariables(); getAncestors(true).forEach(ancestor -> ancestor.getTypeDeclaration().getAllFields().forEach(f -> { fields.add(new ResolvedFieldDeclaration() { @Override public Modifier.Keyword accessSpecifier() { return f.accessSpecifier(); } @Override public String getName() { return f.getName(); } @Override public ResolvedType getType() { return ancestor.useThisTypeParametersOnTheGivenType(f.getType()); } @Override public boolean isStatic() { return f.isStatic(); } @Override public ResolvedTypeDeclaration declaringType() { return f.declaringType(); } }); })); return fields; }
if (!(Object.class.getCanonicalName().equals(getQualifiedName()))) { ResolvedReferenceType superclass; try { superclass = getSuperClass(); } catch (UnsolvedSymbolException e) { if (acceptIncompleteList) { ResolvedReferenceType ancestor; try { ancestor = toReferenceType(implemented); } catch (UnsolvedSymbolException e) { if (acceptIncompleteList) {
@Override public List<ResolvedReferenceType> getAncestors() { List<ResolvedReferenceType> ancestors = new ArrayList<>(); ResolvedReferenceType superclass = getSuperClass(); if (superclass != null) { ancestors.add(superclass); } if (wrappedNode.getImplementedTypes() != null) { for (ClassOrInterfaceType implemented : wrappedNode.getImplementedTypes()) { ResolvedReferenceType ancestor = toReferenceType(implemented); ancestors.add(ancestor); } } return ancestors; }
@Override public ResolvedReferenceType getSuperClass() { if (wrappedNode.getExtendedTypes().isEmpty()) { return object(); } else { return toReferenceType(wrappedNode.getExtendedTypes().get(0)); } }
@Override public boolean canBeAssignedTo(ResolvedReferenceTypeDeclaration other) { // TODO consider generic types if (this.getQualifiedName().equals(other.getQualifiedName())) { return true; } ResolvedClassDeclaration superclass = (ResolvedClassDeclaration) getSuperClass().getTypeDeclaration(); if (superclass != null) { // We want to avoid infinite recursion in case of Object having Object as ancestor if (Object.class.getCanonicalName().equals(superclass.getQualifiedName())) { return true; } if (superclass.canBeAssignedTo(other)) { return true; } } if (this.wrappedNode.getImplementedTypes() != null) { for (ClassOrInterfaceType type : wrappedNode.getImplementedTypes()) { ResolvedReferenceTypeDeclaration ancestor = (ResolvedReferenceTypeDeclaration) new SymbolSolver(typeSolver).solveType(type); if (ancestor.canBeAssignedTo(other)) { return true; } } } return false; }
if (internalType.getName().getId().equals(name)) { if (internalType instanceof ClassOrInterfaceDeclaration) { return SymbolReference.solved(new JavaParserClassDeclaration((com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) internalType, typeSolver)); } else if (internalType instanceof EnumDeclaration) { return SymbolReference.solved(new JavaParserEnumDeclaration((com.github.javaparser.ast.body.EnumDeclaration) internalType, typeSolver)); return new JavaParserClassDeclaration((com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) internalType, typeSolver).solveType(name.substring(prefix.length()), typeSolver); } else if (internalType instanceof EnumDeclaration) { return new SymbolSolver(typeSolver).solveTypeInType(new JavaParserEnumDeclaration((com.github.javaparser.ast.body.EnumDeclaration) internalType, typeSolver), name.substring(prefix.length()));
@Override public SymbolReference<ResolvedMethodDeclaration> solveMethod(String name, List<ResolvedType> argumentsTypes, boolean staticOnly) { return getContext().solveMethod(name, argumentsTypes, staticOnly); }
@Test public void issue113superClassIsResolvedCorrectly() throws FileNotFoundException { String pathToSourceFile = adaptPath("src/test/resources/issue113/com/foo/Widget.java"); CompilationUnit cu = JavaParser.parse(new File(pathToSourceFile)); JavaParserClassDeclaration jssExtendedWidget = new JavaParserClassDeclaration(cu.getClassByName("Widget").get(), typeSolver); ResolvedReferenceType superClass = jssExtendedWidget.getSuperClass(); assertEquals("com.foo.base.Widget", superClass.getQualifiedName()); }
/** * Try to solve a symbol just in the declaration, it does not delegate to the container. */ @Deprecated public SymbolReference<ResolvedTypeDeclaration> solveTypeInType(ResolvedTypeDeclaration typeDeclaration, String name) { if (typeDeclaration instanceof JavaParserClassDeclaration) { return ((JavaParserClassDeclaration) typeDeclaration).solveType(name, typeSolver); } if (typeDeclaration instanceof JavaParserInterfaceDeclaration) { return ((JavaParserInterfaceDeclaration) typeDeclaration).solveType(name, typeSolver); } return SymbolReference.unsolved(ResolvedReferenceTypeDeclaration.class); } }
public static ResolvedReferenceTypeDeclaration toTypeDeclaration(Node node, TypeSolver typeSolver) { if (node instanceof ClassOrInterfaceDeclaration) { if (((ClassOrInterfaceDeclaration) node).isInterface()) { return new JavaParserInterfaceDeclaration((ClassOrInterfaceDeclaration) node, typeSolver); } else { return new JavaParserClassDeclaration((ClassOrInterfaceDeclaration) node, typeSolver); } } else if (node instanceof TypeParameter) { return new JavaParserTypeParameter((TypeParameter) node, typeSolver); } else if (node instanceof EnumDeclaration) { return new JavaParserEnumDeclaration((EnumDeclaration) node, typeSolver); } else if (node instanceof AnnotationDeclaration) { return new JavaParserAnnotationDeclaration((AnnotationDeclaration) node, typeSolver); } else { throw new IllegalArgumentException(node.getClass().getCanonicalName()); } } }
@Test public void testHasDirectlyAnnotation() throws IOException, ParseException { TypeSolver typeSolver = new ReflectionTypeSolver(); CompilationUnit cu = JavaParser.parse(adaptPath(new File("src/test/resources/Annotations.java.txt"))); JavaParserClassDeclaration ca = new JavaParserClassDeclaration(Navigator.demandClass(cu, "CA"), typeSolver); assertEquals(true, ca.hasDirectlyAnnotation("foo.bar.MyAnnotation")); assertEquals(false, ca.hasDirectlyAnnotation("foo.bar.MyAnnotation2")); assertEquals(false, ca.hasDirectlyAnnotation("MyAnnotation")); assertEquals(false, ca.hasDirectlyAnnotation("foo.bar.MyUnexistingAnnotation")); JavaParserClassDeclaration cb = new JavaParserClassDeclaration(Navigator.demandClass(cu, "CB"), typeSolver); assertEquals(false, cb.hasDirectlyAnnotation("foo.bar.MyAnnotation")); assertEquals(true, cb.hasDirectlyAnnotation("foo.bar.MyAnnotation2")); assertEquals(false, cb.hasDirectlyAnnotation("MyAnnotation")); assertEquals(false, cb.hasDirectlyAnnotation("foo.bar.MyUnexistingAnnotation")); }
@Test public void testHasAnnotation() throws IOException, ParseException { TypeSolver typeSolver = new ReflectionTypeSolver(); CompilationUnit cu = JavaParser.parse(adaptPath(new File("src/test/resources/Annotations.java.txt"))); JavaParserClassDeclaration ca = new JavaParserClassDeclaration(Navigator.demandClass(cu, "CA"), typeSolver); assertEquals(true, ca.hasAnnotation("foo.bar.MyAnnotation")); assertEquals(false, ca.hasAnnotation("foo.bar.MyAnnotation2")); assertEquals(false, ca.hasAnnotation("MyAnnotation")); assertEquals(false, ca.hasAnnotation("foo.bar.MyUnexistingAnnotation")); JavaParserClassDeclaration cb = new JavaParserClassDeclaration(Navigator.demandClass(cu, "CB"), typeSolver); assertEquals(true, cb.hasAnnotation("foo.bar.MyAnnotation")); assertEquals(true, cb.hasAnnotation("foo.bar.MyAnnotation2")); assertEquals(false, cb.hasAnnotation("MyAnnotation")); assertEquals(false, cb.hasAnnotation("foo.bar.MyUnexistingAnnotation")); }
@Test public void testGetAllGenericFields() throws IOException, ParseException { TypeSolver typeSolver = new ReflectionTypeSolver(); CompilationUnit cu = JavaParser.parse(adaptPath(new File("src/test/resources/GenericFields.java.txt"))); JavaParserClassDeclaration classDeclaration = new JavaParserClassDeclaration(Navigator.demandClass(cu, "CB"), typeSolver); assertEquals(3, classDeclaration.getAllFields().size()); ReferenceTypeImpl rtClassDeclaration = new ReferenceTypeImpl(classDeclaration, typeSolver); assertEquals("s", classDeclaration.getAllFields().get(0).getName()); assertEquals(string, classDeclaration.getAllFields().get(0).getType()); assertEquals(string, rtClassDeclaration.getFieldType("s").get()); assertEquals("t", classDeclaration.getAllFields().get(1).getName()); assertEquals("java.util.List<java.lang.Boolean>", classDeclaration.getAllFields().get(1).getType().describe()); assertEquals(listOfBoolean, rtClassDeclaration.getFieldType("t").get()); assertEquals("i", classDeclaration.getAllFields().get(2).getName()); assertEquals(ResolvedPrimitiveType.INT, classDeclaration.getAllFields().get(2).getType()); assertEquals(ResolvedPrimitiveType.INT, rtClassDeclaration.getFieldType("i").get()); }
@Test public void testGetSuperclassWithTypeParameters() { JavaParserClassDeclaration compilationUnit = (JavaParserClassDeclaration) typeSolverNewCode.solveType("com.github.javaparser.ast.body.ConstructorDeclaration"); assertEquals("com.github.javaparser.ast.body.BodyDeclaration", compilationUnit.getSuperClass().getQualifiedName()); assertEquals("com.github.javaparser.ast.body.ConstructorDeclaration", compilationUnit.getSuperClass().typeParametersMap().getValueBySignature("com.github.javaparser.ast.body.BodyDeclaration.T").get().asReferenceType().getQualifiedName()); }
@Override public List<ResolvedReferenceType> getInterfaces() { List<ResolvedReferenceType> interfaces = new ArrayList<>(); if (wrappedNode.getImplementedTypes() != null) { for (ClassOrInterfaceType t : wrappedNode.getImplementedTypes()) { interfaces.add(toReferenceType(t)); } } return interfaces; }
@Test public void testGetQualifiedName() { JavaParserClassDeclaration compilationUnit = (JavaParserClassDeclaration) typeSolver.solveType("com.github.javaparser.ast.CompilationUnit"); assertEquals("com.github.javaparser.ast.CompilationUnit", compilationUnit.getQualifiedName()); }
@Override public boolean canBeAssignedTo(ResolvedReferenceTypeDeclaration other) { // TODO consider generic types if (this.getQualifiedName().equals(other.getQualifiedName())) { return true; } ResolvedClassDeclaration superclass = (ResolvedClassDeclaration) getSuperClass().getTypeDeclaration(); if (superclass != null) { // We want to avoid infinite recursion in case of Object having Object as ancestor if (Object.class.getCanonicalName().equals(superclass.getQualifiedName())) { return true; } if (superclass.canBeAssignedTo(other)) { return true; } } if (this.wrappedNode.getImplementedTypes() != null) { for (ClassOrInterfaceType type : wrappedNode.getImplementedTypes()) { ResolvedReferenceTypeDeclaration ancestor = (ResolvedReferenceTypeDeclaration) new SymbolSolver(typeSolver).solveType(type); if (ancestor.canBeAssignedTo(other)) { return true; } } } return false; }
if (internalType.getName().getId().equals(name)) { if (internalType instanceof ClassOrInterfaceDeclaration) { return SymbolReference.solved(new JavaParserClassDeclaration((com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) internalType, typeSolver)); } else if (internalType instanceof EnumDeclaration) { return SymbolReference.solved(new JavaParserEnumDeclaration((com.github.javaparser.ast.body.EnumDeclaration) internalType, typeSolver)); return new JavaParserClassDeclaration((com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) internalType, typeSolver).solveType(name.substring(prefix.length()), typeSolver); } else if (internalType instanceof EnumDeclaration) { return new SymbolSolver(typeSolver).solveTypeInType(new JavaParserEnumDeclaration((com.github.javaparser.ast.body.EnumDeclaration) internalType, typeSolver), name.substring(prefix.length()));
@Override public ResolvedReferenceType getSuperClass() { if (wrappedNode.getExtendedTypes().isEmpty()) { return object(); } else { return toReferenceType(wrappedNode.getExtendedTypes().get(0)); } }