Skip to content
Closed
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
30 changes: 28 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.4.2</version>
<version>2.14.0</version>
</dependency>
<dependency>
<!-- support for compressed serialized ASTs -->
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.21</version>
<version>1.22</version>
</dependency>
<dependency>
<!-- to reproduce JTD error with nullable annotation -->
Expand Down Expand Up @@ -227,6 +227,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
Expand All @@ -252,6 +253,30 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
Comment on lines +256 to +267
Copy link
Author

Choose a reason for hiding this comment

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

Suggested change
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand All @@ -278,6 +303,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M6</version>
<configuration>
<classpathDependencyExcludes>
<classpathDependencyExclude>ch.qos.logback:logback-classic</classpathDependencyExclude>
Expand Down
2 changes: 1 addition & 1 deletion spoon-pom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>3.0.0-M6</version>
<version>3.0.0-M7</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,6 @@ private CompilationUnit[] ignoreSyntaxErrors(CompilationUnit[] sourceUnits) {
}
}
this.initializeParser();
return sourceUnitList.toArray(new CompilationUnit[sourceUnitList.size()]);
return sourceUnitList.toArray(new CompilationUnit[0]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1389,7 +1389,7 @@ public static void replace(spoon.reflect.declaration.CtElement original, spoon.r
}

public static <E extends spoon.reflect.declaration.CtElement> void replace(spoon.reflect.declaration.CtElement original, java.util.Collection<E> replaces) {
new spoon.support.visitor.replace.ReplacementVisitor(original, replaces.toArray(new spoon.reflect.declaration.CtElement[replaces.size()])).scan(original.getParent());
new spoon.support.visitor.replace.ReplacementVisitor(original, replaces.toArray(new spoon.reflect.declaration.CtElement[0])).scan(original.getParent());
}

private spoon.reflect.declaration.CtElement original;
Expand Down
17 changes: 4 additions & 13 deletions src/test/java/spoon/generating/ReplacementVisitorGenerator.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
/**
* Copyright (C) 2006-2018 INRIA and contributors
* Spoon - http://spoon.gforge.inria.fr/
/*
* SPDX-License-Identifier: (MIT OR CECILL-C)
*
* 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.
* Copyright (C) 2006-2019 INRIA and contributors
*
* 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.
* Spoon is available either under the terms of the MIT License (see LICENSE-MIT.txt) of the Cecill-C License (see LICENSE-CECILL-C.txt). You as the user are entitled to choose the terms under which to adopt Spoon.
*/
package spoon.generating;

Expand Down
70 changes: 35 additions & 35 deletions src/test/java/spoon/generating/replace/ReplaceScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class ReplaceScanner extends CtScanner {
public static final String GENERATING_REPLACE_PACKAGE = "spoon.generating.replace";
public static final String GENERATING_REPLACE_VISITOR = GENERATING_REPLACE_PACKAGE + ".ReplacementVisitor";

private final Map<String, CtClass> listeners = new HashMap<>();
private final Map<String, CtClass<?>> listeners = new HashMap<>();
private final CtClass<Object> target;
private final CtExecutableReference<?> element;
private final CtExecutableReference<?> list;
Expand All @@ -76,14 +76,14 @@ public <T> void visitCtMethod(CtMethod<T> element) {
factory.Annotation().annotate(clone, Override.class);
clone.getBody().getStatements().clear();
for (int i = 1; i < element.getBody().getStatements().size() - 1; i++) {
CtInvocation inv = element.getBody().getStatement(i);
CtInvocation<?> inv = element.getBody().getStatement(i);
List<CtExpression<?>> invArgs = new ArrayList<>(inv.getArguments());
if (invArgs.size() <= 1) {
throw new RuntimeException("You forget the role argument in line " + i + " of method " + element.getSimpleName() + " from " + element.getDeclaringType().getQualifiedName());
}
//remove role argument
invArgs.remove(0);
CtInvocation getter = (CtInvocation) invArgs.get(0);
CtInvocation<?> getter = (CtInvocation<?>) invArgs.get(0);

if (clone.getComments().isEmpty()) {
// Add auto-generated comment.
Expand All @@ -92,7 +92,7 @@ public <T> void visitCtMethod(CtMethod<T> element) {
comment.setContent("auto-generated, see " + ReplacementVisitorGenerator.class.getName());
clone.addComment(comment);
}
Class actualClass = getter.getType().getActualClass();
Class<?> actualClass = getter.getType().getActualClass();
CtInvocation<?> invocation = createInvocation(factory, element, invArgs, getter, actualClass);
clone.getBody().addStatement(invocation);
}
Expand All @@ -101,7 +101,7 @@ public <T> void visitCtMethod(CtMethod<T> element) {

private static Set<String> modelCollectionTypes = new HashSet<>(Arrays.asList(ModelList.class.getName(), ElementNameMap.class.getName()));

private <T> CtInvocation<?> createInvocation(Factory factory, CtMethod<T> candidate, List<CtExpression<?>> invArgs, 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) || List.class.isAssignableFrom(getterTypeClass)) {
Expand All @@ -121,18 +121,18 @@ private <T> CtInvocation<?> createInvocation(Factory factory, CtMethod<T> candid
final String name = getter.getExecutable().getSimpleName().substring(3);
final String listenerName = getter.getExecutable().getDeclaringType().getSimpleName() + name + "ReplaceListener";

CtClass listener;
CtClass<?> listener;
if (listeners.containsKey(listenerName)) {
listener = listeners.get(listenerName);
} else {
final CtTypeReference getterType = getGetterType(factory, getter);
final CtTypeReference<?> getterType = getGetterType(factory, getter);
CtTypeReference<?> setterParamType = getterType;
if (modelCollectionTypes.contains(setterParamType.getQualifiedName())) {
setterParamType = factory.Type().createReference(Collection.class);
}
listener = createListenerClass(factory, listenerName, setterParamType, type);
final CtMethod setter = getSetter(name, getter.getTarget().getType().getDeclaration());
final CtField field = updateField(listener, setter.getDeclaringType().getReference());
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), setterParamType, field, setter);
// Add auto-generated comment.
Expand All @@ -147,9 +147,9 @@ private <T> CtInvocation<?> createInvocation(Factory factory, CtMethod<T> candid
return invocation;
}

private CtTypeReference getGetterType(Factory factory, CtInvocation getter) {
CtTypeReference getterType;
final CtTypeReference type = getter.getType();
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 {
Expand All @@ -159,14 +159,14 @@ private CtTypeReference getGetterType(Factory factory, CtInvocation getter) {
return getterType;
}

private CtTypeReference getTypeFromTypeParameterReference(CtTypeParameterReference ctTypeParameterRef) {
final CtMethod parentMethod = ctTypeParameterRef.getParent(CtMethod.class);
private CtTypeReference<?> getTypeFromTypeParameterReference(CtTypeParameterReference ctTypeParameterRef) {
final CtMethod<?> parentMethod = ctTypeParameterRef.getParent(CtMethod.class);
for (CtTypeParameter formal : parentMethod.getFormalCtTypeParameters()) {
if (formal.getSimpleName().equals(ctTypeParameterRef.getSimpleName())) {
return ((CtTypeParameterReference) formal).getBoundingType();
}
}
final CtInterface parentInterface = ctTypeParameterRef.getParent(CtInterface.class);
final CtInterface<?> parentInterface = ctTypeParameterRef.getParent(CtInterface.class);
for (CtTypeParameter formal : parentInterface.getFormalCtTypeParameters()) {
if (formal.getSimpleName().equals(ctTypeParameterRef.getSimpleName())) {
return formal.getReference().getBoundingType();
Expand All @@ -175,8 +175,8 @@ private CtTypeReference getTypeFromTypeParameterReference(CtTypeParameterReferen
throw new SpoonException("Can't get the type of the CtTypeParameterReference " + ctTypeParameterRef);
}

private CtClass createListenerClass(Factory factory, String listenerName, CtTypeReference getterType, Type type) {
CtClass listener;
private CtClass<?> createListenerClass(Factory factory, String listenerName, CtTypeReference<?> getterType, Type type) {
CtClass<?> listener;
// prototype class to use, we'll change its name and code later
listener = Launcher.parseClass("static class XXX implements ReplaceListener<CtElement> { \n"
+ "private final CtElement element XXX(CtElement element) { this.element = element; }\n"
Expand All @@ -185,13 +185,13 @@ private CtClass createListenerClass(Factory factory, String listenerName, CtType

listener.setSimpleName(listenerName);
target.addNestedType(listener);
final List<CtTypeReference> references = listener.getElements(new TypeFilter<CtTypeReference>(CtTypeReference.class) {
final List<CtTypeReference<?>> references = listener.getElements(new TypeFilter<CtTypeReference<?>>(CtTypeReference.class) {
@Override
public boolean matches(CtTypeReference reference) {
public boolean matches(CtTypeReference<?> reference) {
return (TARGET_REPLACE_PACKAGE + ".CtListener").equals(reference.getQualifiedName());
}
});
for (CtTypeReference reference : references) {
for (CtTypeReference<?> reference : references) {
reference.setPackage(listener.getPackage().getReference());
}
final CtTypeReference<Object> theInterface = factory.Class().createReference(TARGET_REPLACE_PACKAGE + "." + type.name);
Expand All @@ -202,38 +202,38 @@ public boolean matches(CtTypeReference reference) {
return listener;
}

private CtParameter<?> updateConstructor(CtClass listener, CtTypeReference type) {
final CtConstructor ctConstructor = (CtConstructor) listener.getConstructors().toArray(new CtConstructor[listener.getConstructors().size()])[0];
CtAssignment assign = (CtAssignment) ctConstructor.getBody().getStatement(1);
CtThisAccess fieldAccess = (CtThisAccess) ((CtFieldAccess) assign.getAssigned()).getTarget();
((CtTypeAccess) fieldAccess.getTarget()).getAccessedType().setImplicit(true);
private CtParameter<?> updateConstructor(CtClass<?> listener, CtTypeReference<?> type) {
final CtConstructor<?> ctConstructor = (CtConstructor<?>) listener.getConstructors().toArray(new CtConstructor[listener.getConstructors().size()])[0];
CtAssignment<?,?> assign = (CtAssignment<?,?>) ctConstructor.getBody().getStatement(1);
CtThisAccess<?> fieldAccess = (CtThisAccess<?>) ((CtFieldAccess<?>) assign.getAssigned()).getTarget();
((CtTypeAccess<?>) fieldAccess.getTarget()).getAccessedType().setImplicit(true);
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");
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) {
private void updateSetter(Factory factory, CtMethod<?> setListener, CtTypeReference<?> getterType, CtField<?> field, CtMethod<?> setter) {
setListener.getParameters().get(0).setType(getterType);

CtInvocation ctInvocation = factory.Code().createInvocation(//
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);
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) {
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;
Expand All @@ -242,7 +242,7 @@ private CtMethod getSetter(String name, CtType declaration) {
return setter;
}

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

Expand Down
19 changes: 5 additions & 14 deletions src/test/java/spoon/generating/replace/ReplacementVisitor.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
/**
* Copyright (C) 2006-2018 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.
/*
* SPDX-License-Identifier: (MIT OR CECILL-C)
*
* 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.
* Copyright (C) 2006-2019 INRIA and contributors
*
* 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.
* Spoon is available either under the terms of the MIT License (see LICENSE-MIT.txt) of the Cecill-C License (see LICENSE-CECILL-C.txt). You as the user are entitled to choose the terms under which to adopt Spoon.
*/
package spoon.generating.replace;

Expand Down Expand Up @@ -42,7 +33,7 @@ public static void replace(CtElement original, CtElement replace) {
new ReplacementVisitor(original, replace == null ? EMPTY : new CtElement[]{replace}).scan(original.getParent());
}
public static <E extends CtElement> void replace(CtElement original, Collection<E> replaces) {
new ReplacementVisitor(original, replaces.toArray(new CtElement[replaces.size()])).scan(original.getParent());
new ReplacementVisitor(original, replaces.toArray(new CtElement[0])).scan(original.getParent());
}

private CtElement original;
Expand Down