diff options
author | Michajlo Matijkiw <michajlo@google.com> | 2016-09-20 19:52:07 +0000 |
---|---|---|
committer | Laszlo Csomor <laszlocsomor@google.com> | 2016-09-21 07:08:49 +0000 |
commit | 7b7fce1ebed8e34f789fdd3a0a47d741f175e4a3 (patch) | |
tree | a511355205155ea908e23dad97d39f2c25faeab9 /src/main/java/com/google/devtools/build/lib/packages/RuleClass.java | |
parent | 48ad3069fbb9b28ea035a7948c8e330e0eef5898 (diff) |
Use Object for what instead of String
The string is only used for error messaging on failure. Instead of constructing
lots of strings allow passing in a generic Object whos toString is used for the
error message. This allows us to bypass a lot of garbage string generation, at
th expense of a little indirection.
Chose Object over some specialized type, since we use inline strings frequently
as well.
--
MOS_MIGRATED_REVID=133741724
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/packages/RuleClass.java')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/packages/RuleClass.java | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/packages/RuleClass.java b/src/main/java/com/google/devtools/build/lib/packages/RuleClass.java index cba10a705f..53aabfa563 100644 --- a/src/main/java/com/google/devtools/build/lib/packages/RuleClass.java +++ b/src/main/java/com/google/devtools/build/lib/packages/RuleClass.java @@ -1727,9 +1727,11 @@ public final class RuleClass { */ private static Object convertFromBuildLangType(Rule rule, Attribute attr, Object buildLangValue) throws ConversionException { - String what = "attribute '" + attr.getName() + "' in '" + rule.getRuleClass() + "' rule"; - Object converted = - BuildType.selectableConvert(attr.getType(), buildLangValue, what, rule.getLabel()); + Object converted = BuildType.selectableConvert( + attr.getType(), + buildLangValue, + new AttributeConversionContext(attr.getName(), rule.getRuleClass()), + rule.getLabel()); if ((converted instanceof SelectorList<?>) && !attr.isConfigurable()) { throw new ConversionException( @@ -1749,6 +1751,28 @@ public final class RuleClass { } /** + * Provides a {@link #toString()} description of the attribute being converted for + * {@link BuildType#selectableConvert}. This is preferred over a raw string to avoid uselessly + * constructing strings which are never used. A separate class instead of inline to avoid + * accidental memory leaks. + */ + private static class AttributeConversionContext { + private final String attrName; + private final String ruleClass; + + AttributeConversionContext(String attrName, String ruleClass) { + this.attrName = attrName; + this.ruleClass = ruleClass; + } + + @Override + public String toString() { + return "attribute '" + attrName + "' in '" + ruleClass + "' rule"; + } + } + + + /** * Verifies that the rule has a valid value for the attribute according to its allowed values. * * <p>If the value for the given attribute on the given rule is invalid, an error will be recorded |