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

support of noClassPath in getFields #780

Merged
merged 4 commits into from
Aug 23, 2016
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 @@ -40,6 +40,7 @@
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
Expand Down Expand Up @@ -138,7 +139,8 @@ protected Class<T> findClass() {
try {
return (Class<T>) getFactory().getEnvironment().getClassLoader().loadClass(getQualifiedName());
} catch (java.lang.ClassNotFoundException cnfe) {
throw new SpoonClassNotFoundException("cannot load class: " + getQualifiedName() + " with class loader " + Thread.currentThread().getContextClassLoader(), cnfe);
throw new SpoonClassNotFoundException("cannot load class: " + getQualifiedName() + " with class loader "
+ Thread.currentThread().getContextClassLoader(), cnfe);
}
}

Expand Down Expand Up @@ -354,25 +356,48 @@ public CtTypeReference<?> unbox() {

@Override
public Collection<CtFieldReference<?>> getDeclaredFields() {
Collection<CtFieldReference<?>> l = new ArrayList<>();
CtType<?> t = getDeclaration();
if (t == null) {
for (Field f : getActualClass().getDeclaredFields()) {
l.add(getFactory().Field().createReference(f));
try {
return getDeclaredFieldReferences();
} catch (SpoonClassNotFoundException cnfe) {
return handleParentNotFound(cnfe);
}
if (getActualClass().isAnnotation()) {
for (Method m : getActualClass().getDeclaredMethods()) {
CtTypeReference<?> retRef = getFactory().Type().createReference(m.getReturnType());
CtFieldReference<?> fr = getFactory().Field().createReference(this, retRef, m.getName());
// fr.
l.add(fr);
}
} else {
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 : getActualClass().getDeclaredFields()) {
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 Collection<CtFieldReference<?>> handleParentNotFound(SpoonClassNotFoundException cnfe) {
String msg = "cannot load class: " + getQualifiedName() + " with class loader "
+ Thread.currentThread().getContextClassLoader();
if (getFactory().getEnvironment().getNoClasspath()) {
// should not be thrown in 'noClasspath' environment (#775)
Launcher.LOGGER.warn(msg);
return Collections.emptyList();
} else {
return t.getDeclaredFields();
throw cnfe;
}
return l;
}

@Override
Expand All @@ -389,7 +414,11 @@ public Collection<CtExecutableReference<?>> getDeclaredExecutables() {
public Collection<CtFieldReference<?>> getAllFields() {
CtType<?> t = getDeclaration();
if (t == null) {
return RtHelper.getAllFields(getActualClass(), getFactory());
try {
return RtHelper.getAllFields(getActualClass(), getFactory());
} catch (SpoonClassNotFoundException cnfe) {
return handleParentNotFound(cnfe);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove one level of indentation?

} else {
return t.getAllFields();
}
Expand Down
9 changes: 8 additions & 1 deletion src/test/java/spoon/test/api/NoClasspathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static org.junit.Assert.fail;

import java.io.File;
import java.util.Collection;
import java.util.List;

import org.junit.Test;
Expand All @@ -21,6 +22,7 @@
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.factory.Factory;
import spoon.reflect.reference.CtExecutableReference;
import spoon.reflect.reference.CtFieldReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.support.reflect.reference.SpoonClassNotFoundException;
Expand Down Expand Up @@ -53,11 +55,16 @@ public void test() throws Exception {
}
assertNull(superclass.getDeclaration());

// should be empty as in noClasspath the actual class cannot be retrieved
assertTrue(superclass.getAllFields().isEmpty());

// now we really make sure we don't have the class in the classpath
try {
superclass.getActualClass();
fail();
} catch (SpoonException e) {}
} catch (SpoonClassNotFoundException e) {
// expected
}

{
CtMethod<?> method = clazz.getMethod("method", new CtTypeReference[0]);
Expand Down