use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class VisibilityTest method testVisibilityOfClassesNamedByClassesInJavaLangPackage.
@Test
public void testVisibilityOfClassesNamedByClassesInJavaLangPackage() throws Exception {
final File sourceOutputDir = new File("target/spooned/spoon/test/visibility_package/testclasses");
final Launcher launcher = new Launcher();
launcher.getEnvironment().setAutoImports(true);
launcher.getEnvironment().setSourceOutputDirectory(sourceOutputDir);
final Factory factory = launcher.getFactory();
final SpoonModelBuilder compiler = launcher.createCompiler();
compiler.addInputSource(new File("./src/test/java/spoon/test/visibility/testclasses/"));
compiler.setSourceOutputDirectory(sourceOutputDir);
compiler.build();
compiler.generateProcessedSourceFiles(OutputType.CLASSES);
// Class must be imported.
final CtClass<?> aDouble = (CtClass<?>) factory.Type().get(spoon.test.visibility.testclasses.internal.Double.class);
assertNotNull(aDouble);
assertEquals(spoon.test.visibility.testclasses.internal.Double.class, aDouble.getActualClass());
// Class mustn't be imported.
final CtClass<?> aFloat = (CtClass<?>) factory.Type().get(spoon.test.visibility.testclasses.Float.class);
assertNotNull(aFloat);
assertEquals(spoon.test.visibility.testclasses.Float.class, aFloat.getActualClass());
canBeBuilt(new File("./target/spooned/spoon/test/visibility_package/testclasses/"), 7);
}
use of spoon.reflect.declaration.CtClass in project dspot by STAMP-project.
the class DSpotUtils method addGeneratedTestToExistingClass.
private static CtClass<?> addGeneratedTestToExistingClass(CtType<?> type, String pathname) {
Launcher launcher = new Launcher();
launcher.getEnvironment().setNoClasspath(true);
launcher.addInputResource(pathname);
launcher.buildModel();
final CtClass<?> existingAmplifiedTest = launcher.getFactory().Class().get(type.getQualifiedName());
type.getMethods().stream().filter(testCase -> !existingAmplifiedTest.getMethods().contains(testCase)).forEach(existingAmplifiedTest::addMethod);
return existingAmplifiedTest;
}
use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class TypeFactory method get.
/**
* Gets a created type from its qualified name.
*
* @return a found type or null if does not exist
*/
@SuppressWarnings("unchecked")
public <T> CtType<T> get(final String qualifiedName) {
int packageIndex = qualifiedName.lastIndexOf(CtPackage.PACKAGE_SEPARATOR);
CtPackage pack;
if (packageIndex > 0) {
pack = factory.Package().get(qualifiedName.substring(0, packageIndex));
} else {
pack = factory.Package().getRootPackage();
}
if (pack != null) {
CtType<T> type = pack.getType(qualifiedName.substring(packageIndex + 1));
if (type != null) {
return type;
}
}
int inertTypeIndex = qualifiedName.lastIndexOf(CtType.INNERTTYPE_SEPARATOR);
if (inertTypeIndex > 0) {
String s = qualifiedName.substring(0, inertTypeIndex);
CtType<T> t = factory.Type().get(s);
if (t == null) {
return null;
}
String className = qualifiedName.substring(inertTypeIndex + 1);
final CtTypeReference<T> reference = t.getReference();
if (reference.isLocalType()) {
final List<CtClass<T>> enclosingClasses = t.getElements(new TypeFilter<CtClass<T>>(CtClass.class) {
@Override
public boolean matches(CtClass<T> element) {
return super.matches(element) && element.getQualifiedName().equals(qualifiedName);
}
});
if (enclosingClasses.size() == 0) {
return null;
}
return enclosingClasses.get(0);
}
try {
// If the class name can't be parsed in integer, the method throws an exception.
// If the class name is an integer, the class is an anonymous class, otherwise,
// it is a standard class.
Integer.parseInt(className);
final List<CtNewClass> anonymousClasses = t.getElements(new TypeFilter<CtNewClass>(CtNewClass.class) {
@Override
public boolean matches(CtNewClass element) {
return super.matches(element) && element.getAnonymousClass().getQualifiedName().equals(qualifiedName);
}
});
if (anonymousClasses.size() == 0) {
return null;
}
return anonymousClasses.get(0).getAnonymousClass();
} catch (NumberFormatException e) {
return t.getNestedType(className);
}
}
return null;
}
use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class TypeFactory method createTypeAdapter.
/**
* Create a {@link GenericTypeAdapter} for adapting of formal type parameters from any compatible context to the context of provided `formalTypeDeclarer`
*
* @param formalTypeDeclarer
* the target scope of the returned {@link GenericTypeAdapter}
*/
public GenericTypeAdapter createTypeAdapter(CtFormalTypeDeclarer formalTypeDeclarer) {
class Visitor extends CtAbstractVisitor {
GenericTypeAdapter adapter;
@Override
public <T> void visitCtClass(CtClass<T> ctClass) {
adapter = new ClassTypingContext(ctClass);
}
@Override
public <T> void visitCtInterface(CtInterface<T> intrface) {
adapter = new ClassTypingContext(intrface);
}
@Override
public <T> void visitCtMethod(CtMethod<T> m) {
adapter = new MethodTypingContext().setMethod(m);
}
@Override
public <T> void visitCtConstructor(CtConstructor<T> c) {
adapter = new MethodTypingContext().setConstructor(c);
}
}
Visitor visitor = new Visitor();
((CtElement) formalTypeDeclarer).accept(visitor);
return visitor.adapter;
}
use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class GenericsTest method testGetDeclarationOfTypeParameterReference.
@Test
public void testGetDeclarationOfTypeParameterReference() {
Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/generics/testclasses/ExtendedPaella.java");
launcher.addInputResource("./src/test/java/spoon/test/generics/testclasses/Paella.java");
launcher.buildModel();
Factory factory = launcher.getFactory();
CtClass extendedPaella = factory.getModel().getElements(new NamedElementFilter<>(CtClass.class, "ExtendedPaella")).get(0);
List<CtTypeParameter> typeParameterList = extendedPaella.getFormalCtTypeParameters();
assertEquals(1, typeParameterList.size());
CtMethod totoMethod = factory.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "toto")).get(0);
CtTypeReference returnTypeToto = totoMethod.getType();
CtTypeReference paramToto = ((CtParameter) totoMethod.getParameters().get(0)).getType();
CtType declaration = returnTypeToto.getDeclaration();
assertSame(typeParameterList.get(0), declaration);
assertSame(typeParameterList.get(0), paramToto.getDeclaration());
CtMethod machinMethod = factory.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "machin")).get(0);
CtTypeReference returnTypeMachin = machinMethod.getType();
List<CtTypeParameter> formalCtTypeParameters = machinMethod.getFormalCtTypeParameters();
assertEquals(1, formalCtTypeParameters.size());
CtType declarationMachin = returnTypeMachin.getDeclaration();
assertNotSame(typeParameterList.get(0), declarationMachin);
assertSame(formalCtTypeParameters.get(0), declarationMachin);
CtClass innerPaella = factory.getModel().getElements(new NamedElementFilter<>(CtClass.class, "InnerPaella")).get(0);
List<CtTypeParameter> innerTypeParametersList = innerPaella.getFormalCtTypeParameters();
assertEquals(typeParameterList.get(0), innerTypeParametersList.get(0).getSuperclass().getDeclaration());
CtMethod innerMachinMethod = factory.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "innerMachin")).get(0);
CtTypeReference returnTypeInnerMachin = innerMachinMethod.getType();
CtTypeReference paramInnerMachinType = ((CtParameter) innerMachinMethod.getParameters().get(0)).getType();
List<CtTypeParameter> innerMachinFormalCtType = innerMachinMethod.getFormalCtTypeParameters();
assertSame(typeParameterList.get(0), returnTypeInnerMachin.getDeclaration());
assertSame(innerMachinFormalCtType.get(0), paramInnerMachinType.getDeclaration());
CtMethod innerTotoMethod = factory.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "innerToto")).get(0);
CtTypeReference returnInnerToto = innerTotoMethod.getType();
CtTypeReference paramInnerToto = ((CtParameter) innerTotoMethod.getParameters().get(0)).getType();
List<CtTypeParameter> innerTotoFormatCtType = innerTotoMethod.getFormalCtTypeParameters();
assertSame(innerTotoFormatCtType.get(0), paramInnerToto.getDeclaration());
assertSame(innerTypeParametersList.get(0), returnInnerToto.getDeclaration());
}
Aggregations