Skip to content

Commit e6280ef

Browse files
authored
Merge pull request #3142 from 4everTheOne/fix/issue-3140
Implemented getAncestors method for Annotation declaration
2 parents f762a0d + 05a62e1 commit e6280ef

File tree

4 files changed

+214
-1
lines changed

4 files changed

+214
-1
lines changed

javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javaparsermodel/JavaParserFacade.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@
9191
import com.github.javaparser.symbolsolver.model.resolution.SymbolReference;
9292
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
9393
import com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl;
94+
import com.github.javaparser.symbolsolver.reflectionmodel.ReflectionAnnotationDeclaration;
9495
import com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration;
96+
import com.github.javaparser.symbolsolver.reflectionmodel.ReflectionEnumDeclaration;
97+
import com.github.javaparser.symbolsolver.reflectionmodel.ReflectionInterfaceDeclaration;
9598
import com.github.javaparser.symbolsolver.resolution.ConstructorResolutionLogic;
9699
import com.github.javaparser.symbolsolver.resolution.MethodResolutionLogic;
97100
import com.github.javaparser.symbolsolver.resolution.SymbolSolver;
@@ -792,6 +795,18 @@ public ResolvedType classToResolvedType(Class<?> clazz) {
792795
if (clazz.isPrimitive()) {
793796
return ResolvedPrimitiveType.byName(clazz.getName());
794797
}
795-
return new ReferenceTypeImpl(new ReflectionClassDeclaration(clazz, typeSolver), typeSolver);
798+
799+
ResolvedReferenceTypeDeclaration declaration;
800+
if (clazz.isAnnotation()) {
801+
declaration = new ReflectionAnnotationDeclaration(clazz, typeSolver);
802+
} else if (clazz.isEnum()) {
803+
declaration = new ReflectionEnumDeclaration(clazz, typeSolver);
804+
} else if (clazz.isInterface()) {
805+
declaration = new ReflectionInterfaceDeclaration(clazz, typeSolver);
806+
} else {
807+
declaration = new ReflectionClassDeclaration(clazz, typeSolver);
808+
}
809+
return new ReferenceTypeImpl(declaration, typeSolver);
796810
}
811+
797812
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
3+
* Copyright (C) 2011, 2013-2021 The JavaParser Team.
4+
*
5+
* This file is part of JavaParser.
6+
*
7+
* JavaParser can be used either under the terms of
8+
* a) the GNU Lesser General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
* b) the terms of the Apache License
12+
*
13+
* You should have received a copy of both licenses in LICENCE.LGPL and
14+
* LICENCE.APACHE. Please refer to those files for details.
15+
*
16+
* JavaParser is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*/
21+
22+
package com.github.javaparser.symbolsolver.javaparsermodel;
23+
24+
import com.github.javaparser.resolution.types.ResolvedType;
25+
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
26+
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
27+
import org.junit.jupiter.api.Test;
28+
29+
import static org.junit.jupiter.api.Assertions.assertEquals;
30+
31+
class JavaParserFacadeTest {
32+
33+
private final TypeSolver typeSolver = new ReflectionTypeSolver();
34+
35+
@Test
36+
void classToResolvedType_givenPrimitiveShouldBeAReflectionPrimitiveDeclaration() {
37+
ResolvedType resolvedType = JavaParserFacade.get(typeSolver).classToResolvedType(int.class);
38+
assertEquals(int.class.getCanonicalName(), resolvedType.describe());
39+
}
40+
41+
@Test
42+
void classToResolvedType_givenClassShouldBeAReflectionClassDeclaration() {
43+
ResolvedType resolvedType = JavaParserFacade.get(typeSolver).classToResolvedType(String.class);
44+
assertEquals(String.class.getCanonicalName(), resolvedType.describe());
45+
}
46+
47+
@Test
48+
void classToResolvedType_givenClassShouldBeAReflectionInterfaceDeclaration() {
49+
ResolvedType resolvedType = JavaParserFacade.get(typeSolver).classToResolvedType(String.class);
50+
assertEquals(String.class.getCanonicalName(), resolvedType.describe());
51+
}
52+
53+
@Test
54+
void classToResolvedType_givenEnumShouldBeAReflectionEnumDeclaration() {
55+
ResolvedType resolvedType = JavaParserFacade.get(typeSolver).classToResolvedType(TestEnum.class);
56+
assertEquals(TestEnum.class.getCanonicalName(), resolvedType.describe());
57+
}
58+
59+
@Test
60+
void classToResolvedType_givenAnnotationShouldBeAReflectionAnnotationDeclaration() {
61+
ResolvedType resolvedType = JavaParserFacade.get(typeSolver).classToResolvedType(Override.class);
62+
assertEquals(Override.class.getCanonicalName(), resolvedType.describe());
63+
}
64+
65+
/**
66+
* Enum to be tested in {@link JavaParserFacadeTest#classToResolvedType_givenEnumShouldBeAReflectionEnumDeclaration}.
67+
*/
68+
private enum TestEnum {
69+
A, B;
70+
}
71+
72+
}

javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/javassistmodel/JavassistAnnotationDeclarationTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
package com.github.javaparser.symbolsolver.javassistmodel;
2323

2424
import com.github.javaparser.resolution.declarations.ResolvedAnnotationDeclarationTest;
25+
import com.github.javaparser.resolution.types.ResolvedReferenceType;
2526
import com.github.javaparser.symbolsolver.logic.AbstractTypeDeclaration;
2627
import com.github.javaparser.symbolsolver.logic.AbstractTypeDeclarationTest;
2728
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
@@ -32,6 +33,11 @@
3233
import org.junit.jupiter.api.Disabled;
3334
import org.junit.jupiter.api.Test;
3435

