diff options
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/analysis/skylark')
2 files changed, 31 insertions, 27 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkLateBoundDefault.java b/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkLateBoundDefault.java index bf91cafbab..139f8656cb 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkLateBoundDefault.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkLateBoundDefault.java @@ -28,6 +28,7 @@ import com.google.devtools.build.lib.packages.AttributeMap; import com.google.devtools.build.lib.packages.Rule; import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; import com.google.devtools.build.lib.skylarkbuildapi.LateBoundDefaultApi; +import com.google.devtools.build.lib.skylarkinterface.SkylarkInterfaceUtils; import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; import com.google.devtools.build.lib.skylarkinterface.SkylarkPrinter; import java.lang.reflect.InvocationTargetException; @@ -192,27 +193,31 @@ public class SkylarkLateBoundDefault<FragmentT> extends AbstractLabelLateBoundDe ImmutableMap.Builder<String, SkylarkLateBoundDefault<?>> lateBoundDefaultMap = new ImmutableMap.Builder<>(); Class<?> fragmentClass = key.fragmentClass; - String fragmentName = SkylarkModule.Resolver.resolveName(fragmentClass); - for (Method method : fragmentClass.getMethods()) { - if (method.isAnnotationPresent(SkylarkConfigurationField.class)) { - // TODO(b/68817606): Use annotation processors to verify these constraints. - Preconditions.checkArgument( - method.getReturnType() == Label.class, - String.format("Method %s must have return type 'Label'", method)); - Preconditions.checkArgument( - method.getParameterTypes().length == 0, - String.format("Method %s must not accept arguments", method)); + SkylarkModule fragmentModule = + SkylarkInterfaceUtils.getSkylarkModule(fragmentClass); - SkylarkConfigurationField configField = - method.getAnnotation(SkylarkConfigurationField.class); - lateBoundDefaultMap.put( - configField.name(), - new SkylarkLateBoundDefault<>( - configField, - fragmentClass, - fragmentName, - method, - key.toolsRepository)); + if (fragmentModule != null) { + for (Method method : fragmentClass.getMethods()) { + if (method.isAnnotationPresent(SkylarkConfigurationField.class)) { + // TODO(b/68817606): Use annotation processors to verify these constraints. + Preconditions.checkArgument( + method.getReturnType() == Label.class, + String.format("Method %s must have return type 'Label'", method)); + Preconditions.checkArgument( + method.getParameterTypes().length == 0, + String.format("Method %s must not accept arguments", method)); + + SkylarkConfigurationField configField = + method.getAnnotation(SkylarkConfigurationField.class); + lateBoundDefaultMap.put( + configField.name(), + new SkylarkLateBoundDefault<>( + configField, + fragmentClass, + fragmentModule.name(), + method, + key.toolsRepository)); + } } } return lateBoundDefaultMap.build(); diff --git a/src/main/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/SkylarkConfigurationFieldProcessor.java b/src/main/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/SkylarkConfigurationFieldProcessor.java index fc4ff11b02..8f7b998269 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/SkylarkConfigurationFieldProcessor.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/SkylarkConfigurationFieldProcessor.java @@ -15,7 +15,6 @@ package com.google.devtools.build.lib.analysis.skylark.annotations.processor; import com.google.devtools.build.lib.analysis.skylark.annotations.SkylarkConfigurationField; -import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; import java.util.Set; import javax.annotation.processing.AbstractProcessor; import javax.annotation.processing.Messager; @@ -38,7 +37,7 @@ import javax.tools.Diagnostic; * * <p>Checks the following invariants about {@link SkylarkConfigurationField}-annotated methods: * <ul> - * <li>The annotated method must be on a configuration fragment exposed to skylark.</li> + * <li>The annotated method must be on a configuration fragment.</li> * <li>The method must have return type Label.</li> * <li>The method must be public.</li> * <li>The method must have zero arguments.</li> @@ -52,6 +51,7 @@ public final class SkylarkConfigurationFieldProcessor extends AbstractProcessor private Messager messager; private Types typeUtils; + private Elements elementUtils; private TypeMirror labelType; private TypeMirror configurationFragmentType; @@ -65,7 +65,7 @@ public final class SkylarkConfigurationFieldProcessor extends AbstractProcessor super.init(processingEnv); messager = processingEnv.getMessager(); typeUtils = processingEnv.getTypeUtils(); - Elements elementUtils = processingEnv.getElementUtils(); + elementUtils = processingEnv.getElementUtils(); labelType = elementUtils.getTypeElement("com.google.devtools.build.lib.cmdline.Label").asType(); configurationFragmentType = @@ -82,7 +82,7 @@ public final class SkylarkConfigurationFieldProcessor extends AbstractProcessor if (!isMethodOfSkylarkExposedConfigurationFragment(methodElement)) { error(methodElement, "@SkylarkConfigurationField annotated methods must be methods " - + "of configuration fragments with the @SkylarkModule annotation."); + + "of configuration fragments."); } if (!typeUtils.isSameType(methodElement.getReturnType(), labelType)) { error(methodElement, "@SkylarkConfigurationField annotated methods must return Label."); @@ -104,6 +104,7 @@ public final class SkylarkConfigurationFieldProcessor extends AbstractProcessor private boolean isMethodOfSkylarkExposedConfigurationFragment( ExecutableElement methodElement) { + if (methodElement.getEnclosingElement().getKind() != ElementKind.CLASS) { return false; } @@ -111,9 +112,7 @@ public final class SkylarkConfigurationFieldProcessor extends AbstractProcessor if (!typeUtils.isAssignable(classElement.asType(), configurationFragmentType)) { return false; } - if (classElement.getAnnotation(SkylarkModule.class) == null) { - return false; - } + return true; } |