use of spoon.reflect.factory.TypeFactory in project spoon by INRIA.
the class FieldFactoryTest method testCreateFromSource.
@Test
public void testCreateFromSource() throws Exception {
CtClass<?> target = build("spoon.test.testclasses", "SampleClass");
Factory factory = build(Foo.class, Bar.class, SuperClass.class);
final CtClass<Object> type = factory.Class().get(Foo.class);
CtField<?> source = type.getField("i");
FieldFactory ff = type.getFactory().Field();
TypeFactory tf = type.getFactory().Type();
ff.create(target, source);
CtField<?> field = target.getField("i");
Assert.assertEquals("i", field.getSimpleName());
CtTypeReference<?> tref = tf.createReference("int");
Assert.assertEquals(tref, field.getType());
CtElement parent = field.getParent();
Assert.assertTrue(parent instanceof CtClass<?>);
Assert.assertEquals("SampleClass", ((CtClass<?>) parent).getSimpleName());
}
use of spoon.reflect.factory.TypeFactory in project spoon by INRIA.
the class ReferenceQueryTest method getAllTypeReferencesInEnum.
@Test
public void getAllTypeReferencesInEnum() throws Exception {
CtEnum<ReferenceQueryTestEnum> testEnum = build("spoon.test.reflect.visitor", "ReferenceQueryTestEnum");
List<CtTypeReference<?>> enumTypeRefs = Query.getElements(testEnum, new ReferenceTypeFilter<CtTypeReference<?>>(CtTypeReference.class));
TypeFactory typeFactory = testEnum.getFactory().Type();
for (Class<?> c : new Class<?>[] { Integer.class, Long.class, Boolean.class, Number.class, String.class, Void.class }) {
Assert.assertTrue("the reference query on the enum should return all the types defined in the enum declaration", enumTypeRefs.contains(typeFactory.createReference(c)));
}
}
use of spoon.reflect.factory.TypeFactory in project spoon by INRIA.
the class FieldFactoryTest method testCreate.
@Test
public void testCreate() throws Exception {
CtClass<?> type = build("spoon.test.testclasses", "SampleClass");
FieldFactory ff = type.getFactory().Field();
TypeFactory tf = type.getFactory().Type();
Set<ModifierKind> mods = new HashSet<ModifierKind>();
mods.add(ModifierKind.PRIVATE);
CtTypeReference<?> tref = tf.createReference(String.class);
ff.create(type, mods, tref, "name");
CtField<?> field = type.getField("name");
Assert.assertEquals("name", field.getSimpleName());
Assert.assertEquals(tref, field.getType());
CtElement parent = field.getParent();
Assert.assertTrue(parent instanceof CtClass<?>);
Assert.assertEquals("SampleClass", ((CtClass<?>) parent).getSimpleName());
}
use of spoon.reflect.factory.TypeFactory in project spoon by INRIA.
the class TypeFactoryTest method reflectionAPI.
@Test
public void reflectionAPI() throws Exception {
// Spoon can be used as reflection API
CtType s = new TypeFactory().get(String.class);
assertEquals("String", s.getSimpleName());
assertEquals("java.lang.String", s.getQualifiedName());
assertEquals(3, s.getSuperInterfaces().size());
assertEquals(2, s.getMethodsByName("toLowerCase").size());
}
use of spoon.reflect.factory.TypeFactory in project spoon by INRIA.
the class ContextBuilder method getVariableDeclaration.
@SuppressWarnings("unchecked")
private <T, U extends CtVariable<T>> U getVariableDeclaration(final String name, final Class<U> clazz) {
final CoreFactory coreFactory = jdtTreeBuilder.getFactory().Core();
final TypeFactory typeFactory = jdtTreeBuilder.getFactory().Type();
final ClassFactory classFactory = jdtTreeBuilder.getFactory().Class();
final InterfaceFactory interfaceFactory = jdtTreeBuilder.getFactory().Interface();
final FieldFactory fieldFactory = jdtTreeBuilder.getFactory().Field();
final ReferenceBuilder referenceBuilder = jdtTreeBuilder.getReferencesBuilder();
final Environment environment = jdtTreeBuilder.getFactory().getEnvironment();
// there is some extra work to do if we are looking for CtFields (and subclasses)
final boolean lookingForFields = clazz == null || coreFactory.createField().getClass().isAssignableFrom(clazz);
// try to find the variable on stack beginning with the most recent element
for (final ASTPair astPair : stack) {
// the variable may have been declared directly by one of these elements
final ScopeRespectingVariableScanner<U> scanner = new ScopeRespectingVariableScanner(name, clazz);
astPair.element.accept(scanner);
if (scanner.getResult() != null) {
return scanner.getResult();
}
// the variable may have been declared in a super class/interface
if (lookingForFields && astPair.node instanceof TypeDeclaration) {
final TypeDeclaration nodeDeclaration = (TypeDeclaration) astPair.node;
final Deque<ReferenceBinding> referenceBindings = new ArrayDeque<>();
// add super class if any
if (nodeDeclaration.superclass != null && nodeDeclaration.superclass.resolvedType instanceof ReferenceBinding) {
referenceBindings.push((ReferenceBinding) nodeDeclaration.superclass.resolvedType);
}
// add interfaces if any
if (nodeDeclaration.superInterfaces != null) {
for (final TypeReference tr : nodeDeclaration.superInterfaces) {
if (tr.resolvedType instanceof ReferenceBinding) {
referenceBindings.push((ReferenceBinding) tr.resolvedType);
}
}
}
while (!referenceBindings.isEmpty()) {
final ReferenceBinding referenceBinding = referenceBindings.pop();
for (final FieldBinding fieldBinding : referenceBinding.fields()) {
if (name.equals(new String(fieldBinding.readableName()))) {
final String qualifiedNameOfParent = new String(referenceBinding.readableName());
final CtType parentOfField = referenceBinding.isClass() ? classFactory.create(qualifiedNameOfParent) : interfaceFactory.create(qualifiedNameOfParent);
U field = (U) fieldFactory.create(parentOfField, EnumSet.noneOf(ModifierKind.class), referenceBuilder.getTypeReference(fieldBinding.type), name);
return field.setExtendedModifiers(JDTTreeBuilderQuery.getModifiers(fieldBinding.modifiers, true, false));
}
}
// add super class if any
final ReferenceBinding superclass = referenceBinding.superclass();
if (superclass != null) {
referenceBindings.push(superclass);
}
// add interfaces if any
final ReferenceBinding[] interfaces = referenceBinding.superInterfaces();
if (interfaces != null) {
for (ReferenceBinding rb : interfaces) {
referenceBindings.push(rb);
}
}
}
}
}
// the variable may have been imported statically from another class/interface
if (lookingForFields) {
final CtReference potentialReferenceToField = referenceBuilder.getDeclaringReferenceFromImports(name.toCharArray());
if (potentialReferenceToField != null && potentialReferenceToField instanceof CtTypeReference) {
final CtTypeReference typeReference = (CtTypeReference) potentialReferenceToField;
try {
final Class classOfType = typeReference.getActualClass();
if (classOfType != null) {
final CtType declaringTypeOfField = typeReference.isInterface() ? interfaceFactory.get(classOfType) : classFactory.get(classOfType);
final CtField field = declaringTypeOfField.getField(name);
if (field != null) {
return (U) field;
}
}
} catch (final SpoonClassNotFoundException scnfe) {
// field that has been imported statically from another class (or interface).
if (environment.getNoClasspath()) {
// assume a constant value according to JLS.
if (name.toUpperCase().equals(name)) {
final CtType parentOfField = classFactory.create(typeReference.getQualifiedName());
// it is the best thing we can do
final CtField field = coreFactory.createField();
field.setParent(parentOfField);
field.setSimpleName(name);
// it is the best thing we can do
field.setType(typeFactory.nullType());
return (U) field;
}
}
}
}
}
return null;
}
Aggregations