|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.painless.node; |
| 21 | + |
| 22 | +import org.elasticsearch.painless.Location; |
| 23 | +import org.elasticsearch.painless.lookup.PainlessLookup; |
| 24 | +import org.elasticsearch.painless.lookup.PainlessLookupUtility; |
| 25 | + |
| 26 | +import java.util.Objects; |
| 27 | + |
| 28 | +/** |
| 29 | + * Represents a Painless type as a {@link Class}. This may still |
| 30 | + * require resolution to ensure the type in the {@link PainlessLookup}. |
| 31 | + */ |
| 32 | +public class DResolvedType extends DType { |
| 33 | + |
| 34 | + protected final Class<?> type; |
| 35 | + |
| 36 | + /** |
| 37 | + * If set to {@code true} ensures the type is in the {@link PainlessLookup}. |
| 38 | + * If set to {@code false} assumes the type is valid. |
| 39 | + */ |
| 40 | + protected final boolean checkInLookup; |
| 41 | + |
| 42 | + public DResolvedType(Location location, Class<?> type) { |
| 43 | + this(location, type, true); |
| 44 | + } |
| 45 | + |
| 46 | + public DResolvedType(Location location, Class<?> type, boolean checkInLookup) { |
| 47 | + super(location); |
| 48 | + this.type = Objects.requireNonNull(type); |
| 49 | + this.checkInLookup = checkInLookup; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * If {@link #checkInLookup} is {@code true} checks if the type is in the |
| 54 | + * {@link PainlessLookup}, otherwise returns {@code this}. |
| 55 | + * @throws IllegalArgumentException if both checking the type is in the {@link PainlessLookup} |
| 56 | + * and the type cannot be resolved from the {@link PainlessLookup} |
| 57 | + * @return a {@link DResolvedType} where the resolved Painless type is retrievable |
| 58 | + */ |
| 59 | + @Override |
| 60 | + public DResolvedType resolveType(PainlessLookup painlessLookup) { |
| 61 | + if (checkInLookup == false) { |
| 62 | + return this; |
| 63 | + } |
| 64 | + |
| 65 | + if (painlessLookup.getClasses().contains(type) == false) { |
| 66 | + throw location.createError(new IllegalArgumentException( |
| 67 | + "cannot resolve type [" + PainlessLookupUtility.typeToCanonicalTypeName(type) + "]")); |
| 68 | + } |
| 69 | + |
| 70 | + return new DResolvedType(location, type, false); |
| 71 | + } |
| 72 | + |
| 73 | + public Class<?> getType() { |
| 74 | + return type; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public String toString() { |
| 79 | + return " (DResolvedType [" + PainlessLookupUtility.typeToCanonicalTypeName(type) + "])"; |
| 80 | + } |
| 81 | +} |
0 commit comments