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

refactor: get*Field now use shadow types #2008

Merged
merged 3 commits into from
May 27, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@
import spoon.support.visitor.ClassTypingContext;

import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -339,45 +337,11 @@ public CtTypeReference<?> unbox() {

@Override
public Collection<CtFieldReference<?>> getDeclaredFields() {
CtType<?> t = getDeclaration();
if (t == null) {
try {
return getDeclaredFieldReferences();
} catch (SpoonClassNotFoundException cnfe) {
handleParentNotFound(cnfe);
return Collections.emptyList();
}
} else {
CtType<?> t = getTypeDeclaration();
if (t != null) {
return t.getDeclaredFields();
}
}

/**
* Collects all field references of the declared class.
*
* @return collection of field references
*/
private Collection<CtFieldReference<?>> getDeclaredFieldReferences() {
Collection<CtFieldReference<?>> references = new ArrayList<>();
for (Field f : getDeclaredFields(getActualClass())) {
references.add(getFactory().Field().createReference(f));
}
if (getActualClass().isAnnotation()) {
for (Method m : getActualClass().getDeclaredMethods()) {
CtTypeReference<?> retRef = getFactory().Type().createReference(m.getReturnType());
CtFieldReference<?> fr = getFactory().Field().createReference(this, retRef, m.getName());
references.add(fr);
}
}
return references;
}

private Field[] getDeclaredFields(Class<?> cls) {
try {
return cls.getDeclaredFields();
} catch (Throwable e) {
throw new SpoonClassNotFoundException("cannot load fields of class: " + getQualifiedName(), e);
}
return Collections.emptyList();
}

private void handleParentNotFound(SpoonClassNotFoundException cnfe) {
Expand All @@ -397,50 +361,19 @@ public CtFieldReference<?> getDeclaredField(String name) {
if (name == null) {
return null;
}
CtType<?> t = getDeclaration();
if (t == null) {
try {
Collection<CtFieldReference<?>> fields = getDeclaredFieldReferences();
for (CtFieldReference<?> field : fields) {
if (name.equals(field.getSimpleName())) {
return field;
}
}
} catch (SpoonClassNotFoundException cnfe) {
handleParentNotFound(cnfe);
return null;
}
return null;
} else {
CtType<?> t = getTypeDeclaration();
if (t != null) {
return t.getDeclaredField(name);
}
return null;
}

public CtFieldReference<?> getDeclaredOrInheritedField(String fieldName) {
CtType<?> t = getDeclaration();
if (t == null) {
CtFieldReference<?> field = getDeclaredField(fieldName);
if (field != null) {
return field;
}
CtTypeReference<?> typeRef = getSuperclass();
if (typeRef != null) {
field = typeRef.getDeclaredOrInheritedField(fieldName);
if (field != null) {
return field;
}
}
Set<CtTypeReference<?>> ifaces = getSuperInterfaces();
for (CtTypeReference<?> iface : ifaces) {
field = iface.getDeclaredOrInheritedField(fieldName);
if (field != null) {
return field;
}
}
return field;
} else {
CtType<?> t = getTypeDeclaration();
if (t != null) {
return t.getDeclaredOrInheritedField(fieldName);
}
return null;
}


Expand Down
17 changes: 17 additions & 0 deletions src/test/java/spoon/test/field/FieldTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.CtMethod;
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.TypeFilter;
import spoon.support.reflect.eval.VisitorPartialEvaluator;
import spoon.test.field.testclasses.A;
Expand Down Expand Up @@ -71,6 +73,21 @@ public void testAddAFieldInAClassAtAPositionGiven() throws Exception {
assertEquals(second, fieldClass.getFields().get(2));
}

@Test
public void testgetDeclaredFields() throws Exception {
// contract: get*Fields works for both references
final CtClass<AddFieldAtTop> aClass = (CtClass<AddFieldAtTop>) buildClass(AddFieldAtTop.class);

assertEquals(1, aClass.getReference().getDeclaredFields().size());
CtTypeReference<?> fileClass = aClass.getFactory().Type().get(File.class).getReference();
assertEquals(13, fileClass.getDeclaredFields().size());
assertEquals("pathSeparator", fileClass.getDeclaredField("pathSeparator").getSimpleName());
assertEquals("pathSeparator", fileClass.getDeclaredOrInheritedField("pathSeparator").getSimpleName());

// double check that we can still go to the declaration
assertEquals("pathSeparator", fileClass.getDeclaredField("pathSeparator").getFieldDeclaration().getSimpleName());
}

@Test
public void testAddFieldsAtTop() throws Exception {
// contract: When we use CtType#addFieldAtTop, field added should be printed at the top of the type.
Expand Down