Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typed overloads for methods taking class names #549

Merged
merged 2 commits into from Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/main/java/io/github/classgraph/AnnotationInfoList.java
Expand Up @@ -28,6 +28,7 @@
*/
package io.github.classgraph;

import java.lang.annotation.Annotation;
import java.lang.annotation.Repeatable;
import java.util.ArrayList;
import java.util.HashSet;
Expand All @@ -36,6 +37,7 @@
import java.util.Set;

import io.github.classgraph.ClassInfo.RelType;
import nonapi.io.github.classgraph.utils.Assert;
import nonapi.io.github.classgraph.utils.CollectionUtils;
import nonapi.io.github.classgraph.utils.LogNode;

Expand Down Expand Up @@ -344,6 +346,17 @@ public AnnotationInfoList directOnly() {

// -------------------------------------------------------------------------------------------------------------

/**
* Get the {@link Repeatable} annotation with the given class, or the empty list if none found.
*
* @param annotationClass The class to search for.
* @return The list of annotations with the given class, or the empty list if none found.
*/
public AnnotationInfoList getRepeatable(final Class<? extends Annotation> annotationClass) {
Assert.isAnnotation(annotationClass);
return getRepeatable(annotationClass.getName());
}

/**
* Get the {@link Repeatable} annotation with the given name, or the empty list if none found.
*
Expand Down
158 changes: 153 additions & 5 deletions src/main/java/io/github/classgraph/ClassInfo.java
Expand Up @@ -29,6 +29,7 @@
package io.github.classgraph;

import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.annotation.Inherited;
import java.lang.annotation.Repeatable;
import java.lang.reflect.Modifier;
Expand Down Expand Up @@ -56,6 +57,7 @@
import nonapi.io.github.classgraph.types.Parser;
import nonapi.io.github.classgraph.types.TypeUtils;
import nonapi.io.github.classgraph.types.TypeUtils.ModifierType;
import nonapi.io.github.classgraph.utils.Assert;
import nonapi.io.github.classgraph.utils.LogNode;

/** Holds metadata about a class encountered during a scan. */
Expand Down Expand Up @@ -1291,6 +1293,16 @@ public boolean isArrayClass() {
return this instanceof ArrayClassInfo;
}

/**
* Checks if this class extends the superclass.
*
* @param superclass A superclass.
* @return true if this class extends the superclass.
*/
public boolean extendsSuperclass(final Class<?> superclass) {
return extendsSuperclass(superclass.getName());
}

/**
* Checks if this class extends the named superclass.
*
Expand Down Expand Up @@ -1348,6 +1360,17 @@ public boolean isImplementedInterface() {
return relatedClasses.get(RelType.CLASSES_IMPLEMENTING) != null || isInterface();
}

/**
* Checks whether this class implements the interface.
*
* @param interfaceClazz An interface.
* @return true if this class implements the interface.
*/
public boolean implementsInterface(final Class<?> interfaceClazz) {
Assert.isInterface(interfaceClazz);
return implementsInterface(interfaceClazz.getName());
}

/**
* Checks whether this class implements the named interface.
*
Expand All @@ -1359,6 +1382,17 @@ public boolean implementsInterface(final String interfaceName) {
return getInterfaces().containsName(interfaceName);
}

/**
* Checks whether this class has the annotation.
*
* @param annotation An annotation.
* @return true if this class has the annotation.
*/
public boolean hasAnnotation(final Class<? extends Annotation> annotation) {
Assert.isAnnotation(annotation);
return hasAnnotation(annotation.getName());
}

/**
* Checks whether this class has the named annotation.
*
Expand Down Expand Up @@ -1397,6 +1431,17 @@ public boolean hasField(final String fieldName) {
return false;
}

/**
* Checks whether this class declares a field with the annotation.
*
* @param annotation A field annotation.
* @return true if this class declares a field with the annotation.
*/
public boolean hasDeclaredFieldAnnotation(final Class<? extends Annotation> annotation) {
Assert.isAnnotation(annotation);
return hasDeclaredFieldAnnotation(annotation.getName());
}

/**
* Checks whether this class declares a field with the named annotation.
*
Expand All @@ -1413,6 +1458,17 @@ public boolean hasDeclaredFieldAnnotation(final String fieldAnnotationName) {
return false;
}

/**
* Checks whether this class or one of its superclasses declares a field with the annotation.
*
* @param fieldAnnotation A field annotation.
* @return true if this class or one of its superclasses declares a field with the annotation.
*/
public boolean hasFieldAnnotation(final Class<? extends Annotation> fieldAnnotation) {
Assert.isAnnotation(fieldAnnotation);
return hasFieldAnnotation(fieldAnnotation.getName());
}

/**
* Checks whether this class or one of its superclasses declares a field with the named annotation.
*
Expand Down Expand Up @@ -1456,6 +1512,17 @@ public boolean hasMethod(final String methodName) {
return false;
}

/**
* Checks whether this class declares a method with the annotation.
*
* @param methodAnnotation A method annotation.
* @return true if this class declares a method with the annotation.
*/
public boolean hasDeclaredMethodAnnotation(final Class<? extends Annotation> methodAnnotation) {
Assert.isAnnotation(methodAnnotation);
return hasDeclaredMethodAnnotation(methodAnnotation.getName());
}

/**
* Checks whether this class declares a method with the named annotation.
*
Expand All @@ -1472,6 +1539,19 @@ public boolean hasDeclaredMethodAnnotation(final String methodAnnotationName) {
return false;
}

/**
* Checks whether this class or one of its superclasses or interfaces declares a method with the
* annotation.
*
* @param methodAnnotation A method annotation.
* @return true if this class or one of its superclasses or interfaces declares a method with the
* annotation.
*/
public boolean hasMethodAnnotation(final Class<? extends Annotation> methodAnnotation) {
Assert.isAnnotation(methodAnnotation);
return hasMethodAnnotation(methodAnnotation.getName());
}

/**
* Checks whether this class or one of its superclasses or interfaces declares a method with the named
* annotation.
Expand All @@ -1490,6 +1570,17 @@ public boolean hasMethodAnnotation(final String methodAnnotationName) {
return false;
}

/**
* Checks whether this class declares a method with the annotation.
*
* @param methodParameterAnnotation A method annotation.
* @return true if this class declares a method with the annotation.
*/
public boolean hasDeclaredMethodParameterAnnotation(final Class<? extends Annotation> methodParameterAnnotation) {
Assert.isAnnotation(methodParameterAnnotation);
return hasDeclaredMethodParameterAnnotation(methodParameterAnnotation.getName());
}

/**
* Checks whether this class declares a method with the named annotation.
*
Expand All @@ -1506,6 +1597,17 @@ public boolean hasDeclaredMethodParameterAnnotation(final String methodParameter
return false;
}

/**
* Checks whether this class or one of its superclasses or interfaces has a method with the annotation.
*
* @param methodParameterAnnotation A method annotation.
* @return true if this class or one of its superclasses or interfaces has a method with the annotation.
*/
public boolean hasMethodParameterAnnotation(final Class<? extends Annotation> methodParameterAnnotation) {
Assert.isAnnotation(methodParameterAnnotation);
return hasMethodParameterAnnotation(methodParameterAnnotation.getName());
}

/**
* Checks whether this class or one of its superclasses or interfaces has a method with the named annotation.
*
Expand Down Expand Up @@ -1837,18 +1939,41 @@ public AnnotationInfoList getAnnotationInfo() {
}

/**
* Get a the named non-{@link Repeatable} annotation on this class, or null if the class does not have the named
* Get a the non-{@link Repeatable} annotation on this class, or null if the class does not have the
* annotation. (Use {@link #getAnnotationInfoRepeatable(String)} for {@link Repeatable} annotations.)
*
* <p>
* Also handles the {@link Inherited} meta-annotation, which causes an annotation to annotate a class and all of
* its subclasses.
*
* <p>
* Note that if you need to get multiple annotations, it is faster to call {@link #getAnnotationInfo()},
* and then get the annotations from the returned {@link AnnotationInfoList}, so that the returned list
* doesn't have to be built multiple times.
*
* @param annotation
* The annotation.
* @return An {@link AnnotationInfo} object representing the annotation on this class, or null if the
* class does not have the annotation.
*/
public AnnotationInfo getAnnotationInfo(final Class<? extends Annotation> annotation) {
Assert.isAnnotation(annotation);
return getAnnotationInfo(annotation.getName());
}

/**
* Get a the named non-{@link Repeatable} annotation on this class, or null if the class does not have the named
* annotation. (Use {@link #getAnnotationInfoRepeatable(String)} for {@link Repeatable} annotations.)
*
* <p>
* Also handles the {@link Inherited} meta-annotation, which causes an annotation to annotate a class and all of
* its subclasses.
*
* <p>
* Note that if you need to get multiple named annotations, it is faster to call {@link #getAnnotationInfo()},
* and then get the named annotations from the returned {@link AnnotationInfoList}, so that the returned list
* doesn't have to be built multiple times.
*
*
* @param annotationName
* The annotation name.
* @return An {@link AnnotationInfo} object representing the named annotation on this class, or null if the
Expand All @@ -1859,18 +1984,41 @@ public AnnotationInfo getAnnotationInfo(final String annotationName) {
}

/**
* Get a the named {@link Repeatable} annotation on this class, or the empty list if the class does not have the
* named annotation.
* Get a the {@link Repeatable} annotation on this class, or the empty list if the class does not have the
* annotation.
*
* <p>
* Also handles the {@link Inherited} meta-annotation, which causes an annotation to annotate a class and all of
* its subclasses.
*
* <p>
* Note that if you need to get multiple annotations, it is faster to call {@link #getAnnotationInfo()},
* and then get the annotations from the returned {@link AnnotationInfoList}, so that the returned list
* doesn't have to be built multiple times.
*
* @param annotation
* The annotation.
* @return An {@link AnnotationInfoList} of all instances of the annotation on this class, or the empty
* list if the class does not have the annotation.
*/
public AnnotationInfoList getAnnotationInfoRepeatable(final Class<? extends Annotation> annotation) {
Assert.isAnnotation(annotation);
return getAnnotationInfoRepeatable(annotation.getName());
}

/**
* Get a the named {@link Repeatable} annotation on this class, or the empty list if the class does not have the
* named annotation.
*
* <p>
* Also handles the {@link Inherited} meta-annotation, which causes an annotation to annotate a class and all of
* its subclasses.
*
* <p>
* Note that if you need to get multiple named annotations, it is faster to call {@link #getAnnotationInfo()},
* and then get the named annotations from the returned {@link AnnotationInfoList}, so that the returned list
* doesn't have to be built multiple times.
*
*
* @param annotationName
* The annotation name.
* @return An {@link AnnotationInfoList} of all instances of the named annotation on this class, or the empty
Expand Down
45 changes: 43 additions & 2 deletions src/main/java/io/github/classgraph/FieldInfo.java
Expand Up @@ -28,6 +28,7 @@
*/
package io.github.classgraph;

import java.lang.annotation.Annotation;
import java.lang.annotation.Repeatable;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
Expand All @@ -40,6 +41,7 @@
import nonapi.io.github.classgraph.types.ParseException;
import nonapi.io.github.classgraph.types.TypeUtils;
import nonapi.io.github.classgraph.types.TypeUtils.ModifierType;
import nonapi.io.github.classgraph.utils.Assert;
import nonapi.io.github.classgraph.utils.LogNode;

/**
Expand Down Expand Up @@ -370,10 +372,24 @@ public AnnotationInfoList getAnnotationInfo() {
: AnnotationInfoList.getIndirectAnnotations(annotationInfo, /* annotatedClass = */ null);
}

