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

New implementation for replace #632

Merged
merged 8 commits into from
Apr 28, 2016
66 changes: 66 additions & 0 deletions src/main/java/spoon/generating/GeneratingTypeProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Copyright (C) 2006-2015 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.generating;

import spoon.generating.replace.ReplaceScanner;
import spoon.processing.AbstractProcessor;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.ModifierKind;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.CtScanner;
import spoon.reflect.visitor.ReferenceFilter;

import java.util.List;

import static spoon.generating.replace.ReplaceScanner.GENERATING_REPLACE_VISITOR;
import static spoon.generating.replace.ReplaceScanner.TARGET_REPLACE_PACKAGE;

public class GeneratingTypeProcessor extends AbstractProcessor<CtType<?>> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to ReplacementVisitorGenerator

@Override
public boolean isToBeProcessed(CtType<?> candidate) {
return CtScanner.class.getName().equals(candidate.getQualifiedName()) && super.isToBeProcessed(candidate);
}

@Override
public void process(CtType<?> element) {
new ReplaceScanner(createReplacementVisitor()).scan(getFactory().Class().get(CtScanner.class));
}

private CtClass<Object> createReplacementVisitor() {
final CtPackage aPackage = getFactory().Package().getOrCreate(TARGET_REPLACE_PACKAGE);
final CtClass<Object> target = getFactory().Class().get(GENERATING_REPLACE_VISITOR);
target.addModifier(ModifierKind.PUBLIC);
aPackage.addType(target);
final List<CtTypeReference> references = target.getReferences(new ReferenceFilter<CtTypeReference>() {
@Override
public boolean matches(CtTypeReference reference) {
return reference != null && GENERATING_REPLACE_VISITOR.equals(reference.getQualifiedName());
}

@Override
public Class<CtTypeReference> getType() {
return CtTypeReference.class;
}
});
for (CtTypeReference reference : references) {
reference.setPackage(aPackage.getReference());
}
return target;
}
}
31 changes: 31 additions & 0 deletions src/main/java/spoon/generating/replace/CtListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright (C) 2006-2015 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.generating.replace;

import spoon.reflect.declaration.CtElement;

class CtListener implements ReplaceListener<CtElement> {
private CtElement element;

CtListener(CtElement element) {
this.element = element;
}

@Override
public void set(CtElement replace) {
}
}
23 changes: 23 additions & 0 deletions src/main/java/spoon/generating/replace/ReplaceListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright (C) 2006-2015 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.generating.replace;

import spoon.reflect.declaration.CtElement;

