aboutsummaryrefslogtreecommitdiffstats
path: root/javaparser-core/src/main/java/com/github/javaparser/ast/observer/ObservableProperty.java
blob: d7f82f36ce122ea4416404ddac7fd0d1c69e12fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
 * Copyright (C) 2007-2010 Júlio Vilmar Gesser.
 * Copyright (C) 2011, 2013-2016 The JavaParser Team.
 *
 * This file is part of JavaParser.
 *
 * JavaParser can be used either under the terms of
 * a) the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * b) the terms of the Apache License
 *
 * You should have received a copy of both licenses in LICENCE.LGPL and
 * LICENCE.APACHE. Please refer to those files for details.
 *
 * JavaParser is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 */
package com.github.javaparser.ast.observer;

import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.utils.Utils;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Optional;
import java.util.Arrays;
import javax.annotation.Generated;

/**
 * Properties considered by the AstObserver
 */
@Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
public enum ObservableProperty {

    ANNOTATIONS(Type.MULTIPLE_REFERENCE),
    ANONYMOUS_CLASS_BODY(Type.MULTIPLE_REFERENCE),
    ARGUMENTS(Type.MULTIPLE_REFERENCE),
    ASTERISK(Type.SINGLE_ATTRIBUTE),
    BODY(Type.SINGLE_REFERENCE),
    CATCH_CLAUSES(Type.MULTIPLE_REFERENCE),
    CHECK(Type.SINGLE_REFERENCE),
    CLASS_BODY(Type.MULTIPLE_REFERENCE),
    CLASS_DECLARATION(Type.SINGLE_REFERENCE),
    CLASS_EXPR(Type.SINGLE_REFERENCE),
    COMMENT(Type.SINGLE_REFERENCE),
    COMPARE(Type.SINGLE_REFERENCE),
    COMPONENT_TYPE(Type.SINGLE_REFERENCE),
    CONDITION(Type.SINGLE_REFERENCE),
    CONTENT(Type.SINGLE_ATTRIBUTE),
    DEFAULT_VALUE(Type.SINGLE_REFERENCE),
    DIMENSION(Type.SINGLE_REFERENCE),
    ELEMENTS(Type.MULTIPLE_REFERENCE),
    ELEMENT_TYPE(Type.SINGLE_REFERENCE),
    ELSE_EXPR(Type.SINGLE_REFERENCE),
    ELSE_STMT(Type.SINGLE_REFERENCE),
    ENCLOSING_PARAMETERS(Type.SINGLE_ATTRIBUTE),
    ENTRIES(Type.MULTIPLE_REFERENCE),
    EXPRESSION(Type.SINGLE_REFERENCE),
    EXTENDED_TYPE(Type.SINGLE_REFERENCE),
    EXTENDED_TYPES(Type.MULTIPLE_REFERENCE),
    FINALLY_BLOCK(Type.SINGLE_REFERENCE),
    IDENTIFIER(Type.SINGLE_ATTRIBUTE),
    IMPLEMENTED_TYPES(Type.MULTIPLE_REFERENCE),
    IMPORTS(Type.MULTIPLE_REFERENCE),
    INDEX(Type.SINGLE_REFERENCE),
    INITIALIZATION(Type.MULTIPLE_REFERENCE),
    INITIALIZER(Type.SINGLE_REFERENCE),
    INNER(Type.SINGLE_REFERENCE),
    INTERFACE(Type.SINGLE_ATTRIBUTE),
    ITERABLE(Type.SINGLE_REFERENCE),
    LABEL(Type.SINGLE_REFERENCE),
    LEFT(Type.SINGLE_REFERENCE),
    LEVELS(Type.MULTIPLE_REFERENCE),
    MEMBERS(Type.MULTIPLE_REFERENCE),
    MEMBER_VALUE(Type.SINGLE_REFERENCE),
    MESSAGE(Type.SINGLE_REFERENCE),
    MODIFIERS(Type.MULTIPLE_ATTRIBUTE),
    MODULE(Type.SINGLE_REFERENCE),
    MODULE_NAMES(Type.MULTIPLE_REFERENCE),
    MODULE_STMTS(Type.MULTIPLE_REFERENCE),
    NAME(Type.SINGLE_REFERENCE),
    OPEN(Type.SINGLE_ATTRIBUTE),
    OPERATOR(Type.SINGLE_ATTRIBUTE),
    ORIGIN(Type.SINGLE_ATTRIBUTE),
    PACKAGE_DECLARATION(Type.SINGLE_REFERENCE),
    PAIRS(Type.MULTIPLE_REFERENCE),
    PARAMETER(Type.SINGLE_REFERENCE),
    PARAMETERS(Type.MULTIPLE_REFERENCE),
    QUALIFIER(Type.SINGLE_REFERENCE),
    RECEIVER_PARAMETER(Type.SINGLE_REFERENCE),
    RESOURCES(Type.MULTIPLE_REFERENCE),
    RIGHT(Type.SINGLE_REFERENCE),
    SCOPE(Type.SINGLE_REFERENCE),
    SELECTOR(Type.SINGLE_REFERENCE),
    STATEMENT(Type.SINGLE_REFERENCE),
    STATEMENTS(Type.MULTIPLE_REFERENCE),
    STATIC(Type.SINGLE_ATTRIBUTE),
    SUPER_TYPE(Type.SINGLE_REFERENCE),
    TARGET(Type.SINGLE_REFERENCE),
    THEN_EXPR(Type.SINGLE_REFERENCE),
    THEN_STMT(Type.SINGLE_REFERENCE),
    THIS(Type.SINGLE_ATTRIBUTE),
    THROWN_EXCEPTIONS(Type.MULTIPLE_REFERENCE),
    TRY_BLOCK(Type.SINGLE_REFERENCE),
    TYPE(Type.SINGLE_REFERENCE),
    TYPES(Type.MULTIPLE_REFERENCE),
    TYPE_ARGUMENTS(Type.MULTIPLE_REFERENCE),
    TYPE_BOUND(Type.MULTIPLE_REFERENCE),
    TYPE_PARAMETERS(Type.MULTIPLE_REFERENCE),
    UPDATE(Type.MULTIPLE_REFERENCE),
    VALUE(Type.SINGLE_REFERENCE),
    VALUES(Type.MULTIPLE_REFERENCE),
    VARIABLE(Type.SINGLE_REFERENCE),
    VARIABLES(Type.MULTIPLE_REFERENCE),
    VAR_ARGS(Type.SINGLE_ATTRIBUTE),
    VAR_ARGS_ANNOTATIONS(Type.MULTIPLE_REFERENCE),
    WITH_TYPES(Type.MULTIPLE_REFERENCE),
    CASCADING_IF_STMT(Type.SINGLE_ATTRIBUTE, true),
    ELSE_BLOCK(Type.SINGLE_ATTRIBUTE, true),
    ELSE_BRANCH(Type.SINGLE_ATTRIBUTE, true),
    EXPRESSION_BODY(Type.SINGLE_REFERENCE, true),
    MAXIMUM_COMMON_TYPE(Type.SINGLE_REFERENCE, true),
    POSTFIX(Type.SINGLE_ATTRIBUTE, true),
    PREFIX(Type.SINGLE_ATTRIBUTE, true),
    THEN_BLOCK(Type.SINGLE_ATTRIBUTE, true),
    USING_DIAMOND_OPERATOR(Type.SINGLE_ATTRIBUTE, true),
    RANGE,
    COMMENTED_NODE;

