JavaParser Source Viewer

Home|JavaParser/com/github/javaparser/metamodel/PropertyMetaModel.java
1/*
2 * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser.
3 * Copyright (C) 2011, 2013-2020 The JavaParser Team.
4 *
5 * This file is part of JavaParser.
6 *
7 * JavaParser can be used either under the terms of
8 * a) the GNU Lesser General Public License as published by
9 *     the Free Software Foundation, either version 3 of the License, or
10 *     (at your option) any later version.
11 * b) the terms of the Apache License
12 *
13 * You should have received a copy of both licenses in LICENCE.LGPL and
14 * LICENCE.APACHE. Please refer to those files for details.
15 *
16 * JavaParser is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU Lesser General Public License for more details.
20 */
21
22package com.github.javaparser.metamodel;
23
24import com.github.javaparser.ast.Node;
25
26import java.lang.reflect.Field;
27import java.util.Optional;
28
29import static com.github.javaparser.utils.CodeGenerationUtils.getterName;
30import static com.github.javaparser.utils.CodeGenerationUtils.setterName;
31
32/**
33 * Meta-data about a property of a node in the AST.
34 */
35public class PropertyMetaModel {
36    private final BaseNodeMetaModel containingNodeMetaModel;
37    private final String name;
38    private final Class<?> type;
39    private final Optional<BaseNodeMetaModelnodeReference;
40    private final boolean isOptional;
41    private final boolean isNonEmpty;
42    private final boolean isNodeList;
43    private final boolean hasWildcard;
44
45    public PropertyMetaModel(BaseNodeMetaModel containingNodeMetaModelString nameClass<?> typeOptional<BaseNodeMetaModelnodeReferenceboolean isOptionalboolean isNonEmptyboolean isNodeListboolean hasWildcard) {
46        this.containingNodeMetaModel = containingNodeMetaModel;
47        this.name = name;
48        this.type = type;
49        this.nodeReference = nodeReference;
50        this.isOptional = isOptional;
51        this.isNonEmpty = isNonEmpty;
52        this.isNodeList = isNodeList;
53        this.hasWildcard = hasWildcard;
54    }
55
56    /**
57     * @return is this the field fieldName on class c?
58     */
59    public boolean is(Class<? extends NodecString fieldName) {
60        return containingNodeMetaModel.is(c) && name.equals(fieldName);
61    }
62
63    /**
64     * @return is this fields called fieldName?
65     */
66    public boolean is(String fieldName) {
67        return name.equals(fieldName);
68    }
69
70    /**
71     * @return the name used in the AST for the setter
72     */
73    public String getSetterMethodName() {
74        return setterName(name);
75    }
76
77    /**
78     * @return the name used in the AST for the getter
79     */
80    public String getGetterMethodName() {
81        return getterName(typename);
82    }
83
84    /**
85     * @return the NodeMetaModel that "has" this property.
86     */
87    public BaseNodeMetaModel getContainingNodeMetaModel() {
88        return containingNodeMetaModel;
89    }
90
91    /**
92     * @return the name of the property. This is equal to the name of the field in the AST.
93     */
94    public String getName() {
95        return name;
96    }
97
98    /**
99     * @return if this property is a String or a NodeList: whether it may be empty.
100     */
101    public boolean isNonEmpty() {
102        return isNonEmpty;
103    }
104
105    /**
106     * @return the class of the field.
107     */
108    public Class<?> getType() {
109        return type;
110    }
111
112    /**
113     * @return if this property is a Node, this will get the node meta model.
114     */
115    public Optional<BaseNodeMetaModelgetNodeReference() {
116        return nodeReference;
117    }
118
119    /**
120     * @return whether this property is optional.
121     */
122    public boolean isOptional() {
123        return isOptional;
124    }
125
126    /**
127     * @return whether this property is not optional.
128     */
129    public boolean isRequired() {
130        return !isOptional;
131    }
132
133    /**
134     * @return whether this property is contained in a NodeList.
135     */
136    public boolean isNodeList() {
137        return isNodeList;
138    }
139
140    /**
141     * @return whether this property has a wildcard following it, like BodyDeclaration&lt;?&gt;.
142     */
143    public boolean hasWildcard() {
144        return hasWildcard;
145    }
146
147    /**
148     * @return whether this property is not a list or set.
149     */
150    public boolean isSingular() {
151        return !isNodeList;
152    }
153
154    @Override
155    public String toString() {
156        return "(" + getTypeName() + ")\t" + containingNodeMetaModel + "#" + name;
157    }
158
159    @Override
160    public boolean equals(Object o) {
161        if (this == o) return true;
162        if (o == null || getClass() != o.getClass()) return false;
163
164        PropertyMetaModel that = (PropertyMetaModelo;
165
166        if (!name.equals(that.name)) return false;
167        if (!type.equals(that.type)) return false;
168
169        return true;
170    }
171
172    @Override
173    public int hashCode() {
174        int result = name.hashCode();
175        result = 31 * result + type.hashCode();
176        return result;
177    }
178
179    /**
180     * @return the type of a single element of this property, so no Optional or NodeList.
181     */
182    public String getTypeNameGenerified() {
183        if (hasWildcard) {
184            return getTypeName() + "<?>";
185        }
186        return getTypeName();
187    }
188
189    /**
190     * @return the raw type of a single element of this property, so nothing but the name.
191     */
192    public String getTypeName() {
193        return type.getSimpleName();
194    }
195
196    /**
197     * @return the type that is returned from getters in the AST.
198     */
199    public String getTypeNameForGetter() {
200        if (isOptional) {
201            return "Optional<" + getTypeNameForSetter() + ">";
202        }
203        return getTypeNameForSetter();
204    }
205
206    /**
207     * @return the type that is passed to setters in the AST.
208     */
209    public String getTypeNameForSetter() {
210        if (isNodeList) {
211            return "NodeList<" + getTypeNameGenerified() + ">";
212        }
213        return getTypeNameGenerified();
214    }
215
216    /**
217     * @return is this property an AST Node?
218     */
219    public boolean isNode() {
220        return getNodeReference().isPresent();
221    }
222
223    /**
224     * The name of the field in the containing BaseNodeMetaModel for this property meta model.
225     */
226    public String getMetaModelFieldName() {
227        return getName() + "PropertyMetaModel";
228    }
229
230    /**
231     * @return is this property an attribute, meaning: not a node?
232     */
233    public boolean isAttribute() {
234        return !isNode();
235    }
236
237    /**
238     * Introspects the node to get the value from this field.
239     * Note that an optional empty field will return null here.
240     */
241    public Object getValue(Node node) {
242        try {
243            for (Class<?> c = node.getClass(); c != nullc = c.getSuperclass()) {
244                Field[] fields = c.getDeclaredFields();
245                for (Field classField : fields) {
246                    if (classField.getName().equals(getName())) {
247                        classField.setAccessible(true);
248                        return classField.get(node);
249                    }
250                }
251            }
252            throw new NoSuchFieldError(getName());
253        } catch (IllegalAccessException e) {
254            throw new RuntimeException(e);
255        }
256    }
257}
258
MembersX
PropertyMetaModel:isSingular
PropertyMetaModel:getType
PropertyMetaModel:hashCode
PropertyMetaModel:getNodeReference
PropertyMetaModel:name
PropertyMetaModel:PropertyMetaModel
PropertyMetaModel:toString
PropertyMetaModel:nodeReference
PropertyMetaModel:equals:Block:that
PropertyMetaModel:type
PropertyMetaModel:hasWildcard
PropertyMetaModel:getGetterMethodName
PropertyMetaModel:equals
PropertyMetaModel:getValue:Block:Block:Block:fields
PropertyMetaModel:getName
PropertyMetaModel:is
PropertyMetaModel:isNonEmpty
PropertyMetaModel:getTypeNameForSetter
PropertyMetaModel:getSetterMethodName
PropertyMetaModel:getContainingNodeMetaModel
PropertyMetaModel:getValue
PropertyMetaModel:isNode
PropertyMetaModel:isRequired
PropertyMetaModel:containingNodeMetaModel
PropertyMetaModel:getTypeNameGenerified
PropertyMetaModel:hashCode:Block:result
PropertyMetaModel:getTypeNameForGetter
PropertyMetaModel:getMetaModelFieldName
PropertyMetaModel:isAttribute
PropertyMetaModel:isNodeList
PropertyMetaModel:isOptional
PropertyMetaModel:getTypeName
Members
X