diff options
author | 2016-09-12 14:35:41 +0000 | |
---|---|---|
committer | 2016-09-12 17:09:22 +0000 | |
commit | 0cfbcf8ac356ee681331c1b04290074f5d220d1f (patch) | |
tree | 182c5410d5b888201215c167cfa2313d10a9a3ca /src/main/java/com/google/devtools/build/lib/rules/repository/WorkspaceAttributeMapper.java | |
parent | d85fd98f4103cb9f1f1015a606757d08d2ef2cee (diff) |
Don't print stack traces when repository rules have the wrong type
This also entirely disallows select() in repository rules. All repository rules should now error out if the wrong type is given, instead of printing a stack trace.
Fixes #1307.
--
MOS_MIGRATED_REVID=132872804
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/rules/repository/WorkspaceAttributeMapper.java')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/rules/repository/WorkspaceAttributeMapper.java | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/repository/WorkspaceAttributeMapper.java b/src/main/java/com/google/devtools/build/lib/rules/repository/WorkspaceAttributeMapper.java new file mode 100644 index 0000000000..b2edf3d96c --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/rules/repository/WorkspaceAttributeMapper.java @@ -0,0 +1,76 @@ +// Copyright 2016 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.rules.repository; + +import com.google.devtools.build.lib.cmdline.Label; +import com.google.devtools.build.lib.packages.AggregatingAttributeMapper; +import com.google.devtools.build.lib.packages.BuildType.SelectorList; +import com.google.devtools.build.lib.packages.Rule; +import com.google.devtools.build.lib.syntax.EvalException; +import com.google.devtools.build.lib.syntax.Type; + +/** + * An attribute mapper for workspace rules. Similar to NonconfigurableAttributeWrapper, but throws + * a wrapped EvalException if select() is used. + */ +public class WorkspaceAttributeMapper { + + public static WorkspaceAttributeMapper of(Rule rule) { + return new WorkspaceAttributeMapper(rule); + } + + private final Rule rule; + + private WorkspaceAttributeMapper(Rule rule) { + this.rule = rule; + } + + public <T> T get(String attributeName, Type<T> type) throws EvalException { + Object value = getObject(attributeName); + try { + return type.cast(value); + } catch (ClassCastException e) { + throw new EvalException( + rule.getAttributeContainer().getAttributeLocation(attributeName), e.getMessage()); + } + } + + /** + * Returns the value for an attribute without casting it to any particular type. + */ + public Object getObject(String attributeName) throws EvalException { + Object value = rule.getAttributeContainer().getAttr(attributeName); + if (value instanceof SelectorList) { + String message; + if (rule.getLocation().getPath().getBaseName().equals( + Label.EXTERNAL_PACKAGE_FILE_NAME.getPathString())) { + message = "select() cannot be used in WORKSPACE files"; + } else { + message = "select() cannot be used in macros called from WORKSPACE files"; + } + throw new EvalException( + rule.getAttributeContainer().getAttributeLocation(attributeName), message); + } + return value; + } + + public boolean isAttributeValueExplicitlySpecified(String attr) { + return rule.getAttributeContainer().isAttributeValueExplicitlySpecified(attr); + } + + public Iterable<String> getAttributeNames() { + return AggregatingAttributeMapper.of(rule).getAttributeNames(); + } +} |