    enum Type {

        SINGLE_ATTRIBUTE(false, false), SINGLE_REFERENCE(false, true), MULTIPLE_ATTRIBUTE(true, false), MULTIPLE_REFERENCE(true, true);

        private boolean multiple;

        private boolean node;

        Type(boolean multiple, boolean node) {
            this.multiple = multiple;
            this.node = node;
        }
    }

    private Type type;

    private boolean derived;

    public static ObservableProperty fromCamelCaseName(String camelCaseName) {
        Optional<ObservableProperty> observableProperty = Arrays.stream(values()).filter(v -> v.camelCaseName().equals(camelCaseName)).findFirst();
        if (observableProperty.isPresent()) {
            return observableProperty.get();
        } else {
            throw new IllegalArgumentException("No property found with the given camel case name: " + camelCaseName);
        }
    }

    ObservableProperty(Type type) {
        this.type = type;
        this.derived = false;
    }

    ObservableProperty(Type type, boolean derived) {
        this.type = type;
        this.derived = derived;
    }

    ObservableProperty() {
        this(Type.SINGLE_REFERENCE, false);
    }

    public boolean isDerived() {
        return derived;
    }

    public boolean isAboutNodes() {
        return type.node;
    }

    public boolean isAboutValues() {
        return !isAboutNodes();
    }

