Skip to content

Commit

Permalink
Merge remote-tracking branch 'inria/master' into add-ctimport
Browse files Browse the repository at this point in the history
  • Loading branch information
surli committed Nov 21, 2017
2 parents c1a8d44 + eaf70fb commit e2c216c
Show file tree
Hide file tree
Showing 20 changed files with 206 additions and 29 deletions.
2 changes: 1 addition & 1 deletion chore/travis/travis-verify.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
source /opt/jdk_switcher/jdk_switcher.sh
pip install --user CommonMark requests pygithub

jdk_switcher use oraclejdk9 && mvn -Djava.src.version=1.9 verify site install -DskipTests && python ./chore/check-links-in-doc.py
jdk_switcher use oraclejdk9 && mvn -Djava.src.version=1.9 verify site javadoc:jar install -DskipTests && python ./chore/check-links-in-doc.py
13 changes: 11 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<groupId>fr.inria.gforge.spoon</groupId>
<artifactId>spoon-core</artifactId>
<packaging>jar</packaging>
<version>6.0.0-SNAPSHOT</version>
<version>7.0.0-SNAPSHOT</version>
<name>Spoon Core</name>
<description>Spoon is a tool for meta-programming, analysis and transformation of Java programs.</description>
<url>http://spoon.gforge.inria.fr/</url>
Expand Down Expand Up @@ -240,7 +240,11 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>

Expand Down Expand Up @@ -508,6 +512,11 @@
</lifecycleMappingMetadata>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.0.0-M1</version>
</plugin>
</plugins>
</pluginManagement>
<extensions>
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/spoon/processing/ProcessorPropertiesImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,24 @@
*/
package spoon.processing;

import org.apache.commons.lang3.ClassUtils;

import java.util.HashMap;
import java.util.Map;

