diff options
author | 2018-04-16 12:58:03 -0700 | |
---|---|---|
committer | 2018-04-16 13:01:41 -0700 | |
commit | fcea9dec559bca165c08a36d4c8c8a20c1840755 (patch) | |
tree | 41375a79789279315ff08ccc8524f55768f2c887 /src/main/java/com/google/devtools/build/lib/skylarkinterface | |
parent | 65e1fa81add2c40a5434d26c7a46a1ef9b249981 (diff) |
Create @SkylarkGlobalLibrary annotation, allowing classes/interfaces with global skylark functions to use @SkylarkCallable instead of @SkylarkSignature.
Also migrate skylark's global "rule" function to @SkylarkCallable, thus demonstrating the new feature.
RELNOTES: None.
PiperOrigin-RevId: 193085313
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/skylarkinterface')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/skylarkinterface/SkylarkGlobalLibrary.java | 32 | ||||
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/skylarkinterface/SkylarkInterfaceUtils.java | 4 |
2 files changed, 35 insertions, 1 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/skylarkinterface/SkylarkGlobalLibrary.java b/src/main/java/com/google/devtools/build/lib/skylarkinterface/SkylarkGlobalLibrary.java new file mode 100644 index 0000000000..1ee6388394 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/skylarkinterface/SkylarkGlobalLibrary.java @@ -0,0 +1,32 @@ +// Copyright 2018 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package com.google.devtools.build.lib.skylarkinterface; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * An annotation to mark a class whose methods are global (top-level) Skylark functions. + * + * <p>A class or interface annotated with this annotation indicates that all of its methods + * which are annotated with {@link SkylarkCallable} should be treated as global top-level + * functions. + * + * <p>Global libraries should be stateless, and must have a public zero-arg constructor. + */ +@Target({ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +public @interface SkylarkGlobalLibrary {} 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 e3751a8494..2c269a74d9 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 @@ -86,7 +86,9 @@ public class SkylarkInterfaceUtils { public static SkylarkCallable getSkylarkCallable(Class<?> classObj, Method method) { try { Method superMethod = classObj.getMethod(method.getName(), method.getParameterTypes()); - if (classObj.isAnnotationPresent(SkylarkModule.class) + boolean classAnnotatedForCallables = classObj.isAnnotationPresent(SkylarkModule.class) + || classObj.isAnnotationPresent(SkylarkGlobalLibrary.class); + if (classAnnotatedForCallables && superMethod.isAnnotationPresent(SkylarkCallable.class)) { return superMethod.getAnnotation(SkylarkCallable.class); } |