();
+ ClassOrInterfaceType cit;
+}
+{
+ "permits" cit = AnnotatedClassOrInterfaceType() { ret.add(cit); }
+ ( "," cit = AnnotatedClassOrInterfaceType() { ret.add(cit); } )*
+ { return ret; }
+}
+
/**
* https://docs.oracle.com/javase/specs/jls/se15/html/jls-8.html#jls-8.9
* {@code
diff --git a/javaparser-symbol-solver-core/pom.xml b/javaparser-symbol-solver-core/pom.xml
index 8f4f63d3c3..115bbe127d 100644
--- a/javaparser-symbol-solver-core/pom.xml
+++ b/javaparser-symbol-solver-core/pom.xml
@@ -3,8 +3,8 @@
javaparser-parent
com.github.javaparser
- 3.25.2
-
+ 3.25.3
+
4.0.0
javaparser-symbol-solver-core
diff --git a/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javaparsermodel/contexts/MethodCallExprContext.java b/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javaparsermodel/contexts/MethodCallExprContext.java
index 213ae36cd7..4909fbee3b 100644
--- a/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javaparsermodel/contexts/MethodCallExprContext.java
+++ b/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javaparsermodel/contexts/MethodCallExprContext.java
@@ -340,13 +340,24 @@ private MethodUsage resolveMethodTypeParameters(MethodUsage methodUsage, List
// but Arrays.aslist(String[]{""}) must returns List
- // May be the result depends on the component type of the array
+ // Arrays.asList() accept generic type T. Since Java generics work only on
+ // reference types (object types), not on primitives, and int[] is an object
+ // then Arrays.aslist(int[]{1}) returns List
ResolvedType lastActualParamType =
actualParamTypes.get(actualParamTypes.size() - 1);
- ResolvedType actualType =
- lastActualParamType.isArray() && lastActualParamType.asArrayType().getComponentType().isReferenceType() ?
- lastActualParamType.asArrayType().getComponentType() :
- lastActualParamType;
+ ResolvedType actualType = lastActualParamType;
+ if (lastActualParamType.isArray()) {
+ ResolvedType componentType = lastActualParamType.asArrayType().getComponentType();
+ // in cases where, the expected type is a generic type (Arrays.asList(T... a)) and the component type of the array type is a reference type
+ // or the expected type is not a generic (IntStream.of(int... values)) and the component type is not a reference type
+ // then the actual type is the component type (in the example above 'int')
+ if ((componentType.isReferenceType()
+ && ResolvedTypeVariable.class.isInstance(expectedType))
+ || (!componentType.isReferenceType()
+ && !ResolvedTypeVariable.class.isInstance(expectedType))) {
+ actualType = lastActualParamType.asArrayType().getComponentType();
+ }
+ }
if (!expectedType.isAssignableBy(actualType)) {
for (ResolvedTypeParameterDeclaration tp : methodUsage.getDeclaration().getTypeParameters()) {
expectedType = MethodResolutionLogic.replaceTypeParam(expectedType, tp, typeSolver);
diff --git a/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javaparsermodel/declarations/JavaParserAnnotationDeclaration.java b/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javaparsermodel/declarations/JavaParserAnnotationDeclaration.java
index f99376a54d..405bac13ff 100644
--- a/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javaparsermodel/declarations/JavaParserAnnotationDeclaration.java
+++ b/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javaparsermodel/declarations/JavaParserAnnotationDeclaration.java
@@ -21,6 +21,10 @@
package com.github.javaparser.symbolsolver.javaparsermodel.declarations;
+import java.lang.annotation.Inherited;
+import java.util.*;
+import java.util.stream.Collectors;
+
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.body.AnnotationDeclaration;
import com.github.javaparser.ast.body.AnnotationMemberDeclaration;
@@ -31,10 +35,6 @@
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.logic.AbstractTypeDeclaration;
-import java.lang.annotation.Inherited;
-import java.util.*;
-import java.util.stream.Collectors;
-
/**
* @author Federico Tomassetti
*/
@@ -92,6 +92,14 @@ public boolean hasDirectlyAnnotation(String canonicalName) {
return AstResolutionUtils.hasDirectlyAnnotation(wrappedNode, typeSolver, canonicalName);
}
+ /*
+ * Returns a set of the declared annotation on this type
+ */
+ @Override
+ public Set getDeclaredAnnotations() {
+ return javaParserTypeAdapter.getDeclaredAnnotations();
+ }
+
@Override
public String getPackageName() {
return AstResolutionUtils.getPackageName(wrappedNode);
diff --git a/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javaparsermodel/declarations/JavaParserAnonymousClassDeclaration.java b/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javaparsermodel/declarations/JavaParserAnonymousClassDeclaration.java
index 5e95bf1131..18b497aee1 100644
--- a/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javaparsermodel/declarations/JavaParserAnonymousClassDeclaration.java
+++ b/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javaparsermodel/declarations/JavaParserAnonymousClassDeclaration.java
@@ -21,6 +21,9 @@
package com.github.javaparser.symbolsolver.javaparsermodel.declarations;
+import java.util.*;
+import java.util.stream.Collectors;
+
import com.github.javaparser.ast.AccessSpecifier;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.body.FieldDeclaration;
@@ -44,9 +47,6 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
-import java.util.*;
-import java.util.stream.Collectors;
-
/**
* An anonymous class declaration representation.
*/
diff --git a/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javaparsermodel/declarations/JavaParserClassDeclaration.java b/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javaparsermodel/declarations/JavaParserClassDeclaration.java
index 6778fb2c95..c73ab84457 100644
--- a/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javaparsermodel/declarations/JavaParserClassDeclaration.java
+++ b/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javaparsermodel/declarations/JavaParserClassDeclaration.java
@@ -21,6 +21,9 @@
package com.github.javaparser.symbolsolver.javaparsermodel.declarations;
+import java.util.*;
+import java.util.stream.Collectors;
+
import com.github.javaparser.ast.AccessSpecifier;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.body.BodyDeclaration;
@@ -44,9 +47,6 @@
import com.github.javaparser.symbolsolver.logic.AbstractClassDeclaration;
import com.github.javaparser.symbolsolver.resolution.SymbolSolver;
-import java.util.*;
-import java.util.stream.Collectors;
-
/**
* @author Federico Tomassetti
*/
@@ -104,7 +104,7 @@ public String toString() {
///
/// Public methods: fields
///
-
+
@Override
public List getAllFields() {
List fields = javaParserTypeAdapter.getFieldsForDeclaredVariables();
@@ -143,7 +143,7 @@ public boolean isVolatile() {
public ResolvedTypeDeclaration declaringType() {
return f.declaringType();
}
-
+
@Override
public Optional toAst() {
return f.toAst();
@@ -223,6 +223,14 @@ public boolean hasDirectlyAnnotation(String canonicalName) {
return AstResolutionUtils.hasDirectlyAnnotation(wrappedNode, typeSolver, canonicalName);
}
+ /*
+ * Returns a set of the declared annotation on this type
+ */
+ @Override
+ public Set getDeclaredAnnotations() {
+ return javaParserTypeAdapter.getDeclaredAnnotations();
+ }
+
@Override
public boolean isInterface() {
return wrappedNode.isInterface();
diff --git a/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javaparsermodel/declarations/JavaParserInterfaceDeclaration.java b/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javaparsermodel/declarations/JavaParserInterfaceDeclaration.java
index 4bae624d24..5264281b72 100644
--- a/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javaparsermodel/declarations/JavaParserInterfaceDeclaration.java
+++ b/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javaparsermodel/declarations/JavaParserInterfaceDeclaration.java
@@ -21,6 +21,9 @@
package com.github.javaparser.symbolsolver.javaparsermodel.declarations;
+import java.util.*;
+import java.util.stream.Collectors;
+
import com.github.javaparser.ast.AccessSpecifier;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.body.BodyDeclaration;
@@ -44,9 +47,6 @@
import com.github.javaparser.symbolsolver.logic.AbstractTypeDeclaration;
import com.github.javaparser.symbolsolver.resolution.SymbolSolver;
-import java.util.*;
-import java.util.stream.Collectors;
-
/**
* @author Federico Tomassetti
*/
@@ -118,6 +118,14 @@ public boolean hasDirectlyAnnotation(String canonicalName) {
return AstResolutionUtils.hasDirectlyAnnotation(wrappedNode, typeSolver, canonicalName);
}
+ /*
+ * Returns a set of the declared annotation on this type
+ */
+ @Override
+ public Set getDeclaredAnnotations() {
+ return javaParserTypeAdapter.getDeclaredAnnotations();
+ }
+
@Override
public boolean isInterface() {
return true;
@@ -232,7 +240,7 @@ public boolean isVolatile() {
public ResolvedTypeDeclaration declaringType() {
return f.declaringType();
}
-
+
@Override
public Optional toAst() {
return f.toAst();
@@ -240,7 +248,7 @@ public Optional toAst() {
});
})
);
-
+
return fields;
}
diff --git a/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javaparsermodel/declarations/JavaParserTypeAdapter.java b/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javaparsermodel/declarations/JavaParserTypeAdapter.java
index 893e783b53..5733080a7d 100644
--- a/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javaparsermodel/declarations/JavaParserTypeAdapter.java
+++ b/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javaparsermodel/declarations/JavaParserTypeAdapter.java
@@ -21,14 +21,19 @@
package com.github.javaparser.symbolsolver.javaparsermodel.declarations;
+import java.util.*;
+import java.util.stream.Collectors;
+
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.*;
+import com.github.javaparser.ast.nodeTypes.NodeWithAnnotations;
import com.github.javaparser.ast.nodeTypes.NodeWithMembers;
import com.github.javaparser.ast.nodeTypes.NodeWithSimpleName;
import com.github.javaparser.ast.nodeTypes.NodeWithTypeParameters;
import com.github.javaparser.ast.type.TypeParameter;
import com.github.javaparser.resolution.TypeSolver;
+import com.github.javaparser.resolution.declarations.ResolvedAnnotationDeclaration;
import com.github.javaparser.resolution.declarations.ResolvedFieldDeclaration;
import com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration;
import com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration;
@@ -39,12 +44,10 @@
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.resolution.SymbolSolver;
-import java.util.*;
-
/**
* @author Federico Tomassetti
*/
-public class JavaParserTypeAdapter & NodeWithMembers> {
+public class JavaParserTypeAdapter & NodeWithMembers & NodeWithAnnotations> {
private T wrappedNode;
private TypeSolver typeSolver;
@@ -157,7 +160,7 @@ public Optional containerType() {
.getParentNode()
.map(node -> node.getSymbolResolver().toTypeDeclaration(node));
}
-
+
public List getFieldsForDeclaredVariables() {
List fields = new ArrayList<>();
if (wrappedNode.getMembers() != null) {
@@ -173,6 +176,15 @@ public List getFieldsForDeclaredVariables() {
return fields;
}
+ /*
+ * Returns a set of the declared annotation on this type
+ */
+ public Set getDeclaredAnnotations() {
+ return wrappedNode.getAnnotations().stream()
+ .map(annotation -> annotation.resolve())
+ .collect(Collectors.toSet());
+ }
+
public Set internalTypes() {
// Use a special Set implementation that avoids calculating the hashCode of the node,
// since this can be very time-consuming for big node trees, and we are sure there are
diff --git a/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javassistmodel/JavassistClassDeclaration.java b/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javassistmodel/JavassistClassDeclaration.java
index 8e83c3e013..c56fd027c6 100644
--- a/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javassistmodel/JavassistClassDeclaration.java
+++ b/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javassistmodel/JavassistClassDeclaration.java
@@ -21,6 +21,10 @@
package com.github.javaparser.symbolsolver.javassistmodel;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+
import com.github.javaparser.ast.AccessSpecifier;
import com.github.javaparser.ast.Node;
import com.github.javaparser.resolution.Context;
@@ -37,13 +41,10 @@
import com.github.javaparser.symbolsolver.core.resolution.SymbolResolutionCapability;
import com.github.javaparser.symbolsolver.logic.AbstractClassDeclaration;
import com.github.javaparser.symbolsolver.resolution.SymbolSolver;
+
import javassist.CtClass;
import javassist.CtField;
-import java.util.List;
-import java.util.Optional;
-import java.util.Set;
-
/**
* @author Federico Tomassetti
*/
@@ -76,6 +77,14 @@ public boolean hasDirectlyAnnotation(String canonicalName) {
return ctClass.hasAnnotation(canonicalName);
}
+ /*
+ * Returns a set of the declared annotation on this type
+ */
+ @Override
+ public Set getDeclaredAnnotations() {
+ return javassistTypeDeclarationAdapter.getDeclaredAnnotations();
+ }
+
@Override
public Set getDeclaredMethods() {
return javassistTypeDeclarationAdapter.getDeclaredMethods();
@@ -120,7 +129,8 @@ public String getQualifiedName() {
return ctClass.getName().replace('$', '.');
}
- @Deprecated
+ @Override
+ @Deprecated
public Optional solveMethodAsUsage(String name, List argumentsTypes,
Context invokationContext, List typeParameterValues) {
return JavassistUtils.solveMethodAsUsage(name, argumentsTypes, typeSolver, invokationContext, typeParameterValues, this, ctClass);
diff --git a/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javassistmodel/JavassistInterfaceDeclaration.java b/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javassistmodel/JavassistInterfaceDeclaration.java
index bc183d78bf..76ba87101d 100644
--- a/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javassistmodel/JavassistInterfaceDeclaration.java
+++ b/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javassistmodel/JavassistInterfaceDeclaration.java
@@ -21,6 +21,9 @@
package com.github.javaparser.symbolsolver.javassistmodel;
+import java.util.*;
+import java.util.stream.Collectors;
+
import com.github.javaparser.ast.AccessSpecifier;
import com.github.javaparser.resolution.Context;
import com.github.javaparser.resolution.MethodUsage;
@@ -35,12 +38,10 @@
import com.github.javaparser.symbolsolver.core.resolution.SymbolResolutionCapability;
import com.github.javaparser.symbolsolver.logic.AbstractTypeDeclaration;
import com.github.javaparser.symbolsolver.resolution.SymbolSolver;
+
import javassist.CtClass;
import javassist.CtField;
-import java.util.*;
-import java.util.stream.Collectors;
-
/**
* @author Federico Tomassetti
*/
@@ -93,7 +94,8 @@ public String getQualifiedName() {
return ctClass.getName().replace('$', '.');
}
- @Deprecated
+ @Override
+ @Deprecated
public Optional solveMethodAsUsage(String name, List argumentsTypes,
Context invokationContext, List typeParameterValues) {
return JavassistUtils.solveMethodAsUsage(name, argumentsTypes, typeSolver, invokationContext, typeParameterValues, this, ctClass);
@@ -137,6 +139,14 @@ public boolean hasDirectlyAnnotation(String canonicalName) {
return ctClass.hasAnnotation(canonicalName);
}
+ /*
+ * Returns a set of the declared annotation on this type
+ */
+ @Override
+ public Set getDeclaredAnnotations() {
+ return javassistTypeDeclarationAdapter.getDeclaredAnnotations();
+ }
+
@Override
public String getName() {
String[] nameElements = ctClass.getSimpleName().replace('$', '.').split("\\.");
diff --git a/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javassistmodel/JavassistTypeDeclarationAdapter.java b/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javassistmodel/JavassistTypeDeclarationAdapter.java
index fe1cf9d74b..ebbd8e82ec 100644
--- a/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javassistmodel/JavassistTypeDeclarationAdapter.java
+++ b/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javassistmodel/JavassistTypeDeclarationAdapter.java
@@ -21,6 +21,12 @@
package com.github.javaparser.symbolsolver.javassistmodel;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.util.*;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.resolution.UnsolvedSymbolException;
import com.github.javaparser.resolution.declarations.*;
@@ -28,6 +34,7 @@
import com.github.javaparser.resolution.model.typesystem.ReferenceTypeImpl;
import com.github.javaparser.resolution.types.ResolvedReferenceType;
import com.github.javaparser.resolution.types.ResolvedType;
+
import javassist.CtClass;
import javassist.CtField;
import javassist.NotFoundException;
@@ -35,15 +42,26 @@
import javassist.bytecode.BadBytecode;
import javassist.bytecode.SignatureAttribute;
-import java.util.*;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
-
/**
* @author Federico Tomassetti
*/
public class JavassistTypeDeclarationAdapter {
+ // this a workaround to get the annotation type (taken from Javassist AnnotationImpl class)
+ private static final String JDK_ANNOTATION_CLASS_NAME = "java.lang.annotation.Annotation";
+ private static Method JDK_ANNOTATION_TYPE_METHOD = null;
+
+ static {
+ // Try to resolve the JDK annotation type method
+ try {
+ Class> clazz = Class.forName(JDK_ANNOTATION_CLASS_NAME);
+ JDK_ANNOTATION_TYPE_METHOD = clazz.getMethod("annotationType", (Class[])null);
+ }
+ catch (Exception ignored) {
+ // Probably not JDK5+
+ }
+ }
+
private CtClass ctClass;
private TypeSolver typeSolver;
private ResolvedReferenceTypeDeclaration typeDeclaration;
@@ -164,6 +182,36 @@ public List getDeclaredFields() {
return fields;
}
+ /*
+ * Returns a set of the declared annotation on this type
+ */
+ public Set getDeclaredAnnotations() {
+ try {
+ Object[] annotations = ctClass.getAnnotations();
+ return Stream.of(annotations)
+ .map(annotation -> getAnnotationType(annotation))
+ .filter(annotationType -> annotationType != null)
+ .map(annotationType -> typeSolver.solveType(annotationType))
+ .map(rrtd -> rrtd.asAnnotation())
+ .collect(Collectors.toSet());
+ } catch (ClassNotFoundException e) {
+ // There is nothing to do except returns an empty set
+ }
+ return Collections.EMPTY_SET;
+
+ }
+
+ private String getAnnotationType(Object annotation) {
+ String typeName = null;
+ try {
+ Class> annotationClass = (Class>) Proxy.getInvocationHandler(annotation)
+ .invoke(annotation, JDK_ANNOTATION_TYPE_METHOD, null);
+ typeName = annotationClass.getTypeName();
+ } catch (Throwable e) {
+ }
+ return typeName;
+ }
+
public List getTypeParameters() {
if (null == ctClass.getGenericSignature()) {
return Collections.emptyList();
diff --git a/javaparser-symbol-solver-testing/pom.xml b/javaparser-symbol-solver-testing/pom.xml
index 5ba6e1fbdf..6f6d327605 100644
--- a/javaparser-symbol-solver-testing/pom.xml
+++ b/javaparser-symbol-solver-testing/pom.xml
@@ -3,7 +3,7 @@
javaparser-parent
com.github.javaparser
- 3.25.2
+ 3.25.3
4.0.0
diff --git a/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/javaparsermodel/declarations/JavaParserClassDeclarationTest.java b/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/javaparsermodel/declarations/JavaParserClassDeclarationTest.java
index fc6ad17cb1..08fb1d0235 100644
--- a/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/javaparsermodel/declarations/JavaParserClassDeclarationTest.java
+++ b/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/javaparsermodel/declarations/JavaParserClassDeclarationTest.java
@@ -927,12 +927,22 @@ void testHasAnnotation() throws IOException {
assertFalse(ca.hasAnnotation("foo.bar.MyUnexistingAnnotation"));
JavaParserClassDeclaration cb = new JavaParserClassDeclaration(Navigator.demandClass(cu, "CB"), typeSolver);
- assertTrue(cb.hasAnnotation("foo.bar.MyAnnotation"));
+ assertFalse(cb.hasAnnotation("foo.bar.MyAnnotation"));
assertTrue(cb.hasAnnotation("foo.bar.MyAnnotation2"));
assertFalse(cb.hasAnnotation("MyAnnotation"));
assertFalse(cb.hasAnnotation("foo.bar.MyUnexistingAnnotation"));
}
+ @Test
+ void testHasInheritedAnnotation() throws IOException {
+ TypeSolver typeSolver = new ReflectionTypeSolver();
+
+ CompilationUnit cu = parse(adaptPath("src/test/resources/Annotations.java.txt"));
+
+ JavaParserClassDeclaration child = new JavaParserClassDeclaration(Navigator.demandClass(cu, "Child"), typeSolver);
+ assertTrue(child.hasAnnotation("foo.bar.InheritedAnnotation"));
+ }
+
///
///
///
diff --git a/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/javassistmodel/JavassistClassDeclarationTest.java b/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/javassistmodel/JavassistClassDeclarationTest.java
index 6474cca1c5..cb0eee4079 100644
--- a/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/javassistmodel/JavassistClassDeclarationTest.java
+++ b/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/javassistmodel/JavassistClassDeclarationTest.java
@@ -21,6 +21,17 @@
package com.github.javaparser.symbolsolver.javassistmodel;
+import static org.junit.jupiter.api.Assertions.*;
+
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.*;
+import java.util.stream.Collectors;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Nested;
+import org.junit.jupiter.api.Test;
+
import com.github.javaparser.ast.Node;
import com.github.javaparser.resolution.MethodUsage;
import com.github.javaparser.resolution.TypeSolver;
@@ -39,19 +50,10 @@
import com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import com.google.common.collect.ImmutableSet;
+
import javassist.ClassPool;
import javassist.CtClass;
import javassist.NotFoundException;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Nested;
-import org.junit.jupiter.api.Test;
-
-import java.io.IOException;
-import java.nio.file.Path;
-import java.util.*;
-import java.util.stream.Collectors;
-
-import static org.junit.jupiter.api.Assertions.*;
class JavassistClassDeclarationTest extends AbstractClassDeclarationTest {
@@ -162,7 +164,8 @@ void testHasDirectlyAnnotation() {
@Test
void testHasAnnotation() {
JavassistClassDeclaration compilationUnit = (JavassistClassDeclaration) anotherTypeSolver.solveType("com.github.javaparser.test.TestChildClass");
- assertTrue(compilationUnit.hasAnnotation("com.github.javaparser.test.TestAnnotation"));
+ //TestChildClass has no TestAnnotation annotation declared even though parent class is annotated with this annotation because TestAnnotation annotation is not inheritable
+ assertFalse(compilationUnit.hasAnnotation("com.github.javaparser.test.TestAnnotation"));
}
@Test
diff --git a/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/javassistmodel/JavassistEnumDeclarationTest.java b/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/javassistmodel/JavassistEnumDeclarationTest.java
index a65731f99c..35cfd540db 100644
--- a/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/javassistmodel/JavassistEnumDeclarationTest.java
+++ b/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/javassistmodel/JavassistEnumDeclarationTest.java
@@ -21,19 +21,20 @@
package com.github.javaparser.symbolsolver.javassistmodel;
+import static org.junit.jupiter.api.Assertions.*;
+
+import java.io.IOException;
+import java.nio.file.Path;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.resolution.declarations.ResolvedEnumDeclaration;
import com.github.javaparser.symbolsolver.AbstractSymbolResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-
-import java.io.IOException;
-import java.nio.file.Path;
-
-import static org.junit.jupiter.api.Assertions.*;
class JavassistEnumDeclarationTest extends AbstractSymbolResolutionTest {
@@ -139,7 +140,7 @@ void testHasDirectlyAnnotation(){
@Test
void testHasAnnotation(){
ResolvedEnumDeclaration compilationUnit = (ResolvedEnumDeclaration) anotherTypeSolver.solveType("com.github.javaparser.test.TestParentEnum");
- assertTrue(compilationUnit.hasAnnotation("com.github.javaparser.test.TestAnnotation"));
+ assertFalse(compilationUnit.hasAnnotation("com.github.javaparser.test.TestAnnotation"));
}
///
diff --git a/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/javassistmodel/JavassistInterfaceDeclarationTest.java b/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/javassistmodel/JavassistInterfaceDeclarationTest.java
index 740e7a4980..6cffe3c671 100644
--- a/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/javassistmodel/JavassistInterfaceDeclarationTest.java
+++ b/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/javassistmodel/JavassistInterfaceDeclarationTest.java
@@ -21,6 +21,19 @@
package com.github.javaparser.symbolsolver.javassistmodel;
+import static org.junit.jupiter.api.Assertions.*;
+
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.function.Consumer;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Nested;
+import org.junit.jupiter.api.Test;
+
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration;
import com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration;
@@ -36,20 +49,9 @@
import com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import com.github.javaparser.utils.Pair;
+
import javassist.ClassPool;
import javassist.CtClass;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Nested;
-import org.junit.jupiter.api.Test;
-
-import java.io.IOException;
-import java.nio.file.Path;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.List;
-import java.util.function.Consumer;
-
-import static org.junit.jupiter.api.Assertions.*;
class JavassistInterfaceDeclarationTest extends AbstractSymbolResolutionTest {
@@ -155,7 +157,7 @@ void testHasDirectlyAnnotation(){
@Test
void testHasAnnotation(){
JavassistInterfaceDeclaration compilationUnit = (JavassistInterfaceDeclaration) anotherTypeSolver.solveType("com.github.javaparser.test.TestChildInterface");
- assertTrue(compilationUnit.hasAnnotation("com.github.javaparser.test.TestAnnotation"));
+ assertFalse(compilationUnit.hasAnnotation("com.github.javaparser.test.TestAnnotation"));
}
@Nested
diff --git a/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/model/typesystem/WildcardUsageTest.java b/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/model/typesystem/WildcardUsageTest.java
index 501518cf07..10bde6cf71 100644
--- a/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/model/typesystem/WildcardUsageTest.java
+++ b/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/model/typesystem/WildcardUsageTest.java
@@ -21,19 +21,25 @@
package com.github.javaparser.symbolsolver.model.typesystem;
+import static org.junit.jupiter.api.Assertions.*;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration;
import com.github.javaparser.resolution.model.typesystem.ReferenceTypeImpl;
+import com.github.javaparser.resolution.types.ResolvedReferenceType;
+import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.resolution.types.ResolvedTypeVariable;
import com.github.javaparser.resolution.types.ResolvedWildcard;
import com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-
-import java.util.Collections;
-
-import static org.junit.jupiter.api.Assertions.*;
class WildcardUsageTest {
@@ -424,4 +430,57 @@ class Foo {
assertEquals(false, ref.getFieldType("bar").isPresent());
}*/
+// @Test
+// void testIsAssignableByGenerics() {
+// ResolvedType listOfSomethingExtendingNumbers = genericType(List.class.getCanonicalName(),
+// extendsBound(Number.class.getCanonicalName()));
+// ResolvedType listOfNumbers = genericType(List.class.getCanonicalName(), Number.class.getCanonicalName());
+// ResolvedType listOfSomethingExtendingIntegers = genericType(List.class.getCanonicalName(),
+// extendsBound(Integer.class.getCanonicalName()));
+//
+// ResolvedType list1 = genericType(List.class.getCanonicalName(), Integer.class.getCanonicalName());
+//
+// print(listOfSomethingExtendingIntegers.asReferenceType().getAllAncestors());
+//
+// Collection extends Number> c1;
+// Collection c2 = new ArrayList<>();;
+// List extends Number> lnum = new ArrayList<>();
+// List extends Integer> lint = new ArrayList<>();
+// c1 = lint;
+// c1 = lnum;
+// lnum = lint;
+// c1 = c2;
+// }
+
+
+ // Utility methods
+
+ private void print(List ancestors) {
+ for (ResolvedReferenceType ancestor : ancestors) {
+ System.out.println(ancestor.describe());
+ }
+ }
+
+ private List types(String... types) {
+ return Arrays.stream(types).map(type -> type(type)).collect(Collectors.toList());
+ }
+
+ private ResolvedType type(String type) {
+ return new ReferenceTypeImpl(typeSolver.solveType(type));
+ }
+
+ private ResolvedType genericType(String type, String... parameterTypes) {
+ return new ReferenceTypeImpl(typeSolver.solveType(type), types(parameterTypes));
+ }
+
+ private ResolvedType genericType(String type, ResolvedType... parameterTypes) {
+ return new ReferenceTypeImpl(typeSolver.solveType(type), Arrays.asList(parameterTypes));
+ }
+
+ private ResolvedType extendsBound(String type) {
+ return ResolvedWildcard.extendsBound(type(type));
+ }
+
+
+
}
diff --git a/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/resolution/DefaultPackageTest.java b/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/resolution/DefaultPackageTest.java
index a389ec1627..cb977ce19e 100644
--- a/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/resolution/DefaultPackageTest.java
+++ b/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/resolution/DefaultPackageTest.java
@@ -21,6 +21,14 @@
package com.github.javaparser.symbolsolver.resolution;
+import static com.github.javaparser.StaticJavaParser.parse;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.util.*;
+
+import org.junit.jupiter.api.Test;
+
import com.github.javaparser.ast.AccessSpecifier;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.resolution.UnsolvedSymbolException;
@@ -31,13 +39,6 @@
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.logic.AbstractClassDeclaration;
import com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver;
-import org.junit.jupiter.api.Test;
-
-import java.util.*;
-
-import static com.github.javaparser.StaticJavaParser.parse;
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* See issue #16
@@ -146,6 +147,11 @@ public Optional containerType() {
public SymbolReference solveMethod(String name, List argumentsTypes, boolean staticOnly) {
throw new UnsupportedOperationException();
}
+
+ @Override
+ public Set getDeclaredAnnotations() {
+ return new HashSet<>();
+ }
}
@Test
@@ -169,7 +175,7 @@ void aClassInDefaultPackageCanBeAccessedFromOutsideTheDefaultPackageImportingIt(
ResolvedType resolvedType = JavaParserFacade.get(memoryTypeSolver).convertToUsage(jpType);
assertEquals("B", resolvedType.asReferenceType().getQualifiedName());
});
-
+
}
@Test
@@ -181,6 +187,6 @@ void aClassInDefaultPackageCanBeAccessedFromOutsideTheDefaultPackageWithoutImpor
ResolvedType resolvedType = JavaParserFacade.get(memoryTypeSolver).convertToUsage(parse(code).getClassByName("A").get().getExtendedTypes(0));
assertEquals("B", resolvedType.asReferenceType().getQualifiedName());
});
-
+
}
}
diff --git a/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/resolution/JavaParserFacadeResolutionTest.java b/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/resolution/JavaParserFacadeResolutionTest.java
index 76cb94970d..8cac015477 100644
--- a/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/resolution/JavaParserFacadeResolutionTest.java
+++ b/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/resolution/JavaParserFacadeResolutionTest.java
@@ -250,6 +250,49 @@ void resolveTypeParameterFromPrimitiveArrayArgument() {
assertEquals("java.util.List", resolvedType.describe());
}
+ @Test
+ void resolveTypeParameterFromReferenceArrayArgument() {
+ String sourceCode = "" +
+ "import java.util.Arrays;\n" +
+ "\n" +
+ "public class Main {\n" +
+ " public void main(String[] args) {\n" +
+ " Arrays.asList(args);\n" +
+ " }\n" +
+ "}";
+
+ JavaParser parser = createParserWithResolver(defaultTypeSolver());
+ CompilationUnit cu = parser.parse(sourceCode).getResult().get();
+
+ MethodCallExpr mce = cu.findFirst(MethodCallExpr.class).get();
+
+ ResolvedType resolvedType = mce.calculateResolvedType();
+
+ assertEquals("java.util.List", resolvedType.describe());
+ }
+
+ @Test
+ void resolveTypeParameterFromPrimitiveArrayArgumentOnNonGenericExpectedParameter() {
+ String sourceCode = "" +
+ "import java.util.OptionalDouble;\n" +
+ "import java.util.stream.IntStream;\n" +
+ "\n" +
+ "public class Main {\n" +
+ " OptionalDouble pre(int[] values) {\n" +
+ " return IntStream.of(values).map(s -> s).average();\n" +
+ " }\n" +
+ "}";
+
+ JavaParser parser = createParserWithResolver(defaultTypeSolver());
+ CompilationUnit cu = parser.parse(sourceCode).getResult().get();
+
+ MethodCallExpr mce = cu.findFirst(MethodCallExpr.class).get();
+
+ ResolvedType resolvedType = mce.calculateResolvedType();
+
+ assertEquals("java.util.OptionalDouble", resolvedType.describe());
+ }
+
// See issue 3725
@Test
void resolveVarTypeInForEachLoopFromIterableExpression2() {
diff --git a/javaparser-symbol-solver-testing/src/test/resources/Annotations.java.txt b/javaparser-symbol-solver-testing/src/test/resources/Annotations.java.txt
index 1ddea7a35b..0f8e7d5bae 100644
--- a/javaparser-symbol-solver-testing/src/test/resources/Annotations.java.txt
+++ b/javaparser-symbol-solver-testing/src/test/resources/Annotations.java.txt
@@ -80,3 +80,7 @@ class CH {
public @interface InheritedAnnotation {
}
+@InheritedAnnotation
+class Parent {}
+class Child extends Parent {}
+
diff --git a/pom.xml b/pom.xml
index c2513c4588..b0d3f8720f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -14,7 +14,7 @@
com.github.javaparser
javaparser-parent
pom
- 3.25.2
+ 3.25.3
javaparser-parent
https://github.com/javaparser
@@ -215,7 +215,7 @@
org.apache.maven.plugins
maven-install-plugin
- 3.1.0
+ 3.1.1
org.apache.maven.plugins
@@ -231,7 +231,7 @@
org.apache.maven.plugins
maven-release-plugin
- 2.5.3
+ 3.0.0
true
@@ -246,7 +246,7 @@
org.apache.maven.plugins
maven-resources-plugin
- 3.3.0
+ 3.3.1
org.apache.maven.plugins
@@ -273,7 +273,7 @@
org.jacoco
jacoco-maven-plugin
- 0.8.8
+ 0.8.10
org.apache.maven.plugins
@@ -317,7 +317,7 @@
org.apache.maven.plugins
maven-deploy-plugin
- 3.1.0
+ 3.1.1
org.codehaus.mojo
@@ -345,7 +345,7 @@
org.apache.maven.plugins
maven-checkstyle-plugin
- 3.2.1
+ 3.2.2
dev-files/JavaParser-CheckStyle.xml
true
@@ -355,7 +355,7 @@
com.puppycrawl.tools
checkstyle
- 10.9.1
+ 10.10.0
@@ -391,19 +391,19 @@
org.junit.jupiter
junit-jupiter-engine
- 5.9.2
+ 5.9.3
test
org.junit.jupiter
junit-jupiter-params
- 5.9.2
+ 5.9.3
test
org.junit.vintage
junit-vintage-engine
- 5.9.2
+ 5.9.3
test
diff --git a/readme.md b/readme.md
index 774abeda92..e9e5a47840 100644
--- a/readme.md
+++ b/readme.md
@@ -138,14 +138,14 @@ Just add the following to your maven configuration or tailor to your own depende
com.github.javaparser
javaparser-symbol-solver-core
- 3.25.2
+ 3.25.3
```
**Gradle**:
```
-implementation 'com.github.javaparser:javaparser-symbol-solver-core:3.25.2'
+implementation 'com.github.javaparser:javaparser-symbol-solver-core:3.25.3'
```
Since Version 3.5.10, the JavaParser project includes the JavaSymbolSolver.
@@ -160,14 +160,14 @@ Using the dependency above will add both JavaParser and JavaSymbolSolver to your
com.github.javaparser
javaparser-core
- 3.25.2
+ 3.25.3
```
**Gradle**:
```
-implementation 'com.github.javaparser:javaparser-core:3.25.2'
+implementation 'com.github.javaparser:javaparser-core:3.25.3'
```
Since version 3.6.17 the AST can be serialized to JSON.
@@ -179,14 +179,14 @@ There is a separate module for this:
com.github.javaparser
javaparser-core-serialization
- 3.25.2
+ 3.25.3
```
**Gradle**:
```
-implementation 'com.github.javaparser:javaparser-core-serialization:3.25.2'
+implementation 'com.github.javaparser:javaparser-core-serialization:3.25.3'
```
## How To Compile Sources