aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/skylarkinterface
diff options
context:
space:
mode:
authorGravatar cparsons <cparsons@google.com>2018-04-23 12:04:09 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-04-23 12:05:19 -0700
commit981f65bf0a53239c8d5d55a1469c17721a12b2f4 (patch)
tree097d8c6faea51729bac0e9aee95b2d38eacbcd07 /src/main/java/com/google/devtools/build/lib/skylarkinterface
parenta4852b5fbf09c40bfebfec6ed231ebeb11950194 (diff)
Start refactoring elements of the Skylark Build API into their own package.
RELNOTES: None. PiperOrigin-RevId: 193962460
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/skylarkinterface')
-rw-r--r--src/main/java/com/google/devtools/build/lib/skylarkinterface/SkylarkInterfaceUtils.java40
1 files changed, 28 insertions, 12 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/skylarkinterface/SkylarkInterfaceUtils.java b/src/main/java/com/google/devtools/build/lib/skylarkinterface/SkylarkInterfaceUtils.java
index 2c269a74d9..3ee297993a 100644
--- a/src/main/java/com/google/devtools/build/lib/skylarkinterface/SkylarkInterfaceUtils.java
+++ b/src/main/java/com/google/devtools/build/lib/skylarkinterface/SkylarkInterfaceUtils.java
@@ -14,6 +14,7 @@
package com.google.devtools.build.lib.skylarkinterface;
+import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import javax.annotation.Nullable;
@@ -22,30 +23,32 @@ import javax.annotation.Nullable;
*/
public class SkylarkInterfaceUtils {
- private static final class ClassAndSkylarkModule {
+ private static final class ClassWithAnnotation<T extends Annotation> {
final Class<?> klass;
- final SkylarkModule skylarkModule;
+ final T annotation;
- ClassAndSkylarkModule(Class<?> klass, SkylarkModule skylarkModule) {
+ ClassWithAnnotation(Class<?> klass, T annotation) {
this.klass = klass;
- this.skylarkModule = skylarkModule;
+ this.annotation = annotation;
}
}
@Nullable
- private static ClassAndSkylarkModule searchForSkylarkModule(Class<?> classObj) {
- if (classObj.isAnnotationPresent(SkylarkModule.class)) {
- return new ClassAndSkylarkModule(classObj, classObj.getAnnotation(SkylarkModule.class));
+ private static <T extends Annotation> ClassWithAnnotation<T> searchForClassAnnotation(
+ Class<?> classObj,
+ Class<T> annotationClass) {
+ if (classObj.isAnnotationPresent(annotationClass)) {
+ return new ClassWithAnnotation<T>(classObj, classObj.getAnnotation(annotationClass));
}
Class<?> superclass = classObj.getSuperclass();
if (superclass != null) {
- ClassAndSkylarkModule result = searchForSkylarkModule(superclass);
+ ClassWithAnnotation<T> result = searchForClassAnnotation(superclass, annotationClass);
if (result != null) {
return result;
}
}
for (Class<?> interfaceObj : classObj.getInterfaces()) {
- ClassAndSkylarkModule result = searchForSkylarkModule(interfaceObj);
+ ClassWithAnnotation<T> result = searchForClassAnnotation(interfaceObj, annotationClass);
if (result != null) {
return result;
}
@@ -60,8 +63,9 @@ public class SkylarkInterfaceUtils {
*/
@Nullable
public static SkylarkModule getSkylarkModule(Class<?> classObj) {
- ClassAndSkylarkModule result = searchForSkylarkModule(classObj);
- return result == null ? null : result.skylarkModule;
+ ClassWithAnnotation<SkylarkModule> result =
+ searchForClassAnnotation(classObj, SkylarkModule.class);
+ return result == null ? null : result.annotation;
}
/**
@@ -71,11 +75,23 @@ public class SkylarkInterfaceUtils {
*/
@Nullable
public static Class<?> getParentWithSkylarkModule(Class<?> classObj) {
- ClassAndSkylarkModule result = searchForSkylarkModule(classObj);
+ ClassWithAnnotation<SkylarkModule> result =
+ searchForClassAnnotation(classObj, SkylarkModule.class);
return result == null ? null : result.klass;
}
/**
+ * Searches {@code classObj}'s class hierarchy and for a superclass or interface that
+ * is annotated with {@link SkylarkGlobalLibrary} (including possibly {@code classObj} itself),
+ * and returns true if one is found.
+ */
+ public static boolean hasSkylarkGlobalLibrary(Class<?> classObj) {
+ ClassWithAnnotation<SkylarkGlobalLibrary> result =
+ searchForClassAnnotation(classObj, SkylarkGlobalLibrary.class);
+ return result != null;
+ }
+
+ /**
* Returns the {@link SkylarkCallable} annotation for the given method, if it exists, and
* null otherwise. The first annotation of an overridden version of the method that is found
* will be returned, starting with {@code classObj} and following its base classes and