public interface ReplaceListener<T extends CtElement> {
void set(T replace);
}
213 changes: 213 additions & 0 deletions src/main/java/spoon/generating/replace/ReplaceScanner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
/**
* Copyright (C) 2006-2015 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.generating.replace;

import spoon.SpoonException;
import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtConstructorCall;
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtConstructor;
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.CtInterface;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtParameter;
import spoon.reflect.declaration.CtType;
import spoon.reflect.factory.Factory;
import spoon.reflect.reference.CtExecutableReference;
import spoon.reflect.reference.CtTypeParameterReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.CtScanner;
import spoon.reflect.visitor.ReferenceFilter;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ReplaceScanner extends CtScanner {
public static final String TARGET_REPLACE_PACKAGE = "spoon.support.visitor.replace";
public static final String GENERATING_REPLACE_PACKAGE = "spoon.generating.replace";
public static final String GENERATING_REPLACE_VISITOR = GENERATING_REPLACE_PACKAGE + ".ReplacementVisitor";

private final List<String> excludes = Collections.singletonList("spoon.reflect.code.CtLiteral#getValue()");
private final Map<String, CtClass> listeners = new HashMap<String, CtClass>();
private final CtClass<Object> target;
private final CtExecutableReference<?> element;
private final CtExecutableReference<?> list;
private final CtExecutableReference<?> map;
private final CtExecutableReference<?> set;

public ReplaceScanner(CtClass<Object> target) {
this.target = target;
this.element = target.getMethodsByName("replaceElementIfExist").get(0).getReference();
this.list = target.getMethodsByName("replaceInListIfExist").get(0).getReference();
this.map = target.getMethodsByName("replaceInMapIfExist").get(0).getReference();
this.set = target.getMethodsByName("replaceInSetIfExist").get(0).getReference();
}

@Override
public <T> void visitCtMethod(CtMethod<T> element) {
if (!element.getSimpleName().startsWith("visitCt")) {
return;
}

Factory factory = element.getFactory();
CtMethod<T> clone = factory.Core().clone(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);
if (excludes.contains(getter.getExecutable().toString())) {
continue;
}
Class actualClass = getter.getType().getActualClass();
CtInvocation<?> invocation = createInvocation(factory, element, inv, getter, actualClass);
clone.getBody().addStatement(invocation);
}
target.addMethod(clone);
}

private <T> CtInvocation<?> createInvocation(Factory factory, CtMethod<T> candidate, CtInvocation current, CtInvocation getter, Class getterTypeClass) {
CtInvocation<?> invocation;
if (getterTypeClass.equals(Collection.class) || getterTypeClass.equals(List.class)) {
invocation = factory.Code().createInvocation(null, this.list, current.getArguments());
} else if (getterTypeClass.equals(Map.class)) {
invocation = factory.Code().createInvocation(null, this.map, current.getArguments());
} else if (getterTypeClass.equals(Set.class)) {
invocation = factory.Code().createInvocation(null, this.set, current.getArguments());
} else {
invocation = factory.Code().createInvocation(null, this.element, current.getArguments());
// Listener
final String name = getter.getExecutable().getSimpleName().substring(3);
final String listenerName = getter.getExecutable().getDeclaringType().getSimpleName() + name + "ReplaceListener";

CtClass listener;
if (listeners.containsKey(listenerName)) {
listener = listeners.get(listenerName);
} else {
final CtTypeReference getterType = getGetterType(factory, getter);
listener = createListenerClass(factory, listenerName, getterType);
final CtMethod setter = getSetter(name, getter.getTarget().getType().getDeclaration());
final CtField field = updateField(listener, setter.getDeclaringType().getReference());
updateConstructor(listener, setter.getDeclaringType().getReference());
updateSetter(factory, (CtMethod<?>) listener.getMethodsByName("set").get(0), getterType, field, setter);
listeners.put(listenerName, listener);
}

invocation.addArgument(getConstructorCall(listener, factory.Code().createVariableRead(candidate.getParameters().get(0).getReference(), false)));
}
return invocation;
}

private CtTypeReference getGetterType(Factory factory, CtInvocation getter) {
CtTypeReference getterType;
final CtTypeReference type = getter.getType();
if (type instanceof CtTypeParameterReference) {
getterType = getTypeFromTypeParameterReference((CtTypeParameterReference) getter.getExecutable().getDeclaration().getType());
} else {
getterType = factory.Core().clone(type);
}
getterType.getActualTypeArguments().clear();
return getterType;
}

private CtTypeReference getTypeFromTypeParameterReference(CtTypeParameterReference ctTypeParameterRef) {
final CtMethod parentMethod = ctTypeParameterRef.getParent(CtMethod.class);
for (CtTypeReference<?> formal : parentMethod.getFormalTypeParameters()) {
if (formal.getSimpleName().equals(ctTypeParameterRef.getSimpleName())) {
return ((CtTypeParameterReference) formal).getBoundingType();
}
}
final CtInterface parentInterface = ctTypeParameterRef.getParent(CtInterface.class);
for (CtTypeReference<?> formal : parentInterface.getFormalTypeParameters()) {
if (formal.getSimpleName().equals(ctTypeParameterRef.getSimpleName())) {
return ((CtTypeParameterReference) formal).getBoundingType();
}
}
throw new SpoonException("Can't get the type of the CtTypeParameterReference " + ctTypeParameterRef);
}

private CtClass createListenerClass(Factory factory, String listenerName, CtTypeReference getterType) {
CtClass listener;
listener = factory.Core().clone(factory.Class().get(GENERATING_REPLACE_PACKAGE + ".CtListener"));
listener.setSimpleName(listenerName);
target.addNestedType(listener);
final List<CtTypeReference> references = listener.getReferences(new ReferenceFilter<CtTypeReference>() {
@Override
public boolean matches(CtTypeReference reference) {
return reference != null && (GENERATING_REPLACE_PACKAGE + ".CtListener").equals(reference.getQualifiedName());
}

@Override
public Class<CtTypeReference> getType() {
return CtTypeReference.class;
}
});
for (CtTypeReference reference : references) {
reference.setPackage(listener.getPackage().getReference());
}
final CtTypeReference theInterface = listener.getSuperInterfaces().toArray(new CtTypeReference[listener.getSuperInterfaces().size()])[0];
theInterface.getActualTypeArguments().clear();
theInterface.addActualTypeArgument(getterType);
return listener;
}

private CtParameter<?> updateConstructor(CtClass listener, CtTypeReference type) {
final CtConstructor ctConstructor = (CtConstructor) listener.getConstructors().toArray(new CtConstructor[listener.getConstructors().size()])[0];
final CtParameter<?> aParameter = (CtParameter<?>) ctConstructor.getParameters().get(0);
aParameter.setType(type);
return aParameter;
}

private CtField updateField(CtClass listener, CtTypeReference<?> type) {
final CtField field = listener.getField("element");
field.setType(type);
return field;
}

private void updateSetter(Factory factory, CtMethod<?> setListener, CtTypeReference getterType, CtField<?> field, CtMethod setter) {
setListener.getParameters().get(0).setType(getterType);

CtInvocation ctInvocation = factory.Code().createInvocation(//
factory.Code().createVariableRead(field.getReference(), false), //
setter.getReference(), //
factory.Code().createVariableRead(setListener.getParameters().get(0).getReference(), false) //
);
CtBlock ctBlock = factory.Code().createCtBlock(ctInvocation);
setListener.setBody(ctBlock);
}

private CtMethod getSetter(String name, CtType declaration) {
Set<CtMethod> allMethods = declaration.getAllMethods();
CtMethod setter = null;
for (CtMethod aMethod : allMethods) {
if (("set" + name).equals(aMethod.getSimpleName())) {
setter = aMethod;
break;
}
}
return setter;
}

private CtConstructorCall<?> getConstructorCall(CtClass listener, CtExpression argument) {
return listener.getFactory().Code().createConstructorCall(listener.getReference(), argument);
}
}
Loading