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

review refactor: compute CtRole#forName without synonym #1702

Merged
merged 3 commits into from
Nov 12, 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
109 changes: 14 additions & 95 deletions src/main/java/spoon/reflect/path/CtRole.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,99 +95,11 @@ public enum CtRole {
* @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];
for (CtRole role : CtRole.values()) {
if (role.getCamelCaseName().toLowerCase().equals(name.toLowerCase()) || role.name().equals(name)) {
return role;
}
}
name = name.toLowerCase();
if ("implicit".equals(name)) {
return IS_IMPLICIT;
}
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 DIMENSION;
}
if ("actualtypearguments".equals(name)) {
return TYPE_PARAMETER;
}
if ("formalcttypeparameters".equals(name)) {
return TYPE_PARAMETER;
}
if ("typecasts".equals(name)) {
return CAST;
}
if ("cases".equals(name)) {
return CASE;
}
if ("enumvalues".equals(name) || "elementvalues".equals(name)) {
return VALUE;
}
if ("throwntypes".equals(name)) {
return THROWN;
}
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 FIELD;
}
if ("throwexpression".equals(name)) {
return THROWN;
}
if ("declaringtype".equals(name)) {
return DECLARING_TYPE;
}
if ("boundingtype".equals(name)) {
return BOUNDING_TYPE;
}
if ("returntype".equals(name)
|| "componenttype".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 SUB_PACKAGE;
}
if ("superclass".equals(name)) {
return SUPER_TYPE;
}
if ("name".equals(name) || "simplename".equals(name)) {
return NAME;
}
return null;
}

Expand All @@ -197,11 +109,18 @@ public static CtRole fromName(String name) {
*/
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);
String[] tokens = s.split("_");
if (tokens.length == 1) {
return s;
} else {
StringBuilder buffer = new StringBuilder(tokens[0]);
for (int i = 1; i < tokens.length; i++) {
String t = tokens[i];
buffer.append(Character.toUpperCase(t.charAt(0)));
buffer.append(t.substring(1));
}
return buffer.toString();
}
return s;
}

@Override
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/spoon/test/role/TestCtRole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package spoon.test.role;

import org.junit.Test;
import spoon.reflect.path.CtRole;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

public class TestCtRole {
@Test
public void testGetCtRoleByName() {
// contract: one should be able to get CtRole based on its name (without '_' whatever the case is, or with '_' in uppercase)

String name = "DECLARING_TYPE"; // exactly the same name: OK
assertEquals(CtRole.DECLARING_TYPE, CtRole.fromName(name));

name = "declaringType"; // camel case: OK
assertEquals(CtRole.DECLARING_TYPE, CtRole.fromName(name));

name = "declaringtype"; // lower case: OK
assertEquals(CtRole.DECLARING_TYPE, CtRole.fromName(name));

name = "declaring_type"; // lower case with underscore: not accepted
assertNull(CtRole.fromName(name));

for (CtRole role : CtRole.values()) {
assertEquals(role, CtRole.fromName(role.name().replaceAll("_", "").toLowerCase()));
}


}
}