Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(role): rename CtPathRole to CtRole and add new roles #1372

Merged
merged 1 commit into from
Jun 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/spoon/reflect/path/CtPathBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ public <T extends CtElement> CtPathBuilder type(Class<T> type, String[]... args)
/**
* Match on elements by their role.
*
* @see spoon.reflect.path.CtPathRole
* @see CtRole
*/
public CtPathBuilder role(CtPathRole role, String[]... args) {
public CtPathBuilder role(CtRole role, String[]... args) {
CtRolePathElement e = new CtRolePathElement(role);
if (args != null) {
for (String[] arg : args) {
Expand Down
61 changes: 0 additions & 61 deletions src/main/java/spoon/reflect/path/CtPathRole.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/java/spoon/reflect/path/CtPathStringBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public CtPath fromString(String pathStr) throws CtPathException {
} else if (CtTypedNameElement.STRING.equals(kind)) {
pathElement = new CtTypedNameElement(load(matcher.group(2)));
} else if (CtRolePathElement.STRING.equals(kind)) {
pathElement = new CtRolePathElement(CtPathRole.fromName(matcher.group(2)));
pathElement = new CtRolePathElement(CtRole.fromName(matcher.group(2)));
}

String args = matcher.group(4);
Expand Down
201 changes: 201 additions & 0 deletions src/main/java/spoon/reflect/path/CtRole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
/**
* Copyright (C) 2006-2017 INRIA and contributors
* Spoon - http://spoon.gforge.inria.fr/
*
* This software is governed by the CeCILL-C License under French law and
* abiding by the rules of distribution of free software. You can use, modify
* and/or redistribute the software under the terms of the CeCILL-C license as
* circulated by CEA, CNRS and INRIA at http://www.cecill.info.
*
* This program 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 CeCILL-C License for more details.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-C license and that you accept its terms.
*/
package spoon.reflect.path;

/**
* Created by nicolas on 27/08/2015.
*/
public enum CtRole {
PARENT,
NAME,
DECLARING_TYPE,
TYPE,
BODY,
IS_FINAL,
IS_SHADOW,
IS_STATIC,
IS_IMPLICIT,
IS_DEFAULT,
IS_VARARGS,
IS_UPPER,
DEFAULT_EXPRESSION,
THEN,
ELSE,
PACKAGE,
CONDITION,
SUPER_TYPE,
POSITION,
RIGHT_OPERAND,
LEFT_OPERAND,
LABEL,
CASE,
KIND,
PARAMETER,
EXPRESSION,
TARGET,
OPERAND,
VARIABLE,
FINALIZER,
THROW,
EXECUTABLE,
ASSIGNMENT,
ASSIGNED,
MODIFIERS,
COMMENTS,
TYPES,
INTERFACES,
ANNOTATIONS,
STATEMENTS,
ARGUMENTS,
MEMBERS,
CASTS,
VALUES,
FOR_UPDATE,
FOR_INIT,
RESOURCES,
DIMENSIONS,
BOUNDS,
CATCHERS,
ANONYMOUS_CLASS,
TARGET_LABEL,
TYPE_PARAMETERS,
CONTENT,
TAGS;

/**
* Get the {@link CtRole} associated to the field name
* @param name
* @return
*/
public static CtRole fromName(String name) {
name = name.toLowerCase();
for (int i = 0; i < CtRole.values().length; i++) {
if (CtRole.values()[i].getCamelCaseName().toLowerCase()
.equals(name)) {
return CtRole.values()[i];
}
}
if ("implicit".equals(name)) {
return IS_IMPLICIT;
}
if ("fina".equals(name)) {
return IS_FINAL;
}
if ("stat".equals(name)) {
return IS_STATIC;
}
if ("varargs".equals(name)) {
return IS_VARARGS;
}
if ("defaultmethod".equals(name)) {
return IS_DEFAULT;
}
if ("block".equals(name)) {
return BODY;
}
if ("param".equals(name)) {
return PARAMETER;
}
if ("dimensionexpressions".equals(name)) {
return DIMENSIONS;
}
if ("actualtypearguments".equals(name)) {
return TYPE_PARAMETERS;
}
if ("formalcttypeparameters".equals(name)) {
return TYPE_PARAMETERS;
}
if ("typecasts".equals(name)) {
return CASTS;
}
if ("cases".equals(name)) {
return CASE;
}
if ("labelledstatement".equals(name)) {
return LABEL;
}
if ("enumvalues".equals(name) || "elementvalues".equals(name)) {
return VALUES;
}
if ("throwntypes".equals(name)) {
return THROW;
}
if ("value".equals(name) || "returnedexpression".equals(name) || "expressions".equals(name)) {
return EXPRESSION;
}
if ("asserted".equals(name)) {
return CONDITION;
}
if ("parameters".equals(name)) {
return PARAMETER;
}
if ("typemembers".equals(name)) {
return MEMBERS;
}
if ("throwexpression".equals(name)) {
return THROW;
}
if ("returntype".equals(name)
|| "componenttype".equals(name)
|| "annotationtype".equals(name)) {
return TYPE;
}
if ("caseexpression".equals(name)) {
return CASE;
}
if ("elseexpression".equals(name) || "elsestatement".equals(name)) {
return ELSE;
}
if ("thenexpression".equals(name) || "thenstatement".equals(name)) {
return THEN;
}
if ("righthandoperand".equals(name)) {
return RIGHT_OPERAND;
}
if ("lefthandoperand".equals(name)) {
return LEFT_OPERAND;
}
if ("pack".equals(name) || "packs".equals(name)) {
return PACKAGE;
}
if ("superclass".equals(name)) {
return SUPER_TYPE;
}
if ("name".equals(name) || "simplename".equals(name)) {
return NAME;
}
return null;
}

/**
* Get the camel case representation of the name
* @return the name in camel case
*/
public String getCamelCaseName() {
String s = name().toLowerCase();
int i = s.indexOf("_");
if (i != -1) {
s = s.substring(0, i) + Character.toUpperCase(s.charAt(i + 1)) + s.substring(i + 2);
}
return s;
}

@Override
public String toString() {
return getCamelCaseName();
}
}
10 changes: 5 additions & 5 deletions src/main/java/spoon/reflect/path/impl/CtRolePathElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtExecutable;
import spoon.reflect.declaration.CtField;
import spoon.reflect.path.CtPathRole;
import spoon.reflect.path.CtRole;
import spoon.reflect.visitor.CtInheritanceScanner;

import java.util.Collection;
Expand Down Expand Up @@ -72,7 +72,7 @@ public <R> void scanCtExecutable(CtExecutable<R> e) {
public <T> void visitCtField(CtField<T> e) {
super.visitCtField(e);

if (role == CtPathRole.DEFAULT_VALUE && e.getDefaultExpression() != null) {
if (role == CtRole.DEFAULT_EXPRESSION && e.getDefaultExpression() != null) {
matchs.add(e.getDefaultExpression());
}
}
Expand All @@ -94,13 +94,13 @@ public void visitCtIf(CtIf e) {
}
}

private final CtPathRole role;
private final CtRole role;

public CtRolePathElement(CtPathRole role) {
public CtRolePathElement(CtRole role) {
this.role = role;
}

public CtPathRole getRole() {
public CtRole getRole() {
return role;
}

Expand Down
9 changes: 5 additions & 4 deletions src/test/java/spoon/test/path/PathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import spoon.reflect.path.CtPath;
import spoon.reflect.path.CtPathBuilder;
import spoon.reflect.path.CtPathException;
import spoon.reflect.path.CtPathRole;
import spoon.reflect.path.CtRole;
import spoon.reflect.path.CtPathStringBuilder;

import java.util.Arrays;
Expand Down Expand Up @@ -72,7 +72,8 @@ public void testBuilderMethod() throws Exception {
@Test
public void testBuilder() {
equals(
new CtPathBuilder().recursiveWildcard().name("toto").role(CtPathRole.DEFAULT_VALUE).build(),
new CtPathBuilder().recursiveWildcard().name("toto").role(
CtRole.DEFAULT_EXPRESSION).build(),

factory.Package().get("spoon.test.path").getType("Foo").getField("toto").getDefaultExpression()
);
Expand All @@ -96,7 +97,7 @@ public void testPathFromString() throws Exception {
CtLiteral<String> literal = factory.Core().createLiteral();
literal.setValue("salut");
literal.setType(literal.getFactory().Type().STRING);
equals(new CtPathStringBuilder().fromString(".spoon.test.path.Foo.toto#defaultValue"), literal);
equals(new CtPathStringBuilder().fromString(".spoon.test.path.Foo.toto#defaultExpression"), literal);
}

@Test
Expand Down Expand Up @@ -131,7 +132,7 @@ public void toStringTest() throws Exception {
comparePath(".spoon.test.path.Foo/CtMethod");
comparePath(".spoon.test.path.Foo.foo#body[index=0]");
comparePath(".spoon.test.path.Foo.bar/CtParameter");
comparePath(".spoon.test.path.Foo.toto#defaultValue");
comparePath(".spoon.test.path.Foo.toto#defaultExpression");
comparePath(".spoon.test.path.Foo.*#body[index=0]");
comparePath(".**/CtIf#else");
comparePath(".**#else");
Expand Down