36+
import java.lang.annotation.Annotation;
37+
import java.util.List;
38+
39+
import static org.junit.jupiter.api.Assertions.assertEquals;
40+
3541
class JavassistAnnotationDeclarationTest extends AbstractTypeDeclarationTest implements ResolvedAnnotationDeclarationTest {
3642

3743
@Override
@@ -64,4 +70,16 @@ public void getDeclaredMethodsCantBeNull() {
6470
super.getDeclaredMethodsCantBeNull();
6571
}
6672

73+
@Test
74+
void getAncestors_shouldReturnAnnotation() throws NotFoundException {
75+
TypeSolver typeSolver = new ReflectionTypeSolver();
76+
CtClass clazz = ClassPool.getDefault().getCtClass("java.lang.Override");
77+
JavassistAnnotationDeclaration overrideAnnotation = new JavassistAnnotationDeclaration(clazz, typeSolver);
78+
79+
List<ResolvedReferenceType> ancestors = overrideAnnotation.getAncestors();
80+
assertEquals(2, ancestors.size());
81+
assertEquals(Object.class.getCanonicalName(), ancestors.get(0).getQualifiedName());
82+
assertEquals(Annotation.class.getCanonicalName(), ancestors.get(1).getQualifiedName());
83+
}
84+
6785
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
3+
* Copyright (C) 2011, 2013-2021 The JavaParser Team.
4+
*
5+
* This file is part of JavaParser.
6+
*
7+
* JavaParser can be used either under the terms of
8+
* a) the GNU Lesser General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
* b) the terms of the Apache License
12+
*
13+
* You should have received a copy of both licenses in LICENCE.LGPL and
14+
* LICENCE.APACHE. Please refer to those files for details.
15+
*
16+
* JavaParser is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*/
21+
22+
package com.github.javaparser.symbolsolver.javassistmodel;
23+
24+
import com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration;
25+
import com.github.javaparser.resolution.types.ResolvedReferenceType;
26+
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
27+
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
28+
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
29+
import javassist.ClassPool;
30+
import javassist.CtClass;
31+
import javassist.NotFoundException;
32+
import org.junit.jupiter.api.extension.ExtensionContext;
33+
import org.junit.jupiter.params.ParameterizedTest;
34+
import org.junit.jupiter.params.provider.Arguments;
35+
import org.junit.jupiter.params.provider.ArgumentsProvider;
36+
import org.junit.jupiter.params.provider.ArgumentsSource;
37+
38+
import java.io.IOException;
39+
import java.util.List;
40+
import java.util.stream.Collectors;
41+
import java.util.stream.Stream;
42+
43+
import static java.util.Arrays.asList;
44+
import static java.util.Collections.singletonList;
45+
import static org.junit.jupiter.api.Assertions.assertEquals;
46+
47+
class JavassistTypeDeclarationAdapterTest extends AbstractResolutionTest {
48+
49+
@ParameterizedTest(name = "Given {0} is expected {1}")
50+
@ArgumentsSource(GetAncestorsProvider.class)
51+
void testGetAncestors(String ctClass, List<String> expectedAncestors) throws NotFoundException, IOException {
52+
TypeSolver typeSolver = new ReflectionTypeSolver(false);
53+
CtClass clazz = new ClassPool(true).getCtClass(ctClass);
54+
55+
ResolvedReferenceTypeDeclaration declaration = JavassistFactory.toTypeDeclaration(clazz, typeSolver);
56+
JavassistTypeDeclarationAdapter adapter = new JavassistTypeDeclarationAdapter(clazz, typeSolver, declaration);
57+
58+
List<ResolvedReferenceType> resultAncestors = adapter.getAncestors(false);
59+
assertEquals(expectedAncestors,
60+
resultAncestors.stream().map(ResolvedReferenceType::getQualifiedName).collect(Collectors.toList()));
61+
}
62+
63+
/**
64+
* Class which provider arguments to be tested in {@link JavassistTypeDeclarationAdapterTest#testGetAncestors}
65+
*/
66+
static class GetAncestorsProvider implements ArgumentsProvider {
67+
@Override
68+
public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) {
69+
return Stream.of(
70+
// Node
71+
Arguments.of(
72+
"com.github.javaparser.ast.Node",
73+
asList(
74+
"java.lang.Object",
75+
"java.lang.Cloneable",
76+
"com.github.javaparser.HasParentNode",
77+
"com.github.javaparser.ast.visitor.Visitable",
78+
"com.github.javaparser.ast.nodeTypes.NodeWithRange",
79+
"com.github.javaparser.ast.nodeTypes.NodeWithTokenRange"
80+
)
81+
),
82+
// Expression
83+
Arguments.of(
84+
"com.github.javaparser.ast.expr.Expression",
85+
singletonList(
86+
"com.github.javaparser.ast.Node"
87+
)
88+
),
89+
// Annotation.class
90+
Arguments.of(
91+
"com.github.javaparser.ParseStart",
92+
singletonList(
93+
"java.lang.Object"
94+
)
95+
),
96+
// SlowTest Annotation
97+
Arguments.of(
98+
"com.github.javaparser.SlowTest",
99+
asList(
100+
"java.lang.Object",
101+
"java.lang.annotation.Annotation"
102+
)
103+
)
104+
);
105+
}
106+
}
107+
108+
}

0 commit comments

Comments
 (0)