public class ProcessorPropertiesImpl implements ProcessorProperties {

private final Map<String, Object> _properties = new HashMap<>();

public <T> T get(Class<T> type, String name) {
return (T) _properties.get(name);
if (type.isPrimitive()) {
type = (Class<T>) ClassUtils.primitiveToWrapper(type);
}
T result = (T) _properties.get(name);
if (result == null) {
return null;
} else {
return (type.isAssignableFrom(result.getClass())) ? result : null;
}
}

public void set(String name, Object o) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/spoon/reflect/declaration/CtElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ <E extends CtElement> List<E> getAnnotatedChildren(

/**
* @return a a single value (eg a CtElement), List, Set or Map depending on this `element` and `role`. Returned collections are read-only.
* @param the role of the returned attribute with respect to this element.
* @param role the role of the returned attribute with respect to this element.
*
* For instance, "klass.getValueByRole(CtRole.METHOD)" returns a list of methods.
*
Expand All @@ -327,7 +327,7 @@ <E extends CtElement> List<E> getAnnotatedChildren(

/**
* Sets a field according to a role.
* @param the role of the field to be set
* @param role the role of the field to be set
* @param value to be assigned to this field.
*/
<E extends CtElement, T> E setValueByRole(CtRole role, T value);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/spoon/reflect/factory/Factory.java
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ public interface Factory {
CtPackage createPackage(CtPackage parent, String simpleName);

/**
* @see CoreFactory#create(Class<? extends CtElement>)
* @see CoreFactory#create(Class)
*/
CtElement createElement(Class<? extends CtElement> klass);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* Ensures that all children nodes are visited once, a visit means three method
* calls, one call to "enter", one call to "exit" and one call to biScan.
*
* This class is generated automatically by the processor {@link spoon.generating.CtBiScannerGenerator}.
* This class is generated automatically by the processor spoon.generating.CtBiScannerGenerator.
*
* Is used by EqualsVisitor.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/spoon/support/reflect/package.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

<p>This package provides an implementation of the spoon.reflect package.</p>

<p>The classes of this package should not be instantiated directly, but by using {@link spoon.reflect.CtFactory}.
<p>The classes of this package should not be instantiated directly, but by using {@link spoon.reflect.factory.Factory}.

<h2>Related Documentation</h2>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/**
* Used to set all data in the cloned element.
*
* This class is generated automatically by the processor {@link spoon.generating.CloneVisitorGenerator}.
* This class is generated automatically by the processor spoon.generating.CloneVisitorGenerator.
*/
public class CloneBuilder extends spoon.reflect.visitor.CtInheritanceScanner {
private spoon.reflect.declaration.CtElement other;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/**
* Used to clone a given element.
*
* This class is generated automatically by the processor {@link spoon.generating.CloneVisitorGenerator}.
* This class is generated automatically by the processor spoon.generating.CloneVisitorGenerator.
*/
public class CloneVisitor extends spoon.reflect.visitor.CtScanner {
private final spoon.support.visitor.equals.CloneHelper cloneHelper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/**
* Used to replace an element by another one.
*
* This class is generated automatically by the processor {@link spoon.generating.ReplacementVisitorGenerator}.
* This class is generated automatically by the processor spoon.generating.ReplacementVisitorGenerator.
*/
public class ReplacementVisitor extends spoon.reflect.visitor.CtScanner {
private spoon.reflect.declaration.CtElement original;
Expand Down
19 changes: 16 additions & 3 deletions src/main/java/spoon/testing/utils/ProcessorUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package spoon.testing.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.log4j.Level;
import spoon.Launcher;
import spoon.SpoonException;
Expand All @@ -30,6 +31,8 @@
import java.util.Collection;

public final class ProcessorUtils {
private static final ObjectMapper converter = new ObjectMapper();

private ProcessorUtils() {
throw new AssertionError();
}
Expand All @@ -52,9 +55,19 @@ public static void initProperties(Processor<?> p, ProcessorProperties properties
throw new SpoonException(e);
}
} else {
p.getFactory().getEnvironment().report(p, Level.WARN,
"No value found for property '" + f.getName() + "' in processor " + p.getClass()
.getName());
obj = properties.get(String.class, f.getName());
if (obj != null) {
try {
obj = converter.readValue((String) obj, f.getType());
f.setAccessible(true);
f.set(p, obj);
} catch (Exception e) {
throw new SpoonException("Error while assigning the value to " + f.getName(), e);
}
} else {
p.getFactory().getEnvironment().report(p, Level.WARN,
"No value found for property '" + f.getName() + "' in processor " + p.getClass().getName());
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/spoon/MavenLauncherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public void spoonMavenLauncherTest() {
// without the tests
MavenLauncher launcher = new MavenLauncher("./", MavenLauncher.SOURCE_TYPE.APP_SOURCE);

assertEquals(5, launcher.getEnvironment().getSourceClasspath().length);
assertEquals(7, launcher.getEnvironment().getSourceClasspath().length);
// 52 because of the sub folders of src/main/java
assertEquals(52, launcher.getModelBuilder().getInputSources().size());

Expand Down
6 changes: 5 additions & 1 deletion src/test/java/spoon/generating/CloneVisitorGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ 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(1);
List<CtExpression> invArgs = ((CtInvocation) clone.getBody().getStatement(i)).getArguments();
if (invArgs.size() <= 1) {
throw new RuntimeException("You forget the role argument in line "+i+" of method "+element.getSimpleName()+" from "+element.getDeclaringType().getQualifiedName());
}
final CtInvocation targetInvocation = (CtInvocation) invArgs.get(1);
if ("getValue".equals(targetInvocation.getExecutable().getSimpleName()) && "CtLiteral".equals(targetInvocation.getExecutable().getDeclaringType().getSimpleName())) {
clone.getBody().getStatement(i--).delete();
continue;
Expand Down
6 changes: 5 additions & 1 deletion src/test/java/spoon/generating/CtBiScannerGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ 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(1);
List<CtExpression> invArgs = ((CtInvocation) clone.getBody().getStatement(i)).getArguments();
if (invArgs.size() <= 1) {
throw new RuntimeException("You forget the role argument in line "+i+" of method "+element.getSimpleName()+" from "+element.getDeclaringType().getQualifiedName());
}
final CtInvocation targetInvocation = (CtInvocation) invArgs.get(1);
if ("getValue".equals(targetInvocation.getExecutable().getSimpleName()) && "CtLiteral".equals(targetInvocation.getExecutable().getDeclaringType().getSimpleName())) {
clone.getBody().getStatement(i--).delete();
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/**
* Used to clone a given element.
*
* This class is generated automatically by the processor {@link spoon.generating.CloneVisitorGenerator}.
* This class is generated automatically by the processor spoon.generating.CloneVisitorGenerator.
*/
class CloneVisitorTemplate extends CtScanner {
private final CloneHelper cloneHelper;
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/spoon/generating/replace/ReplaceScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ public <T> void visitCtMethod(CtMethod<T> element) {
for (int i = 1; i < element.getBody().getStatements().size() - 1; 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
/**
* Used to replace an element by another one.
*
* This class is generated automatically by the processor {@link spoon.generating.ReplacementVisitorGenerator}.
* This class is generated automatically by the processor spoon.generating.ReplacementVisitorGenerator.
*/
class ReplacementVisitor extends CtScanner {
public static void replace(CtElement original, CtElement replace) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* Ensures that all children nodes are visited once, a visit means three method
* calls, one call to "enter", one call to "exit" and one call to biScan.
*
* This class is generated automatically by the processor {@link spoon.generating.CtBiScannerGenerator}.
* This class is generated automatically by the processor spoon.generating.CtBiScannerGenerator.
*
* Is used by EqualsVisitor.
*/
Expand Down
16 changes: 9 additions & 7 deletions src/test/java/spoon/reflect/visitor/CtScannerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,16 @@ public boolean matches(CtInvocation element) {
} else {
c.nbChecks++;
//System.out.println(invocation.toString());

// contract: the scan method is called with the same role as the one set on field / property
CtRole expectedRole = metaModel.getRoleOfMethod((CtMethod<?>)invocation.getExecutable().getDeclaration());
CtInvocation<?> scanInvocation = invocation.getParent(CtInvocation.class);
String realRoleName = ((CtFieldRead<?>) scanInvocation.getArguments().get(0)).getVariable().getSimpleName();
if(expectedRole.name().equals(realRoleName) == false) {
problems.add("Wrong role " + realRoleName + " used in " + scanInvocation.getPosition());
}
}
// contract: the scan method is called with the same role as the one set on field / property
CtRole expectedRole = metaModel.getRoleOfMethod((CtMethod<?>) invocation.getExecutable().getDeclaration());
CtInvocation<?> scanInvocation = invocation.getParent(CtInvocation.class);
String realRoleName = ((CtFieldRead<?>) scanInvocation.getArguments().get(0)).getVariable().getSimpleName();
if (expectedRole.name().equals(realRoleName) == false) {
problems.add("Wrong role " + realRoleName + " used in " + scanInvocation.getPosition());
}

});
calledMethods.removeAll(checkedMethods);

Expand Down
Loading

0 comments on commit e2c216c

Please sign in to comment.