Skip to content

Commit

Permalink
issue705: add more types of ObservableProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomassetti committed Feb 15, 2017
1 parent 19e4833 commit a5e6d56
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 57 deletions.
Expand Up @@ -26,22 +26,26 @@


import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.Collection; import java.util.Collection;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Set;
import static com.github.javaparser.ast.observer.ObservableProperty.Type.MULTIPLE_NODES;
import static com.github.javaparser.ast.observer.ObservableProperty.Type.MULTIPLE_PROPERTIES;
import static com.github.javaparser.ast.observer.ObservableProperty.Type.SINGLE_PROPERTY;


/** /**
* Properties considered by the AstObserver * Properties considered by the AstObserver
*/ */
public enum ObservableProperty { public enum ObservableProperty {
ANNOTATIONS(true), ANNOTATIONS(MULTIPLE_NODES),
ANONYMOUS_CLASS_BODY, ANONYMOUS_CLASS_BODY,
ARGUMENTS(true), ARGUMENTS(MULTIPLE_NODES),
IS_ASTERISK,
BLOCK, BLOCK,
BODY, BODY,
CATCH_CLAUSES(true), CATCH_CLAUSES(MULTIPLE_NODES),
CHECK, CHECK,
CLASS_BODY, CLASS_BODY,
CLASS_DECLARATION,
CLASS_EXPR, CLASS_EXPR,
COMMENT, COMMENT,
COMMENTED_NODE, COMMENTED_NODE,
Expand All @@ -54,13 +58,14 @@ public enum ObservableProperty {
ELEMENTS, ELEMENTS,
ELSE_EXPR, ELSE_EXPR,
ELSE_STMT, ELSE_STMT,
ENCLOSING_PARAMETERS,
ENTRIES, ENTRIES,
EXPRESSION, EXPRESSION,
EXTENDED_TYPES(true), EXTENDED_TYPES(MULTIPLE_NODES),
FIELD, FIELD,
FINALLY_BLOCK, FINALLY_BLOCK,
IDENTIFIER, IDENTIFIER,
IMPLEMENTED_TYPES(true), IMPLEMENTED_TYPES(MULTIPLE_NODES),
IMPORTS, IMPORTS,
INDEX, INDEX,
INITIALIZER, INITIALIZER,
Expand All @@ -73,22 +78,20 @@ public enum ObservableProperty {
LEVELS, LEVELS,
MEMBERS, MEMBERS,
MEMBER_VALUE, MEMBER_VALUE,
MODIFIERS, MODIFIERS(MULTIPLE_PROPERTIES),
MESSAGE, MESSAGE,
NAME, NAME,
OPERATOR, OPERATOR,
PACKAGE_DECLARATION, PACKAGE_DECLARATION,
PAIRS, PAIRS,
PARAMETER, PARAMETER,
PARAMETERS, PARAMETERS,
ENCLOSING_PARAMETERS,
QUALIFIER, QUALIFIER,
RANGE, RANGE,
RESOURCES, RESOURCES,
RIGHT, RIGHT,
SCOPE, SCOPE,
SELECTOR, SELECTOR,
IS_ASTERISK,
IS_STATIC, IS_STATIC,
STATIC_MEMBER, STATIC_MEMBER,
STATEMENT, STATEMENT,
Expand All @@ -97,34 +100,56 @@ public enum ObservableProperty {
TARGET, TARGET,
THEN_EXPR, THEN_EXPR,
THEN_STMT, THEN_STMT,
THROWN_TYPES(true), THROWN_TYPES(MULTIPLE_NODES),
TRY_BLOCK, TRY_BLOCK,
TYPE, TYPE(SINGLE_PROPERTY),
TYPES, TYPES,
TYPE_ARGUMENTS(true), TYPE_ARGUMENTS(MULTIPLE_NODES),
TYPE_BOUND, TYPE_BOUND,
CLASS_DECLARATION, TYPE_PARAMETERS(MULTIPLE_NODES),
TYPE_PARAMETERS(true),
UPDATE, UPDATE,
VALUE, VALUE,
VALUES, VALUES,
VARIABLE, VARIABLE,
VARIABLES, VARIABLES(MULTIPLE_NODES),
ELEMENT_TYPE, ELEMENT_TYPE,
VAR_ARGS(true); VAR_ARGS(MULTIPLE_NODES);

enum Type {
SINGLE_PROPERTY(false, false),
SINGLE_NODE(false, true),
MULTIPLE_PROPERTIES(true, false),
MULTIPLE_NODES(true, true);

private boolean multiple;
private boolean node;


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

private Type type;


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


ObservableProperty() { ObservableProperty() {
this(false); this(Type.SINGLE_NODE);
}

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

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


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


public boolean isSingle() { public boolean isSingle() {
Expand All @@ -135,7 +160,7 @@ public String camelCaseName() {
return Utils.toCamelCase(name()); return Utils.toCamelCase(name());
} }


public Node singleValueFor(Node node) { public Node singlePropertyFor(Node node) {
String getterName = "get" + Utils.capitalize(camelCaseName()); String getterName = "get" + Utils.capitalize(camelCaseName());
try { try {
Object result = node.getClass().getMethod(getterName).invoke(node); Object result = node.getClass().getMethod(getterName).invoke(node);
Expand All @@ -144,13 +169,37 @@ public Node singleValueFor(Node node) {
} }
if (result instanceof Node) { if (result instanceof Node) {
return (Node)result; return (Node)result;
} else if (result instanceof Optional){
Optional<Node> opt = (Optional<Node>)result;
if (opt.isPresent()) {
return opt.get();
} else {
return null;
}
} else { } else {
throw new RuntimeException(String.format("Property %s returned %s (%s)", this.name(), result.toString(), result.getClass().getCanonicalName()));
}
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException("Unable to get single value for " + this.name() + " from " + node, e);
}
}

public Object singleValueFor(Node node) {
String getterName = "get" + 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; Optional<Node> opt = (Optional<Node>)result;
if (opt.isPresent()) { if (opt.isPresent()) {
return opt.get(); return opt.get();
} else { } else {
return null; return null;
} }
} else {
return result;
} }
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException("Unable to get single value for " + this.name() + " from " + node, e); throw new RuntimeException("Unable to get single value for " + this.name() + " from " + node, e);
Expand Down
Expand Up @@ -23,32 +23,21 @@


import com.github.javaparser.ASTParserConstants; import com.github.javaparser.ASTParserConstants;
import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Modifier;
import com.github.javaparser.ast.Node; import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList; import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.FieldDeclaration; import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.VariableDeclarator; import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.comments.Comment; import com.github.javaparser.ast.comments.Comment;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.expr.ClassExpr; import com.github.javaparser.ast.expr.ClassExpr;
import com.github.javaparser.ast.expr.SimpleName; import com.github.javaparser.ast.expr.SimpleName;
import com.github.javaparser.ast.nodeTypes.NodeWithModifiers;
import com.github.javaparser.ast.nodeTypes.NodeWithTypeArguments;
import com.github.javaparser.ast.nodeTypes.NodeWithVariables; import com.github.javaparser.ast.nodeTypes.NodeWithVariables;
import com.github.javaparser.ast.observer.Observable;
import com.github.javaparser.ast.observer.ObservableProperty; import com.github.javaparser.ast.observer.ObservableProperty;
import com.github.javaparser.ast.type.ArrayType; import com.github.javaparser.ast.type.*;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.ast.type.Type; import java.util.*;

import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.stream.Collectors;


import static com.github.javaparser.ast.observer.ObservableProperty.*; import static com.github.javaparser.ast.observer.ObservableProperty.*;
import static com.github.javaparser.utils.PositionUtils.sortByBeginPosition; import static com.github.javaparser.utils.PositionUtils.sortByBeginPosition;
Expand Down Expand Up @@ -107,13 +96,29 @@ public ChildElement(ObservableProperty property) {


@Override @Override
public void prettyPrint(Node node, SourcePrinter printer) { public void prettyPrint(Node node, SourcePrinter printer) {
Node child = property.singleValueFor(node); Node child = property.singlePropertyFor(node);
if (child != null) { if (child != null) {
genericPrettyPrint(child, printer); genericPrettyPrint(child, printer);
} }
} }
} }


private static class ValueElement implements Element {
private ObservableProperty property;

public ValueElement(ObservableProperty property) {
this.property = property;
}

@Override
public void prettyPrint(Node node, SourcePrinter printer) {
Object value = property.singleValueFor(node);
if (value != null) {
printer.print(value.toString());
}
}
}

private static class ListElement implements Element { private static class ListElement implements Element {
private ObservableProperty property; private ObservableProperty property;
private Element separator; private Element separator;
Expand All @@ -139,21 +144,40 @@ public ListElement(ObservableProperty property, Element separator, Element prece


@Override @Override
public void prettyPrint(Node node, SourcePrinter printer) { public void prettyPrint(Node node, SourcePrinter printer) {
NodeList nodeList = property.listValueFor(node); if (property.isAboutNodes()) {
if (nodeList == null) { NodeList nodeList = property.listValueFor(node);
return; if (nodeList == null) {
} return;
if (!nodeList.isEmpty() && preceeding != null) { }
preceeding.prettyPrint(node, printer); if (!nodeList.isEmpty() && preceeding != null) {
} preceeding.prettyPrint(node, printer);
for (int i=0;i<nodeList.size();i++) { }
genericPrettyPrint(nodeList.get(i), printer); for (int i = 0; i < nodeList.size(); i++) {
if (separator != null && i != (nodeList.size() - 1)) { genericPrettyPrint(nodeList.get(i), printer);
separator.prettyPrint(node, printer); if (separator != null && i != (nodeList.size() - 1)) {
separator.prettyPrint(node, printer);
}
}
if (!nodeList.isEmpty() && following != null) {
following.prettyPrint(node, printer);
}
} else {
Collection<?> values = property.listPropertyFor(node);
if (values == null) {
return;
}
if (!values.isEmpty() && preceeding != null) {
preceeding.prettyPrint(node, printer);
}
for (Iterator it = values.iterator(); it.hasNext(); ) {
printer.print(it.next().toString());
if (separator != null && it.hasNext()) {
separator.prettyPrint(node, printer);
}
}
if (!values.isEmpty() && following != null) {
following.prettyPrint(node, printer);
} }
}
if (!nodeList.isEmpty() && following != null) {
following.prettyPrint(node, printer);
} }
} }
} }
Expand Down Expand Up @@ -196,7 +220,7 @@ public void prettyPrint(Node node, SourcePrinter printer) {
boolean test; boolean test;
if (condition != null) { if (condition != null) {
if (condition.isSingle()) { if (condition.isSingle()) {
test = condition.singleValueFor(node) != null; test = condition.singlePropertyFor(node) != null;
} else { } else {
test = condition.listValueFor(node) != null && !condition.listValueFor(node).isEmpty(); test = condition.listValueFor(node) != null && !condition.listValueFor(node).isEmpty();
} }
Expand Down Expand Up @@ -254,6 +278,10 @@ Builder child(ObservableProperty property) {
return add(new ChildElement(property)); return add(new ChildElement(property));
} }


Builder value(ObservableProperty property) {
return add(new ValueElement(property));
}

Builder string(int tokenType, String content) { Builder string(int tokenType, String content) {
return add(new StringElement(tokenType, content)); return add(new StringElement(tokenType, content));
} }
Expand Down Expand Up @@ -328,8 +356,7 @@ Builder orphanCommentsBeforeThis() {
} }


Builder annotations() { Builder annotations() {
//return list(ObservableProperty.ANNOTATIONS, newline(), null, newline()); return add(ConcreteSyntaxModel.list(ObservableProperty.ANNOTATIONS, ConcreteSyntaxModel.newline(), null, ConcreteSyntaxModel.newline()));
throw new UnsupportedOperationException();
} }


Builder modifiers() { Builder modifiers() {
Expand Down Expand Up @@ -388,8 +415,8 @@ private static ChildElement child(ObservableProperty property) {
return new ChildElement(property); return new ChildElement(property);
} }


private static ChildElement child(Node node) { private static Element child(Node child) {
throw new UnsupportedOperationException(); return (node, printer) -> genericPrettyPrint(child, printer);
} }


private static ListElement list(ObservableProperty property) { private static ListElement list(ObservableProperty property) {
Expand Down Expand Up @@ -500,6 +527,45 @@ public static ConcreteSyntaxModel forClass(Class<? extends Node> nodeClazz) {
.semicolon() .semicolon()
.build(); .build();
} }
if (nodeClazz.equals(PrimitiveType.class)) {
return new Builder()
.comment()
.annotations()
.value(ObservableProperty.TYPE)
.build();
}
if (nodeClazz.equals(VariableDeclarator.class)) {
return new Builder()
.comment()
.child(ObservableProperty.NAME)
.annotations()
.value(ObservableProperty.TYPE)
.build();

// printJavaComment(n.getComment(), arg);
// n.getName().accept(this, arg);
//
// Type commonType = getMaximumCommonType(n.getAncestorOfType(NodeWithVariables.class).get());
//
// Type type = n.getType();
//
// ArrayType arrayType = null;
//
// for (int i = commonType.getArrayLevel(); i < type.getArrayLevel(); i++) {
// if (arrayType == null) {
// arrayType = (ArrayType) type;
// } else {
// arrayType = (ArrayType) arrayType.getComponentType();
// }
// printAnnotations(arrayType.getAnnotations(), true, arg);
// printer.print("[]");
// }
//
// if (n.getInitializer().isPresent()) {
// printer.print(" = ");
// n.getInitializer().get().accept(this, arg);
// }
}


throw new UnsupportedOperationException("Class " + nodeClazz.getSimpleName()); throw new UnsupportedOperationException("Class " + nodeClazz.getSimpleName());
} }
Expand Down

0 comments on commit a5e6d56

Please sign in to comment.