Skip to content

Commit

Permalink
feature: CtScanner is CtRole aware
Browse files Browse the repository at this point in the history
  • Loading branch information
pvojtechovsky committed Oct 28, 2017
1 parent 40a1ce2 commit 5f5cd60
Show file tree
Hide file tree
Showing 11 changed files with 734 additions and 713 deletions.
4 changes: 2 additions & 2 deletions src/main/java/spoon/generating/CloneVisitorGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public <T> void visitCtMethod(CtMethod<T> element) {

// Changes body of the cloned method.
for (int i = 1; i < clone.getBody().getStatements().size() - 1; i++) {
final CtInvocation targetInvocation = (CtInvocation) ((CtInvocation) clone.getBody().getStatement(i)).getArguments().get(0);
final CtInvocation targetInvocation = (CtInvocation) ((CtInvocation) clone.getBody().getStatement(i)).getArguments().get(1);
if ("getValue".equals(targetInvocation.getExecutable().getSimpleName()) && "CtLiteral".equals(targetInvocation.getExecutable().getDeclaringType().getSimpleName())) {
clone.getBody().getStatement(i--).delete();
continue;
Expand Down Expand Up @@ -134,7 +134,7 @@ public <T> void visitCtMethod(CtMethod<T> element) {
* @param elementVarRead <code>anElement</code>.
*/
private CtInvocation<?> createSetter(CtInvocation scanInvocation, CtVariableAccess<CtElement> elementVarRead) {
final CtInvocation<?> getter = (CtInvocation<?>) scanInvocation.getArguments().get(0);
final CtInvocation<?> getter = (CtInvocation<?>) scanInvocation.getArguments().get(1);
final String getterName = getter.getExecutable().getSimpleName();
final CtExecutableReference<Object> setterRef = factory.Executable().createReference("void CtElement#set" + getterName.substring(3, getterName.length()) + "()");
final CtExecutableReference<Object> cloneRef = factory.Executable().createReference("CtElement spoon.support.visitor.equals.CloneHelper#clone()");
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/spoon/generating/CtBiScannerGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void process() {
clone.getBody().insertBegin(peek);

for (int i = 2; i < clone.getBody().getStatements().size() - 1; i++) {
final CtInvocation targetInvocation = (CtInvocation) ((CtInvocation) clone.getBody().getStatement(i)).getArguments().get(0);
final CtInvocation targetInvocation = (CtInvocation) ((CtInvocation) clone.getBody().getStatement(i)).getArguments().get(1);
if ("getValue".equals(targetInvocation.getExecutable().getSimpleName()) && "CtLiteral".equals(targetInvocation.getExecutable().getDeclaringType().getSimpleName())) {
clone.getBody().getStatement(i--).delete();
continue;
Expand All @@ -81,12 +81,12 @@ public void process() {

// Creates other inv.
final CtVariableAccess<?> otherRead = factory.Code().createVariableRead(peek.getReference(), false);
replace.addArgument(factory.Code().createInvocation(otherRead, ((CtInvocation) replace.getArguments().get(0)).getExecutable()));
replace.addArgument(factory.Code().createInvocation(otherRead, ((CtInvocation) replace.getArguments().get(1)).getExecutable()));

if ("Map".equals(targetInvocation.getExecutable().getType().getSimpleName())) {
((CtExpression) replace.getArguments().get(0)).replace(factory.Code().createInvocation(targetInvocation, factory.Executable().createReference("List Map#values()")));
CtInvocation invocation = factory.Code().createInvocation(replace.getArguments().get(1).clone(), factory.Executable().createReference("List Map#values()"));
replace.getArguments().get(1).replace(invocation);
((CtExpression) replace.getArguments().get(1)).replace(factory.Code().createInvocation(targetInvocation, factory.Executable().createReference("List Map#values()")));
CtInvocation invocation = factory.Code().createInvocation(replace.getArguments().get(2).clone(), factory.Executable().createReference("List Map#values()"));
replace.getArguments().get(2).replace(invocation);
}

clone.getBody().getStatement(i).replace(replace);
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/spoon/generating/replace/ReplaceScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import spoon.reflect.visitor.CtScanner;
import spoon.reflect.visitor.filter.TypeFilter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -81,7 +82,10 @@ public <T> void visitCtMethod(CtMethod<T> element) {
clone.getBody().getStatements().clear();
for (int i = 1; i < element.getBody().getStatements().size() - 1; i++) {
CtInvocation inv = element.getBody().getStatement(i);
CtInvocation getter = (CtInvocation) inv.getArguments().get(0);
List<CtExpression<?>> invArgs = new ArrayList<>(inv.getArguments());
//remove role argument
invArgs.remove(0);
CtInvocation getter = (CtInvocation) invArgs.get(0);

if (clone.getComments().size() == 0) {
// Add auto-generated comment.
Expand All @@ -91,26 +95,26 @@ public <T> void visitCtMethod(CtMethod<T> element) {
clone.addComment(comment);
}
Class actualClass = getter.getType().getActualClass();
CtInvocation<?> invocation = createInvocation(factory, element, inv, getter, actualClass);
CtInvocation<?> invocation = createInvocation(factory, element, invArgs, getter, actualClass);
clone.getBody().addStatement(invocation);
}
target.addMethod(clone);
}

private <T> CtInvocation<?> createInvocation(Factory factory, CtMethod<T> candidate, CtInvocation current, CtInvocation getter, Class getterTypeClass) {
private <T> CtInvocation<?> createInvocation(Factory factory, CtMethod<T> candidate, List<CtExpression<?>> invArgs, CtInvocation getter, Class getterTypeClass) {
CtInvocation<?> invocation;
Type type;
if (getterTypeClass.equals(Collection.class) || getterTypeClass.equals(List.class)) {
invocation = factory.Code().createInvocation(null, this.list, current.getArguments());
invocation = factory.Code().createInvocation(null, this.list, invArgs);
type = Type.LIST;
} else if (getterTypeClass.equals(Map.class)) {
invocation = factory.Code().createInvocation(null, this.map, current.getArguments());
invocation = factory.Code().createInvocation(null, this.map, invArgs);
type = Type.MAP;
} else if (getterTypeClass.equals(Set.class)) {
invocation = factory.Code().createInvocation(null, this.set, current.getArguments());
invocation = factory.Code().createInvocation(null, this.set, invArgs);
type = Type.SET;
} else {
invocation = factory.Code().createInvocation(null, this.element, current.getArguments());
invocation = factory.Code().createInvocation(null, this.element, invArgs);
type = Type.ELEMENT;
}
// Listener
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/spoon/reflect/visitor/CtAbstractBiScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package spoon.reflect.visitor;

import spoon.reflect.declaration.CtElement;
import spoon.reflect.path.CtRole;

import java.util.ArrayDeque;
import java.util.Collection;
Expand All @@ -38,6 +39,9 @@ protected void exit(CtElement e) {
protected boolean isNotEqual = false;

public boolean biScan(Collection<? extends CtElement> elements, Collection<? extends CtElement> others) {
return biScan(null, elements, others);
}
public boolean biScan(CtRole role, Collection<? extends CtElement> elements, Collection<? extends CtElement> others) {
if (isNotEqual) {
return isNotEqual;
}
Expand All @@ -53,11 +57,14 @@ public boolean biScan(Collection<? extends CtElement> elements, Collection<? ext
return fail();
}
for (Iterator<? extends CtElement> firstIt = elements.iterator(), secondIt = others.iterator(); (firstIt.hasNext()) && (secondIt.hasNext());) {
biScan(firstIt.next(), secondIt.next());
biScan(role, firstIt.next(), secondIt.next());
}
return isNotEqual;
}

public boolean biScan(CtRole role, CtElement element, CtElement other) {
return biScan(element, other);
}
public boolean biScan(CtElement element, CtElement other) {
if (isNotEqual) {
return isNotEqual;
Expand Down
Loading

0 comments on commit 5f5cd60

Please sign in to comment.