aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/skylarkinterface
diff options
context:
space:
mode:
authorGravatar Jon Brandvein <brandjon@google.com>2016-10-12 20:37:54 +0000
committerGravatar Yue Gan <yueg@google.com>2016-10-13 08:53:25 +0000
commit65400725553a8b133157d2ddf1a2330de908e9bd (patch)
treea3f39ef12d208eaa70c4f3d57d435063a405d29e /src/main/java/com/google/devtools/build/lib/skylarkinterface
parent214ec35e7e8d10a65ad111a2ce5c9be84d865a26 (diff)
Refactor getParentWithSkylarkModule() into SkylarkInterfaceUtils
-- MOS_MIGRATED_REVID=135956016
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.java45
1 files changed, 32 insertions, 13 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 348a9ccef4..bee73efaf8 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 com.google.devtools.build.lib.util.Pair;
import java.lang.reflect.Method;
import javax.annotation.Nullable;
@@ -22,33 +23,51 @@ import javax.annotation.Nullable;
*/
public class SkylarkInterfaceUtils {
- /**
- * Returns the {@link SkylarkModule} annotation for the given class, if it exists, and
- * null otherwise. The first annotation found will be returned, starting with {@code classObj}
- * and following its base classes and interfaces recursively.
- */
@Nullable
- public static SkylarkModule getSkylarkModule(Class<?> classObj) {
+ private static Pair<Class<?>, SkylarkModule> searchForSkylarkModule(Class<?> classObj) {
if (classObj.isAnnotationPresent(SkylarkModule.class)) {
- return classObj.getAnnotation(SkylarkModule.class);
+ return new Pair<Class<?>, SkylarkModule>(
+ classObj, classObj.getAnnotation(SkylarkModule.class));
}
Class<?> superclass = classObj.getSuperclass();
if (superclass != null) {
- SkylarkModule annotation = getSkylarkModule(superclass);
- if (annotation != null) {
- return annotation;
+ Pair<Class<?>, SkylarkModule> result = searchForSkylarkModule(superclass);
+ if (result != null) {
+ return result;
}
}
for (Class<?> interfaceObj : classObj.getInterfaces()) {
- SkylarkModule annotation = getSkylarkModule(interfaceObj);
- if (annotation != null) {
- return annotation;
+ Pair<Class<?>, SkylarkModule> result = searchForSkylarkModule(interfaceObj);
+ if (result != null) {
+ return result;
}
}
return null;
}
/**
+ * Returns the {@link SkylarkModule} annotation for the given class, if it exists, and
+ * null otherwise. The first annotation found will be returned, starting with {@code classObj}
+ * and following its base classes and interfaces recursively.
+ */
+ @Nullable
+ public static SkylarkModule getSkylarkModule(Class<?> classObj) {
+ Pair<Class<?>, SkylarkModule> result = searchForSkylarkModule(classObj);
+ return result == null ? null : result.second;
+ }
+
+ /**
+ * Searches {@code classObj}'s class hierarchy and returns the first superclass or interface that
+ * is annotated with {@link SkylarkModule} (including possibly {@code classObj} itself), or null
+ * if none is found.
+ */
+ @Nullable
+ public static Class<?> getParentWithSkylarkModule(Class<?> classObj) {
+ Pair<Class<?>, SkylarkModule> result = searchForSkylarkModule(classObj);
+ return result == null ? null : result.first;
+ }
+
+ /**
* 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