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

[fix #304] Refactor Snippet helper #443

Merged
merged 3 commits into from
Dec 8, 2015
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
4 changes: 2 additions & 2 deletions src/main/java/spoon/reflect/factory/ClassFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public <T> CtClass<T> create(CtPackage owner, String simpleName) {
CtClass<T> c = factory.Core().createClass();
c.setSimpleName(simpleName);
if (owner.getTypes().contains(c)) {
owner.getTypes().remove(c);
owner.removeType(c);
}
owner.getTypes().add(c);
owner.addType(c);
c.setParent(owner);
return c;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/spoon/reflect/factory/PackageFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public CtElement getParent() throws ParentNotInitializedException {

@Override
public String getSimpleName() {
return "";
return super.getSimpleName();
}

@Override
Expand Down
121 changes: 36 additions & 85 deletions src/main/java/spoon/support/compiler/SnippetCompilationHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,99 +12,76 @@
import spoon.reflect.code.CtReturn;
import spoon.reflect.code.CtStatement;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtParameter;
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.ModifierKind;
import spoon.reflect.factory.Factory;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.Filter;
import spoon.reflect.visitor.Query;
import spoon.support.compiler.jdt.JDTSnippetCompiler;
import spoon.support.reflect.declaration.CtElementImpl;

public class SnippetCompilationHelper {

private static final String WRAPPER_CLASS_NAME = "Wrapper";
private static final String WRAPPER_METHOD_NAME = "wrap";

public static void compileAndReplaceSnippetsIn(CtType<?> c) {
Factory f = c.getFactory();
CtType<?> workCopy = c;
Set<ModifierKind> backup = EnumSet.noneOf(ModifierKind.class);
backup.addAll(workCopy.getModifiers());

workCopy.getModifiers().remove(ModifierKind.PUBLIC);
workCopy.removeModifier(ModifierKind.PUBLIC);

try {
build(f, workCopy.toString());
} finally {
// restore modifiers
c.setModifiers(backup);
}

}

public static CtStatement compileStatement(CtCodeSnippetStatement st)
throws SnippetCompilationError {
return internalCompileStatement(st);
}

private static CtStatement internalCompileStatement(CtStatement st) {
private static CtStatement internalCompileStatement(CtElement st) {
Factory f = st.getFactory();

CtClass<?> w = createWrapper(st, f);

compile(f, w);
build(f, w);

CtType<?> c = f.Type().get("Wrapper");
CtType<?> c = f.Type().get(WRAPPER_CLASS_NAME);

// Get the part we want

CtMethod<?> wrapper = Query.getElements(c, new Filter<CtMethod<?>>() {

public boolean matches(CtMethod<?> element) {
return element.getSimpleName().equals("wrap");
}

}).get(0);
CtMethod<?> wrapper = c.getMethod(WRAPPER_METHOD_NAME);

CtStatement ret = wrapper.getBody().getStatements().get(0);

// Clean up
c.getPackage().getTypes().remove(c);

// check typing?

return ret;
}

private static CtClass<?> createWrapper(CtStatement st, Factory f) {
CtClass<?> w = f.Class().create("Wrapper");

CtBlock<Void> body = f.Core().createBlock();

body.addStatement(st);

Set<ModifierKind> x = EnumSet.noneOf(ModifierKind.class);
@SuppressWarnings("unchecked")
public static <T> CtExpression<T> compileExpression(
CtCodeSnippetExpression<T> expr) throws SnippetCompilationError {

f.Method().create(
w,
x,
f.Type().createReference(void.class),
"wrap",
CtElementImpl.<CtParameter<?>>emptyList(),
CtElementImpl
.<CtTypeReference<? extends Throwable>>emptySet(),
body);
CtReturn<T> ret = (CtReturn<T>) internalCompileStatement(expr);

return w;
return ret.getReturnedExpression();
}

private static void compile(Factory f, CtType<?> w)
throws SnippetCompilationError {
private static void build(Factory f, CtType<?> w) {

String contents = w.toString();

build(f, contents);

}

private static void build(Factory f, String contents) {
Expand All @@ -113,63 +90,37 @@ private static void build(Factory f, String contents) {
try {
builder.build();
} catch (Exception e) {
throw new ModelBuildingException(
"snippet compilation error while compiling: " + contents, e);
throw new ModelBuildingException("snippet compilation error while compiling: " + contents, e);
}
}

@SuppressWarnings("unchecked")
public static <T> CtExpression<T> compileExpression(
CtCodeSnippetExpression<T> expr) throws SnippetCompilationError {
// create wrapping template

Factory f = expr.getFactory();
CtClass<?> w = createWrapper(expr, f);

String contents = w.toString();

build(f, contents);

CtType<?> c = f.Type().get("Wrapper");

// Get the part we want

CtMethod<T> wrapper = Query.getElements(c, new Filter<CtMethod<T>>() {

public boolean matches(CtMethod<T> element) {
return element.getSimpleName().equals("wrap");
}

}).get(0);

CtReturn<T> ret = (CtReturn<T>) wrapper.getBody().getStatements()
.get(0);
private static CtClass<?> createWrapper(CtElement element, Factory f) {
CtClass<?> w = f.Class().create(WRAPPER_CLASS_NAME);

// Clean up (delete wrapper from factory)
c.getPackage().getTypes().remove(c);

return ret.getReturnedExpression();
}

private static <R, B extends R> CtClass<?> createWrapper(
CtExpression<B> st, Factory f) {
CtClass<?> w = f.Class().create("Wrapper");

CtBlock<B> body = f.Core().createBlock();
CtReturn<B> ret = f.Core().createReturn();
ret.setReturnedExpression(st);
body.addStatement(ret);
w.getPackage().getTypes().remove(w);

CtBlock body = f.Core().createBlock();

CtTypeReference returnType = f.Type().VOID_PRIMITIVE;
if (element instanceof CtStatement) {
body.addStatement((CtStatement) element);
} else if (element instanceof CtExpression) {
CtReturn ret = f.Core().createReturn();
ret.setReturnedExpression((CtExpression) element);
body.addStatement(ret);
returnType = f.Type().OBJECT;
}

Set<ModifierKind> x = EnumSet.noneOf(ModifierKind.class);
Set<ModifierKind> modifiers = EnumSet.noneOf(ModifierKind.class);

f.Method().create(
w,
x,
f.Type().createReference(Object.class),
"wrap",
modifiers,
returnType,
WRAPPER_METHOD_NAME,
CtElementImpl.<CtParameter<?>>emptyList(),
CtElementImpl
.<CtTypeReference<? extends Throwable>>emptySet(),
CtElementImpl.<CtTypeReference<? extends Throwable>>emptySet(),
body);

return w;
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/spoon/test/snippets/SnippetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import org.junit.Test;
import spoon.reflect.code.CtBinaryOperator;
import spoon.reflect.code.CtCodeSnippetExpression;
import spoon.reflect.code.CtConstructorCall;
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.code.CtLocalVariable;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.factory.Factory;
import spoon.test.TestUtils;

Expand Down Expand Up @@ -63,4 +67,17 @@ public void testCompileSnippetSeveralTimes() throws Exception {
assertTrue(thirdCompile instanceof CtBinaryOperator);
assertEquals("1 > 3", thirdCompile.toString());
}

@Test
public void testCompileSnippetWithContext() throws Exception {
// contract: a snippet object can be compiled with a context in the factory.
try {
// Add a class in the context.
factory.Class().create("AClass");
// Try to compile a snippet with a context.
factory.Code().createCodeSnippetStatement("int i = 1;").compile();
} catch (ClassCastException e) {
fail();
}
}
}