diff --git a/src/main/java/spoon/support/reflect/reference/CtTypeReferenceImpl.java b/src/main/java/spoon/support/reflect/reference/CtTypeReferenceImpl.java index c0f308f39ae..a45666536f3 100644 --- a/src/main/java/spoon/support/reflect/reference/CtTypeReferenceImpl.java +++ b/src/main/java/spoon/support/reflect/reference/CtTypeReferenceImpl.java @@ -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; @@ -339,45 +337,11 @@ public CtTypeReference unbox() { @Override public Collection> 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> getDeclaredFieldReferences() { - Collection> 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) { @@ -397,50 +361,19 @@ public CtFieldReference getDeclaredField(String name) { if (name == null) { return null; } - CtType t = getDeclaration(); - if (t == null) { - try { - Collection> 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> 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; } diff --git a/src/test/java/spoon/test/field/FieldTest.java b/src/test/java/spoon/test/field/FieldTest.java index cf20d74054d..25796294b11 100644 --- a/src/test/java/spoon/test/field/FieldTest.java +++ b/src/test/java/spoon/test/field/FieldTest.java @@ -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; @@ -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 aClass = (CtClass) 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.