    public boolean isMultiple() {
        return type.multiple;
    }

    public boolean isSingle() {
        return !isMultiple();
    }

    public String camelCaseName() {
        return Utils.screamingToCamelCase(name());
    }

    public Node getValueAsSingleReference(Node node) {
        Object rawValue = getRawValue(node);
        try {
            if (rawValue instanceof Node) {
                return (Node) rawValue;
            } else if (rawValue instanceof Optional) {
                Optional<Node> opt = (Optional<Node>) rawValue;
                if (opt.isPresent()) {
                    return opt.get();
                } else {
                    return null;
                }
            } else {
                throw new RuntimeException(String.format("Property %s returned %s (%s)", this.name(), rawValue.toString(), rawValue.getClass().getCanonicalName()));
            }
        } catch (ClassCastException e) {
            throw new RuntimeException(e);
        }
    }

    private boolean hasMethod(Node node, String name) {
        try {
            node.getClass().getMethod(name);
            return true;
        } catch (NoSuchMethodException e) {
            return false;
        }
    }

    public NodeList<? extends Node> getValueAsMultipleReference(Node node) {
        Object rawValue = getRawValue(node);
        try {
            if (rawValue == null) {
                return null;
            }
            if (rawValue instanceof NodeList) {
                return (NodeList) rawValue;
            } else {
                Optional<NodeList> opt = (Optional<NodeList>) rawValue;
                if (opt.isPresent()) {
                    return opt.get();
                } else {
                    return null;
                }
            }
        } catch (ClassCastException e) {
            throw new RuntimeException("Unable to get list value for " + this.name() + " from " + node + " (class: " + node.getClass().getSimpleName() + ")", e);
        }
    }

    public Collection<?> getValueAsCollection(Node node) {
        Object rawValue = getRawValue(node);
        try {
            return (Collection) rawValue;
        } catch (ClassCastException e) {
            throw new RuntimeException("Unable to get list value for " + this.name() + " from " + node + " (class: " + node.getClass().getSimpleName() + ")", e);
        }
    }

    public String getValueAsStringAttribute(Node node) {
        return (String) getRawValue(node);
    }

    public Boolean getValueAsBooleanAttribute(Node node) {
        return (Boolean) getRawValue(node);
    }

    public Object getRawValue(Node node) {
        String getterName = "get" + Utils.capitalize(camelCaseName());
        if (!hasMethod(node, getterName)) {
            getterName = "is" + Utils.capitalize(camelCaseName());
            if (!hasMethod(node, getterName)) {
                getterName = "has" + Utils.capitalize(camelCaseName());
            }
        }
        try {
            return node.getClass().getMethod(getterName).invoke(node);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            throw new RuntimeException("Unable to get value for " + this.name() + " from " + node + " (" + node.getClass().getSimpleName() + ")", e);
        }
    }

    public boolean isNull(Node node) {
        return null == getRawValue(node);
    }

    public boolean isNullOrNotPresent(Node node) {
        Object result = getRawValue(node);
        if (result == null) {
            return true;
        }
        if (result instanceof Optional) {
            return !((Optional) result).isPresent();
        }
        return false;
    }

    public boolean isNullOrEmpty(Node node) {
        return Utils.valueIsNullOrEmpty(getRawValue(node));
    }
}