use of spoon.reflect.declaration.CtType in project Ex2Amplifier by STAMP-project.
the class MainGenerator method generateMainMethodFromTestMethod.
public static CtMethod<?> generateMainMethodFromTestMethod(CtMethod<?> testMethod, CtType<?> testClass) {
final Factory factory = testMethod.getFactory();
final CtBlock<?> blockMain = new AssertionRemover().removeAssertion(testMethod).getBody().clone();
final List<CtLiteral<?>> originalLiterals = blockMain.getElements(Ex2Amplifier.CT_LITERAL_TYPE_FILTER);
final int[] count = new int[] { 1 };
final List<CtLocalVariable<?>> localVariables = originalLiterals.stream().map(literal -> factory.createLocalVariable(Utils.getRealTypeOfLiteral(literal), "lit" + count[0]++, factory.createCodeSnippetExpression(createMakeRead(factory, literal)))).collect(Collectors.toList());
final CtMethod<?> mainMethod = initMainMethod(factory);
Collections.reverse(localVariables);
localVariables.forEach(blockMain::insertBegin);
Collections.reverse(localVariables);
final Iterator<CtLocalVariable<?>> iterator = localVariables.iterator();
blockMain.getElements(Ex2Amplifier.CT_LITERAL_TYPE_FILTER).forEach(literal -> literal.replace(factory.createVariableRead(iterator.next().getReference(), false)));
if (!testMethod.getThrownTypes().isEmpty()) {
final CtTry largeTryCatchBlock = createLargeTryCatchBlock(testMethod.getThrownTypes(), factory);
largeTryCatchBlock.setBody(blockMain);
mainMethod.setBody(largeTryCatchBlock);
} else {
mainMethod.setBody(blockMain);
}
removeNonStaticElement(mainMethod, testClass);
return mainMethod;
}
use of spoon.reflect.declaration.CtType in project spoon by INRIA.
the class TypeFactory method get.
/**
* Gets a type from its runtime Java class. If the class isn't in the spoon path,
* the class will be build from the Java reflection and will be marked as
* shadow (see {@link spoon.reflect.declaration.CtShadowable}).
*
* @param <T>
* actual type of the class
* @param cl
* the java class: note that this class should be Class<T> but it
* then poses problem when T is a generic type itself
*/
@SuppressWarnings("unchecked")
public <T> CtType<T> get(Class<?> cl) {
final CtType<T> aType = get(cl.getName());
if (aType == null) {
final CtType<T> shadowClass = (CtType<T>) this.shadowCache.get(cl);
if (shadowClass == null) {
CtType<T> newShadowClass;
try {
newShadowClass = new JavaReflectionTreeBuilder(createFactory()).scan((Class<T>) cl);
} catch (Throwable e) {
throw new SpoonClassNotFoundException("cannot create shadow class: " + cl.getName(), e);
}
newShadowClass.setFactory(factory);
newShadowClass.accept(new CtScanner() {
@Override
public void scan(CtElement element) {
if (element != null) {
element.setFactory(factory);
}
}
});
this.shadowCache.put(cl, newShadowClass);
return newShadowClass;
} else {
return shadowClass;
}
}
return aType;
}
use of spoon.reflect.declaration.CtType in project spoon by INRIA.
the class JDTImportBuilder method build.
// package visible method in a package visible class, not in the public API
void build() {
if (declarationUnit.imports == null || declarationUnit.imports.length == 0) {
return;
}
for (ImportReference importRef : declarationUnit.imports) {
String importName = importRef.toString();
if (!importRef.isStatic()) {
if (importName.endsWith("*")) {
int lastDot = importName.lastIndexOf(".");
String packageName = importName.substring(0, lastDot);
// only get package from the model by traversing from rootPackage the model
// it does not use reflection to achieve that
CtPackage ctPackage = this.factory.Package().get(packageName);
if (ctPackage != null) {
this.imports.add(factory.Type().createImport(ctPackage.getReference()));
}
} else {
CtType klass = this.getOrLoadClass(importName);
if (klass != null) {
this.imports.add(factory.Type().createImport(klass.getReference()));
}
}
} else {
int lastDot = importName.lastIndexOf(".");
String className = importName.substring(0, lastDot);
String methodOrFieldName = importName.substring(lastDot + 1);
CtType klass = this.getOrLoadClass(className);
if (klass != null) {
if (methodOrFieldName.equals("*")) {
this.imports.add(factory.Type().createImport(factory.Type().createWildcardStaticTypeMemberReference(klass.getReference())));
} else {
List<CtNamedElement> methodOrFields = klass.getElements(new NamedElementFilter<>(CtNamedElement.class, methodOrFieldName));
if (methodOrFields.size() > 0) {
CtNamedElement methodOrField = methodOrFields.get(0);
this.imports.add(factory.Type().createImport(methodOrField.getReference()));
}
}
}
}
}
spoonUnit.setImports(this.imports);
}
use of spoon.reflect.declaration.CtType in project spoon by INRIA.
the class ParentExiter method visitCtPackage.
@Override
public void visitCtPackage(CtPackage ctPackage) {
if (child instanceof CtType) {
if (ctPackage.getTypes().contains(child)) {
ctPackage.removeType((CtType<?>) child);
}
ctPackage.addType((CtType<?>) child);
if (child.getPosition() != null && child.getPosition().getCompilationUnit() != null) {
CompilationUnit cu = child.getPosition().getCompilationUnit();
List<CtType<?>> declaredTypes = new ArrayList<>(cu.getDeclaredTypes());
declaredTypes.add((CtType<?>) child);
cu.setDeclaredTypes(declaredTypes);
}
return;
}
super.visitCtPackage(ctPackage);
}
use of spoon.reflect.declaration.CtType in project spoon by INRIA.
the class StandardEnvironment method report.
@Override
public void report(Processor<?> processor, Level level, CtElement element, String message) {
StringBuffer buffer = new StringBuffer();
prefix(buffer, level);
// Adding message
buffer.append(message);
// Add sourceposition (javac format)
try {
CtType<?> type = (element instanceof CtType) ? (CtType<?>) element : element.getParent(CtType.class);
SourcePosition sp = element.getPosition();
if (sp == null) {
buffer.append(" (Unknown Source)");
} else {
buffer.append(" at " + type.getQualifiedName() + ".");
CtExecutable<?> exe = (element instanceof CtExecutable) ? (CtExecutable<?>) element : element.getParent(CtExecutable.class);
if (exe != null) {
buffer.append(exe.getSimpleName());
}
buffer.append("(" + sp.getFile().getName() + ":" + sp.getLine() + ")");
}
} catch (ParentNotInitializedException e) {
buffer.append(" (invalid parent)");
}
print(buffer.toString(), level);
}
Aggregations