dslomov | f129657 | 2017-08-22 16:29:06 +0200 | [diff] [blame] | 1 | // Copyright 2017 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package com.google.devtools.build.lib.packages; |
| 15 | |
| 16 | import com.google.common.collect.ImmutableCollection; |
| 17 | import com.google.common.collect.ImmutableMap; |
cparsons | 6c1c066 | 2018-02-05 02:01:28 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
dslomov | f129657 | 2017-08-22 16:29:06 +0200 | [diff] [blame] | 19 | import com.google.devtools.build.lib.events.Location; |
cparsons | 6c1c066 | 2018-02-05 02:01:28 -0800 | [diff] [blame] | 20 | import com.google.devtools.build.lib.syntax.EvalException; |
| 21 | import com.google.devtools.build.lib.syntax.FuncallExpression; |
| 22 | import com.google.devtools.build.lib.syntax.FuncallExpression.MethodDescriptor; |
dslomov | f129657 | 2017-08-22 16:29:06 +0200 | [diff] [blame] | 23 | import java.util.Map; |
| 24 | |
| 25 | /** Base class for native implementations of {@link Info}. */ |
| 26 | // todo(vladmos,dslomov): make abstract once DefaultInfo stops instantiating it. |
| 27 | public class NativeInfo extends Info { |
| 28 | protected final ImmutableMap<String, Object> values; |
| 29 | |
cparsons | 6c1c066 | 2018-02-05 02:01:28 -0800 | [diff] [blame] | 30 | // Initialized lazily. |
| 31 | private ImmutableSet<String> fieldNames; |
| 32 | |
dslomov | f129657 | 2017-08-22 16:29:06 +0200 | [diff] [blame] | 33 | @Override |
cparsons | 6c1c066 | 2018-02-05 02:01:28 -0800 | [diff] [blame] | 34 | public Object getValue(String name) throws EvalException { |
| 35 | if (values.containsKey(name)) { |
| 36 | return values.get(name); |
| 37 | } else if (hasField(name)) { |
| 38 | MethodDescriptor methodDescriptor = FuncallExpression.getStructField(this.getClass(), name); |
cparsons | 4803293 | 2018-04-18 09:39:02 -0700 | [diff] [blame] | 39 | try { |
| 40 | return FuncallExpression.invokeStructField(methodDescriptor, name, this); |
| 41 | } catch (InterruptedException exception) { |
| 42 | // Struct fields on NativeInfo objects are supposed to behave well and not throw |
| 43 | // exceptions, as they should be logicless field accessors. If this occurs, it's |
| 44 | // indicative of a bad NativeInfo implementation. |
| 45 | throw new IllegalStateException( |
| 46 | String.format("Access of field %s was unexpectedly interrupted, but should be " |
| 47 | + "uninterruptible. This is indicative of a bad provider implementation.", name)); |
| 48 | } |
cparsons | 6c1c066 | 2018-02-05 02:01:28 -0800 | [diff] [blame] | 49 | } else { |
| 50 | return null; |
| 51 | } |
dslomov | f129657 | 2017-08-22 16:29:06 +0200 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | @Override |
brandjon | d331fa7 | 2017-12-28 07:38:31 -0800 | [diff] [blame] | 55 | public boolean hasField(String name) { |
cparsons | 6c1c066 | 2018-02-05 02:01:28 -0800 | [diff] [blame] | 56 | return getFieldNames().contains(name); |
dslomov | f129657 | 2017-08-22 16:29:06 +0200 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | @Override |
brandjon | d331fa7 | 2017-12-28 07:38:31 -0800 | [diff] [blame] | 60 | public ImmutableCollection<String> getFieldNames() { |
cparsons | 6c1c066 | 2018-02-05 02:01:28 -0800 | [diff] [blame] | 61 | if (fieldNames == null) { |
| 62 | fieldNames = ImmutableSet.<String>builder() |
| 63 | .addAll(values.keySet()) |
| 64 | .addAll(FuncallExpression.getStructFieldNames(this.getClass())) |
| 65 | .build(); |
| 66 | } |
| 67 | return fieldNames; |
dslomov | f129657 | 2017-08-22 16:29:06 +0200 | [diff] [blame] | 68 | } |
| 69 | |
cparsons | 2415cb4 | 2018-05-01 10:32:30 -0700 | [diff] [blame] | 70 | public NativeInfo(Provider provider) { |
cparsons | e70aafe | 2018-02-28 12:16:38 -0800 | [diff] [blame] | 71 | this(provider, Location.BUILTIN); |
dslomov | f129657 | 2017-08-22 16:29:06 +0200 | [diff] [blame] | 72 | } |
| 73 | |
cparsons | 2415cb4 | 2018-05-01 10:32:30 -0700 | [diff] [blame] | 74 | public NativeInfo(Provider provider, Location loc) { |
cparsons | e70aafe | 2018-02-28 12:16:38 -0800 | [diff] [blame] | 75 | this(provider, ImmutableMap.of(), loc); |
| 76 | } |
| 77 | |
cparsons | dcd0abc | 2018-06-21 10:13:53 -0700 | [diff] [blame] | 78 | // TODO(cparsons): Remove this constructor once ToolchainInfo stops using it. |
cparsons | e70aafe | 2018-02-28 12:16:38 -0800 | [diff] [blame] | 79 | @Deprecated |
cparsons | 2415cb4 | 2018-05-01 10:32:30 -0700 | [diff] [blame] | 80 | public NativeInfo(Provider provider, Map<String, Object> values, Location loc) { |
brandjon | b549457 | 2018-01-11 13:35:54 -0800 | [diff] [blame] | 81 | super(provider, loc); |
dslomov | f129657 | 2017-08-22 16:29:06 +0200 | [diff] [blame] | 82 | this.values = copyValues(values); |
| 83 | } |
dslomov | f129657 | 2017-08-22 16:29:06 +0200 | [diff] [blame] | 84 | } |