1 /* 2 * Copyright (C) 2007-2010 Júlio Vilmar Gesser. 3 * Copyright (C) 2011, 2013-2016 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 package com.github.javaparser.ast.observer; 22 23 import com.github.javaparser.ast.Node; 24 import com.github.javaparser.ast.NodeList; 25 import com.github.javaparser.ast.Generated; 26 import com.github.javaparser.utils.Utils; 27 import java.lang.reflect.InvocationTargetException; 28 import java.util.Collection; 29 import java.util.Optional; 30 import java.util.Arrays; 31 32 /** 33 * Properties considered by the AstObserver 34 */ 35 @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") 36 public enum ObservableProperty { 37 38 ANNOTATIONS(Type.MULTIPLE_REFERENCE), 39 ANONYMOUS_CLASS_BODY(Type.MULTIPLE_REFERENCE), 40 ARGUMENTS(Type.MULTIPLE_REFERENCE), 41 ASTERISK(Type.SINGLE_ATTRIBUTE), 42 BODY(Type.SINGLE_REFERENCE), 43 CATCH_CLAUSES(Type.MULTIPLE_REFERENCE), 44 CHECK(Type.SINGLE_REFERENCE), 45 CLASS_BODY(Type.MULTIPLE_REFERENCE), 46 CLASS_DECLARATION(Type.SINGLE_REFERENCE), 47 COMMENT(Type.SINGLE_REFERENCE), 48 COMPARE(Type.SINGLE_REFERENCE), 49 COMPONENT_TYPE(Type.SINGLE_REFERENCE), 50 CONDITION(Type.SINGLE_REFERENCE), 51 CONTENT(Type.SINGLE_ATTRIBUTE), 52 DEFAULT_VALUE(Type.SINGLE_REFERENCE), 53 DIMENSION(Type.SINGLE_REFERENCE), 54 DIRECTIVES(Type.MULTIPLE_REFERENCE), 55 ELEMENTS(Type.MULTIPLE_REFERENCE), 56 ELEMENT_TYPE(Type.SINGLE_REFERENCE), 57 ELSE_EXPR(Type.SINGLE_REFERENCE), 58 ELSE_STMT(Type.SINGLE_REFERENCE), 59 ENCLOSING_PARAMETERS(Type.SINGLE_ATTRIBUTE), 60 ENTRIES(Type.MULTIPLE_REFERENCE), 61 EXPRESSION(Type.SINGLE_REFERENCE), 62 EXTENDED_TYPE(Type.SINGLE_REFERENCE), 63 EXTENDED_TYPES(Type.MULTIPLE_REFERENCE), 64 FINALLY_BLOCK(Type.SINGLE_REFERENCE), 65 IDENTIFIER(Type.SINGLE_ATTRIBUTE), 66 IMPLEMENTED_TYPES(Type.MULTIPLE_REFERENCE), 67 IMPORTS(Type.MULTIPLE_REFERENCE), 68 INDEX(Type.SINGLE_REFERENCE), 69 INITIALIZATION(Type.MULTIPLE_REFERENCE), 70 INITIALIZER(Type.SINGLE_REFERENCE), 71 INNER(Type.SINGLE_REFERENCE), 72 INTERFACE(Type.SINGLE_ATTRIBUTE), 73 ITERABLE(Type.SINGLE_REFERENCE), 74 KEYWORD(Type.SINGLE_ATTRIBUTE), 75 LABEL(Type.SINGLE_REFERENCE), 76 LABELS(Type.MULTIPLE_REFERENCE), 77 LEFT(Type.SINGLE_REFERENCE), 78 LEVELS(Type.MULTIPLE_REFERENCE), 79 MEMBERS(Type.MULTIPLE_REFERENCE), 80 MEMBER_VALUE(Type.SINGLE_REFERENCE), 81 MESSAGE(Type.SINGLE_REFERENCE), 82 MODIFIERS(Type.MULTIPLE_REFERENCE), 83 MODULE(Type.SINGLE_REFERENCE), 84 MODULE_NAMES(Type.MULTIPLE_REFERENCE), 85 NAME(Type.SINGLE_REFERENCE), 86 OPEN(Type.SINGLE_ATTRIBUTE), 87 OPERATOR(Type.SINGLE_ATTRIBUTE), 88 ORIGIN(Type.SINGLE_ATTRIBUTE), 89 PACKAGE_DECLARATION(Type.SINGLE_REFERENCE), 90 PAIRS(Type.MULTIPLE_REFERENCE), 91 PARAMETER(Type.SINGLE_REFERENCE), 92 PARAMETERS(Type.MULTIPLE_REFERENCE), 93 QUALIFIER(Type.SINGLE_REFERENCE), 94 RECEIVER_PARAMETER(Type.SINGLE_REFERENCE), 95 RESOURCES(Type.MULTIPLE_REFERENCE), 96 RIGHT(Type.SINGLE_REFERENCE), 97 SCOPE(Type.SINGLE_REFERENCE), 98 SELECTOR(Type.SINGLE_REFERENCE), 99 STATEMENT(Type.SINGLE_REFERENCE), 100 STATEMENTS(Type.MULTIPLE_REFERENCE), 101 STATIC(Type.SINGLE_ATTRIBUTE), 102 SUPER_TYPE(Type.SINGLE_REFERENCE), 103 TARGET(Type.SINGLE_REFERENCE), 104 THEN_EXPR(Type.SINGLE_REFERENCE), 105 THEN_STMT(Type.SINGLE_REFERENCE), 106 THIS(Type.SINGLE_ATTRIBUTE), 107 THROWN_EXCEPTIONS(Type.MULTIPLE_REFERENCE), 108 TRY_BLOCK(Type.SINGLE_REFERENCE), 109 TYPE(Type.SINGLE_REFERENCE), 110 TYPES(Type.MULTIPLE_REFERENCE), 111 TYPE_ARGUMENTS(Type.MULTIPLE_REFERENCE), 112 TYPE_BOUND(Type.MULTIPLE_REFERENCE), 113 TYPE_NAME(Type.SINGLE_REFERENCE), 114 TYPE_PARAMETERS(Type.MULTIPLE_REFERENCE), 115 UPDATE(Type.MULTIPLE_REFERENCE), 116 VALUE(Type.SINGLE_REFERENCE), 117 VALUES(Type.MULTIPLE_REFERENCE), 118 VARIABLE(Type.SINGLE_REFERENCE), 119 VARIABLES(Type.MULTIPLE_REFERENCE), 120 VAR_ARGS(Type.SINGLE_ATTRIBUTE), 121 VAR_ARGS_ANNOTATIONS(Type.MULTIPLE_REFERENCE), 122 WITH(Type.MULTIPLE_REFERENCE), 123 CASCADING_IF_STMT(Type.SINGLE_ATTRIBUTE, true), 124 ELSE_BLOCK(Type.SINGLE_ATTRIBUTE, true), 125 ELSE_BRANCH(Type.SINGLE_ATTRIBUTE, true), 126 EXPRESSION_BODY(Type.SINGLE_REFERENCE, true), 127 MAXIMUM_COMMON_TYPE(Type.SINGLE_REFERENCE, true), 128 POSTFIX(Type.SINGLE_ATTRIBUTE, true), 129 PREFIX(Type.SINGLE_ATTRIBUTE, true), 130 THEN_BLOCK(Type.SINGLE_ATTRIBUTE, true), 131 USING_DIAMOND_OPERATOR(Type.SINGLE_ATTRIBUTE, true), 132 RANGE, 133 COMMENTED_NODE; 134 135 enum Type { 136 137 SINGLE_ATTRIBUTE(false, false), SINGLE_REFERENCE(false, true), MULTIPLE_ATTRIBUTE(true, false), MULTIPLE_REFERENCE(true, true); 138 139 private boolean multiple; 140 141 private boolean node; 142 Type(boolean multiple, boolean node)143 Type(boolean multiple, boolean node) { 144 this.multiple = multiple; 145 this.node = node; 146 } 147 } 148 149 private Type type; 150 151 private boolean derived; 152 fromCamelCaseName(String camelCaseName)153 public static ObservableProperty fromCamelCaseName(String camelCaseName) { 154 Optional<ObservableProperty> observableProperty = Arrays.stream(values()).filter(v -> v.camelCaseName().equals(camelCaseName)).findFirst(); 155 if (observableProperty.isPresent()) { 156 return observableProperty.get(); 157 } else { 158 throw new IllegalArgumentException("No property found with the given camel case name: " + camelCaseName); 159 } 160 } 161 ObservableProperty(Type type)162 ObservableProperty(Type type) { 163 this.type = type; 164 this.derived = false; 165 } 166 ObservableProperty(Type type, boolean derived)167 ObservableProperty(Type type, boolean derived) { 168 this.type = type; 169 this.derived = derived; 170 } 171 ObservableProperty()172 ObservableProperty() { 173 this(Type.SINGLE_REFERENCE, false); 174 } 175 isDerived()176 public boolean isDerived() { 177 return derived; 178 } 179 isAboutNodes()180 public boolean isAboutNodes() { 181 return type.node; 182 } 183 isAboutValues()184 public boolean isAboutValues() { 185 return !isAboutNodes(); 186 } 187 isMultiple()188 public boolean isMultiple() { 189 return type.multiple; 190 } 191 isSingle()192 public boolean isSingle() { 193 return !isMultiple(); 194 } 195 camelCaseName()196 public String camelCaseName() { 197 return Utils.screamingToCamelCase(name()); 198 } 199 getValueAsSingleReference(Node node)200 public Node getValueAsSingleReference(Node node) { 201 Object rawValue = getRawValue(node); 202 try { 203 if (rawValue instanceof Node) { 204 return (Node) rawValue; 205 } else if (rawValue instanceof Optional) { 206 Optional<Node> opt = (Optional<Node>) rawValue; 207 if (opt.isPresent()) { 208 return opt.get(); 209 } else { 210 return null; 211 } 212 } else { 213 throw new RuntimeException(String.format("Property %s returned %s (%s)", this.name(), rawValue.toString(), rawValue.getClass().getCanonicalName())); 214 } 215 } catch (ClassCastException e) { 216 throw new RuntimeException(e); 217 } 218 } 219 hasMethod(Node node, String name)220 private boolean hasMethod(Node node, String name) { 221 try { 222 node.getClass().getMethod(name); 223 return true; 224 } catch (NoSuchMethodException e) { 225 return false; 226 } 227 } 228 getValueAsMultipleReference(Node node)229 public NodeList<? extends Node> getValueAsMultipleReference(Node node) { 230 Object rawValue = getRawValue(node); 231 try { 232 if (rawValue == null) { 233 return null; 234 } 235 if (rawValue instanceof NodeList) { 236 return (NodeList) rawValue; 237 } else { 238 Optional<NodeList> opt = (Optional<NodeList>) rawValue; 239 if (opt.isPresent()) { 240 return opt.get(); 241 } else { 242 return null; 243 } 244 } 245 } catch (ClassCastException e) { 246 throw new RuntimeException("Unable to get list value for " + this.name() + " from " + node + " (class: " + node.getClass().getSimpleName() + ")", e); 247 } 248 } 249 getValueAsCollection(Node node)250 public Collection<?> getValueAsCollection(Node node) { 251 Object rawValue = getRawValue(node); 252 try { 253 return (Collection) rawValue; 254 } catch (ClassCastException e) { 255 throw new RuntimeException("Unable to get list value for " + this.name() + " from " + node + " (class: " + node.getClass().getSimpleName() + ")", e); 256 } 257 } 258 getValueAsStringAttribute(Node node)259 public String getValueAsStringAttribute(Node node) { 260 return (String) getRawValue(node); 261 } 262 getValueAsBooleanAttribute(Node node)263 public Boolean getValueAsBooleanAttribute(Node node) { 264 return (Boolean) getRawValue(node); 265 } 266 getRawValue(Node node)267 public Object getRawValue(Node node) { 268 String getterName = "get" + Utils.capitalize(camelCaseName()); 269 if (!hasMethod(node, getterName)) { 270 getterName = "is" + Utils.capitalize(camelCaseName()); 271 if (!hasMethod(node, getterName)) { 272 getterName = "has" + Utils.capitalize(camelCaseName()); 273 } 274 } 275 try { 276 return node.getClass().getMethod(getterName).invoke(node); 277 } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { 278 throw new RuntimeException("Unable to get value for " + this.name() + " from " + node + " (" + node.getClass().getSimpleName() + ")", e); 279 } 280 } 281 isNull(Node node)282 public boolean isNull(Node node) { 283 return null == getRawValue(node); 284 } 285 isNullOrNotPresent(Node node)286 public boolean isNullOrNotPresent(Node node) { 287 Object result = getRawValue(node); 288 if (result == null) { 289 return true; 290 } 291 if (result instanceof Optional) { 292 return !((Optional) result).isPresent(); 293 } 294 return false; 295 } 296 isNullOrEmpty(Node node)297 public boolean isNullOrEmpty(Node node) { 298 return Utils.valueIsNullOrEmpty(getRawValue(node)); 299 } 300 } 301