use of spoon.reflect.reference.CtPackageReference in project dspot by STAMP-project.
the class AmplificationHelper method cloneTestMethod.
/**
* Clones a test method.
*
* Performs necessary integration with JUnit and adds timeout.
*
* @param method Method to be cloned
* @param suffix Suffix for the cloned method's name
* @return The cloned method
*/
private static CtMethod cloneTestMethod(CtMethod method, String suffix) {
CtMethod cloned_method = cloneMethod(method, suffix);
CtAnnotation testAnnotation = cloned_method.getAnnotations().stream().filter(annotation -> annotation.toString().contains("Test")).findFirst().orElse(null);
if (testAnnotation != null) {
cloned_method.removeAnnotation(testAnnotation);
}
testAnnotation = method.getFactory().Core().createAnnotation();
CtTypeReference<Object> ref = method.getFactory().Core().createTypeReference();
ref.setSimpleName("Test");
CtPackageReference refPackage = method.getFactory().Core().createPackageReference();
refPackage.setSimpleName("org.junit");
ref.setPackage(refPackage);
testAnnotation.setAnnotationType(ref);
Map<String, Object> elementValue = new HashMap<>();
elementValue.put("timeout", timeOutInMs);
testAnnotation.setElementValues(elementValue);
cloned_method.addAnnotation(testAnnotation);
cloned_method.addThrownType(method.getFactory().Type().createReference(Exception.class));
return cloned_method;
}
use of spoon.reflect.reference.CtPackageReference in project spoon by INRIA.
the class JDTTreeBuilderHelper method createModuleExport.
CtPackageExport createModuleExport(ExportsStatement exportsStatement) {
String packageName = new String(exportsStatement.pkgName);
int sourceStart = exportsStatement.sourceStart;
int sourceEnd = exportsStatement.sourceEnd;
CtPackageReference ctPackageReference = jdtTreeBuilder.references.getPackageReference(packageName);
CtPackageExport moduleExport = jdtTreeBuilder.getFactory().Module().createPackageExport(ctPackageReference);
if (exportsStatement.targets != null && exportsStatement.targets.length > 0) {
List<CtModuleReference> moduleReferences = new ArrayList<>();
for (ModuleReference moduleReference : exportsStatement.targets) {
moduleReferences.add(this.jdtTreeBuilder.references.getModuleReference(moduleReference));
}
moduleExport.setTargetExport(moduleReferences);
}
moduleExport.setPosition(this.jdtTreeBuilder.getPositionBuilder().buildPosition(sourceStart, sourceEnd));
return moduleExport;
}
use of spoon.reflect.reference.CtPackageReference in project spoon by INRIA.
the class ReferenceBuilder method getTypeReference.
/**
* JDT doesn't return a correct AST with the resolved type of the reference.
* This method try to build a correct Spoon AST from the name of the JDT
* reference, thanks to the parsing of the string, the name parameterized from
* the JDT reference and java convention.
* Returns a complete Spoon AST when the name is correct, otherwise a spoon type
* reference with a name that correspond to the name of the JDT type reference.
*/
<T> CtTypeReference<T> getTypeReference(TypeReference ref) {
CtTypeReference<T> res = null;
CtTypeReference inner = null;
final String[] namesParameterized = CharOperation.charArrayToStringArray(ref.getParameterizedTypeName());
String nameParameterized = CharOperation.toString(ref.getParameterizedTypeName());
String typeName = CharOperation.toString(ref.getTypeName());
int index = namesParameterized.length - 1;
for (; index >= 0; index--) {
// Start at the end to get the class name first.
CtTypeReference main = getTypeReference(namesParameterized[index]);
if (main == null) {
break;
}
if (res == null) {
res = (CtTypeReference<T>) main;
} else {
inner.setDeclaringType((CtTypeReference<?>) main);
}
inner = main;
}
if (res == null) {
return this.jdtTreeBuilder.getFactory().Type().createReference(nameParameterized);
}
if (inner.getPackage() == null) {
PackageFactory packageFactory = this.jdtTreeBuilder.getFactory().Package();
CtPackageReference packageReference = index >= 0 ? packageFactory.getOrCreate(concatSubArray(namesParameterized, index)).getReference() : packageFactory.topLevel();
inner.setPackage(packageReference);
}
if (!res.toString().replace(", ?", ",?").endsWith(nameParameterized)) {
// verify that we did not match a class that have the same name in a different package
return this.jdtTreeBuilder.getFactory().Type().createReference(typeName);
}
return res;
}
use of spoon.reflect.reference.CtPackageReference in project spoon by INRIA.
the class ReferenceBuilder method getPackageReference.
public CtPackageReference getPackageReference(String name) {
if (name.length() == 0) {
return this.jdtTreeBuilder.getFactory().Package().topLevel();
}
CtPackageReference ref = this.jdtTreeBuilder.getFactory().Core().createPackageReference();
ref.setSimpleName(name);
return ref;
}
use of spoon.reflect.reference.CtPackageReference in project spoon by INRIA.
the class DefaultCoreFactory method createPackageReference.
public CtPackageReference createPackageReference() {
CtPackageReference e = new CtPackageReferenceImpl();
e.setFactory(getMainFactory());
return e;
}
Aggregations