/**
* Get a the non-{@link Repeatable} annotation on this field, or null if the field does not have the
* annotation. (Use {@link #getAnnotationInfoRepeatable(Class)} for {@link Repeatable} annotations.)
*
* @param annotation
* The annotation.
* @return An {@link AnnotationInfo} object representing the annotation on this field, or null if the
* field does not have the annotation.
*/
public AnnotationInfo getAnnotationInfo(final Class<? extends Annotation> annotation) {
Assert.isAnnotation(annotation);
return getAnnotationInfo(annotation.getName());
}

/**
* Get a the named non-{@link Repeatable} annotation on this field, or null if the field does not have the named
* annotation. (Use {@link #getAnnotationInfoRepeatable(String)} for {@link Repeatable} annotations.)
*
*
* @param annotationName
* The annotation name.
* @return An {@link AnnotationInfo} object representing the named annotation on this field, or null if the
Expand All @@ -383,10 +399,24 @@ public AnnotationInfo getAnnotationInfo(final String annotationName) {
return getAnnotationInfo().get(annotationName);
}

/**
* Get a the {@link Repeatable} annotation on this field, or the empty list if the field does not have the
* annotation.
*
* @param annotation
* The annotation.
* @return An {@link AnnotationInfoList} of all instances of the annotation on this field, or the empty
* list if the field does not have the annotation.
*/
public AnnotationInfoList getAnnotationInfoRepeatable(final Class<? extends Annotation> annotation) {
Assert.isAnnotation(annotation);
return getAnnotationInfoRepeatable(annotation.getName());
}

/**
* Get a the named {@link Repeatable} annotation on this field, or the empty list if the field does not have the
* named annotation.
*
*
* @param annotationName
* The annotation name.
* @return An {@link AnnotationInfoList} of all instances of the named annotation on this field, or the empty
Expand All @@ -396,6 +426,17 @@ public AnnotationInfoList getAnnotationInfoRepeatable(final String annotationNam
return getAnnotationInfo().getRepeatable(annotationName);
}

/**
* Check if the field has a given annotation.
*
* @param annotation The annotation.
* @return true if this field has the annotation.
*/
public boolean hasAnnotation(final Class<? extends Annotation> annotation) {
Assert.isAnnotation(annotation);
return hasAnnotation(annotation.getName());
}

/**
* Check if the field has a given named annotation.
*
Expand Down