Fast path annotation types

This commit is contained in:
Clément Fournier
2020-03-23 20:57:51 +01:00
parent 96532f26cc
commit a2994a97d9

View File

@ -78,6 +78,10 @@ public final class TypeHelper {
* @return <code>true</code> if type node n is of type clazzName or a subtype of clazzName
*/
public static boolean isA(final TypeNode n, final String clazzName) {
if (n.getType() != null && n.getType().isAnnotation()) {
return isAnnotationSubtype(n, clazzName);
}
final Class<?> clazz = loadClassWithNodeClassloader(n, clazzName);
if (clazz != null || n.getType() != null) {
@ -87,6 +91,15 @@ public final class TypeHelper {
return fallbackIsA(n, clazzName);
}
private static boolean isAnnotationSubtype(TypeNode n, String clazzName) {
// then, the supertype may only be Object, j.l.Annotation, or the class name
// this avoids classloading altogether
// this is used e.g. by the typeIs function in XPath
return clazzName.equals("java.lang.annotation.Annotation")
|| clazzName.equals("java.lang.Object")
|| clazzName.equals(n.getType().getName());
}
private static boolean fallbackIsA(TypeNode n, String clazzName) {
if (clazzName.equals(n.getImage()) || clazzName.endsWith("." + n.getImage())) {
return true;
@ -134,6 +147,11 @@ public final class TypeHelper {
* @return <code>true</code> if type node n is exactly of type clazzName.
*/
public static boolean isExactlyA(final TypeNode n, final String clazzName) {
if (n.getType() != null && n.getType().getName().equals(clazzName)) {
// fast path avoiding classloading
return true;
}
final Class<?> clazz = loadClassWithNodeClassloader(n, clazzName);
if (clazz != null) {