Skip to content

Commit

Permalink
reorganize ObservableProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomassetti committed Feb 22, 2017
1 parent 56717f7 commit fcf0d94
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 143 deletions.
Expand Up @@ -26,12 +26,7 @@
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Optional;
import static com.github.javaparser.ast.observer.ObservableProperty.Type.*;
import com.github.javaparser.utils.Utils;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Optional;
import static com.github.javaparser.utils.Utils.capitalize;

/**
* Properties considered by the AstObserver
Expand All @@ -58,6 +53,15 @@ enum 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;
Expand All @@ -72,6 +76,10 @@ enum Type {
this(Type.SINGLE_REFERENCE, false);
}

public boolean isDerived() {
return derived;
}

public boolean isAboutNodes() {
return type.node;
}
Expand All @@ -92,27 +100,21 @@ public String camelCaseName() {
return Utils.toCamelCase(name());
}

public Node singlePropertyFor(Node node) {
String getterName = "get" + Utils.capitalize(camelCaseName());
public Node getValueAsSingleReference(Node node) {
Object rawValue = getRawValue(node);
try {
Object result = node.getClass().getMethod(getterName).invoke(node);
if (result == null) {
return null;
}
if (result instanceof Node) {
return (Node) result;
} else if (result instanceof Optional) {
Optional<Node> opt = (Optional<Node>) result;
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(), result.toString(), result.getClass().getCanonicalName()));
throw new RuntimeException(String.format("Property %s returned %s (%s)", this.name(), rawValue.toString(), rawValue.getClass().getCanonicalName()));
}
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException("Unable to get single value for " + this.name() + " from " + node, e);
} catch (ClassCastException e) {
throw new RuntimeException(e);
}
Expand All @@ -127,154 +129,76 @@ private boolean hasMethod(Node node, String name) {
}
}

public Object singleValueFor(Node node) {
String getterName = "get" + Utils.capitalize(camelCaseName());
if (!hasMethod(node, getterName)) {
getterName = "has" + Utils.capitalize(camelCaseName());
if (!hasMethod(node, getterName)) {
if (camelCaseName().startsWith("is")) {
getterName = camelCaseName();
} else {
getterName = "is" + Utils.capitalize(camelCaseName());
}
}
}
try {
Object result = node.getClass().getMethod(getterName).invoke(node);
if (result == null) {
return null;
}
if (result instanceof Optional) {
Optional<Node> opt = (Optional<Node>) result;
if (opt.isPresent()) {
return opt.get();
} else {
return null;
}
} else {
return result;
}
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException("Unable to get single value for " + this.name() + " from " + node + " (class: " + node.getClass().getSimpleName() + ")", e);
}
}

public NodeList<? extends Node> listValueFor(Node node) {
String getterName = "get" + Utils.capitalize(camelCaseName());
public NodeList<? extends Node> getValueAsMultipleReference(Node node) {
Object rawValue = getRawValue(node);
try {
Object result = node.getClass().getMethod(getterName).invoke(node);
if (result == null) {
if (rawValue == null) {
return null;
}
if (result instanceof NodeList) {
return (NodeList) result;
if (rawValue instanceof NodeList) {
return (NodeList) rawValue;
} else {
Optional<NodeList> opt = (Optional<NodeList>) result;
Optional<NodeList> opt = (Optional<NodeList>) rawValue;
if (opt.isPresent()) {
return opt.get();
} else {
return null;
}
}
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
} catch (ClassCastException e) {
throw new RuntimeException("Unable to get list value for " + this.name() + " from " + node + " (class: " + node.getClass().getSimpleName() + ")", e);
}
}

public Collection<?> listPropertyFor(Node node) {
String getterName = "get" + Utils.capitalize(camelCaseName());
public Collection<?> getValueAsCollection(Node node) {
Object rawValue = getRawValue(node);
try {
Object result = node.getClass().getMethod(getterName).invoke(node);
if (result == null) {
return null;
}
return (Collection) result;
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
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 singleStringValueFor(Node node) {
return (String) getValue(node);
public String getValueAsStringAttribute(Node node) {
return (String) getRawValue(node);
}

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

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

public boolean isNullOrNotPresent(Node node) {
String getterName = "get" + Utils.capitalize(camelCaseName());
try {
Object result = node.getClass().getMethod(getterName).invoke(node);
if (result == null) {
return true;
}
if (result instanceof Optional) {
return !((Optional) result).isPresent();
}
return false;
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException("Unable to get single value for " + this.name() + " from " + node, e);
}
}

public static boolean valueIsNullOrEmpty(Object value) {
if (value == null) {
Object result = getRawValue(node);
if (result == null) {
return true;
}
if (value instanceof Optional) {
if (((Optional) value).isPresent()) {
value = ((Optional) value).get();
} else {
return true;
}
}
if (value instanceof Collection) {
if (((Collection) value).isEmpty()) {
return true;
}
if (result instanceof Optional) {
return !((Optional) result).isPresent();
}
return false;
}

public boolean isNullOrEmpty(Node node) {
String getterName = "get" + Utils.capitalize(camelCaseName());
try {
Object result = node.getClass().getMethod(getterName).invoke(node);
return valueIsNullOrEmpty(result);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException("Unable to get single value for " + this.name() + " from " + node, e);
}
}

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);
}
return Utils.valueIsNullOrEmpty(getRawValue(node));
}
}

Expand Up @@ -526,10 +526,10 @@ private static void initializePropertyMetaModels() {
unaryExprMetaModel.getDeclaredPropertyMetaModels().add(unaryExprMetaModel.expressionPropertyMetaModel);
unaryExprMetaModel.operatorPropertyMetaModel = new PropertyMetaModel(unaryExprMetaModel, "operator", com.github.javaparser.ast.expr.UnaryExpr.Operator.class, Optional.empty(), false, false, false, false, false);
unaryExprMetaModel.getDeclaredPropertyMetaModels().add(unaryExprMetaModel.operatorPropertyMetaModel);
unaryExprMetaModel.postfixPropertyMetaModel = new PropertyMetaModel(unaryExprMetaModel, "postfix", boolean.class, Optional.empty(), false, true, false, false, false);
unaryExprMetaModel.getDerivedPropertyMetaModels().add(unaryExprMetaModel.postfixPropertyMetaModel);
unaryExprMetaModel.prefixPropertyMetaModel = new PropertyMetaModel(unaryExprMetaModel, "prefix", boolean.class, Optional.empty(), false, true, false, false, false);
unaryExprMetaModel.getDerivedPropertyMetaModels().add(unaryExprMetaModel.prefixPropertyMetaModel);
unaryExprMetaModel.postfixPropertyMetaModel = new PropertyMetaModel(unaryExprMetaModel, "postfix", boolean.class, Optional.empty(), false, true, false, false, false);
unaryExprMetaModel.getDerivedPropertyMetaModels().add(unaryExprMetaModel.postfixPropertyMetaModel);
variableDeclarationExprMetaModel.annotationsPropertyMetaModel = new PropertyMetaModel(variableDeclarationExprMetaModel, "annotations", com.github.javaparser.ast.expr.AnnotationExpr.class, Optional.of(annotationExprMetaModel), false, false, true, false, false);
variableDeclarationExprMetaModel.getDeclaredPropertyMetaModels().add(variableDeclarationExprMetaModel.annotationsPropertyMetaModel);
variableDeclarationExprMetaModel.modifiersPropertyMetaModel = new PropertyMetaModel(variableDeclarationExprMetaModel, "modifiers", com.github.javaparser.ast.Modifier.class, Optional.empty(), false, false, false, true, false);
Expand Down
Expand Up @@ -12,8 +12,8 @@ public class UnaryExprMetaModel extends ExpressionMetaModel {

public PropertyMetaModel operatorPropertyMetaModel;

public PropertyMetaModel postfixPropertyMetaModel;

public PropertyMetaModel prefixPropertyMetaModel;

public PropertyMetaModel postfixPropertyMetaModel;
}

Expand Up @@ -40,7 +40,7 @@ public CsmAttribute(ObservableProperty property) {

@Override
public void prettyPrint(Node node, SourcePrinter printer) {
Object value = property.singleValueFor(node);
Object value = property.getRawValue(node);
printer.print(PrintingHelper.printToString(value));
}

Expand Down
Expand Up @@ -35,7 +35,7 @@ public CsmChar(ObservableProperty property) {
@Override
public void prettyPrint(Node node, SourcePrinter printer) {
printer.print("'");
printer.print(property.singleStringValueFor(node));
printer.print(property.getValueAsStringAttribute(node));
printer.print("'");
}
}
Expand Up @@ -24,11 +24,8 @@
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.observer.ObservableProperty;
import com.github.javaparser.printer.ConcreteSyntaxModel;
import com.github.javaparser.printer.SourcePrinter;

import java.util.function.Predicate;

public class CsmConditional implements CsmElement {
private Condition condition;
private ObservableProperty property;
Expand Down Expand Up @@ -62,13 +59,13 @@ boolean evaluate(Node node, ObservableProperty property){
return !property.isNullOrNotPresent(node);
}
if (this == FLAG) {
return (Boolean)property.singleValueFor(node);
return property.getValueAsBooleanAttribute(node);
}
if (this == IS_EMPTY) {
return property.listValueFor(node).isEmpty();
return property.getValueAsMultipleReference(node).isEmpty();
}
if (this == IS_NOT_EMPTY) {
NodeList value = property.listValueFor(node);
NodeList value = property.getValueAsMultipleReference(node);
return value != null && !value.isEmpty();
}
throw new UnsupportedOperationException(name());
Expand Down
Expand Up @@ -76,7 +76,7 @@ public CsmList(ObservableProperty property, CsmElement separatorPre, CsmElement
@Override
public void prettyPrint(Node node, SourcePrinter printer) {
if (property.isAboutNodes()) {
NodeList nodeList = property.listValueFor(node);
NodeList nodeList = property.getValueAsMultipleReference(node);
if (nodeList == null) {
return;
}
Expand All @@ -96,7 +96,7 @@ public void prettyPrint(Node node, SourcePrinter printer) {
following.prettyPrint(node, printer);
}
} else {
Collection<?> values = property.listPropertyFor(node);
Collection<?> values = property.getValueAsCollection(node);
if (values == null) {
return;
}
Expand Down
Expand Up @@ -40,7 +40,7 @@ public CsmSingleReference(ObservableProperty property) {

@Override
public void prettyPrint(Node node, SourcePrinter printer) {
Node child = property.singlePropertyFor(node);
Node child = property.getValueAsSingleReference(node);
if (child != null) {
ConcreteSyntaxModel.genericPrettyPrint(child, printer);
}
Expand Down
Expand Up @@ -35,7 +35,7 @@ public CsmString(ObservableProperty property) {
@Override
public void prettyPrint(Node node, SourcePrinter printer) {
printer.print("\"");
printer.print(property.singleStringValueFor(node));
printer.print(property.getValueAsStringAttribute(node));
printer.print("\"");
}
}
Expand Up @@ -181,4 +181,28 @@ private static String stringTransformer(String s, String operationDescription, F
sb.append(s.substring(1));
return sb.toString();
}

/**
* Return true if the value is null, an empty Optional or an empty String.
* @param value
* @return
*/
public static boolean valueIsNullOrEmpty(Object value) {
if (value == null) {
return true;
}
if (value instanceof Optional) {
if (((Optional) value).isPresent()) {
value = ((Optional) value).get();
} else {
return true;
}
}
if (value instanceof Collection) {
if (((Collection) value).isEmpty()) {
return true;
}
}
return false;
}
}

0 comments on commit fcf0d94

Please sign in to comment.