Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-20442] Ensuring linkage errors do not halt iteration over in…
…dexed elements.
  • Loading branch information
jglick committed Nov 6, 2013
1 parent 59f5496 commit c985b86
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/jvnet/hudson/annotation_indexer/Index.java
Expand Up @@ -5,8 +5,6 @@
import java.io.InputStreamReader;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.Enumeration;
import java.util.Iterator;
Expand Down Expand Up @@ -102,6 +100,8 @@ private void fetch() {
listAnnotatedElements(c.getDeclaredFields());
} catch (ClassNotFoundException e) {
LOGGER.log(Level.FINE, "Failed to load: "+name,e);
} catch (LinkageError x) {
LOGGER.log(Level.WARNING, "Failed to load " + name, x);
}
}
}
Expand Down
@@ -1,9 +1,15 @@
package org.jvnet.hudson.annotation_indexer;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Collections;
import java.util.Iterator;
import net.java.dev.hickory.testing.Compilation;
import org.junit.Test;
import static org.junit.Assert.*;
Expand Down Expand Up @@ -65,4 +71,30 @@ public class AnnotationProcessorImplTest {
*/
}

@Indexed @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface C {}
public interface Inaccessible {}
public static class Problematic {@C public Inaccessible bad() {return null;}}
public static class Fine {@C public String good() {return null;}}
public static class StillOK {@C public void whatever() {}}
@Test public void linkageErrorRobustness() throws Exception {
ClassLoader cl = new URLClassLoader(new URL[] {Index.class.getProtectionDomain().getCodeSource().getLocation(), AnnotationProcessorImplTest.class.getProtectionDomain().getCodeSource().getLocation()}, AnnotationProcessorImplTest.class.getClassLoader().getParent()) {
@Override protected Class<?> findClass(String name) throws ClassNotFoundException {
if (name.endsWith("$Inaccessible")) {
throw new ClassNotFoundException("this is intentionally denied");
}
return super.findClass(name);
}
};
// Have to call Index.list using reflection because we have to pass C.class reflectively (otherwise it is not present on the marked classes), and C.class has to have Indexed.class:
Method indexList = cl.loadClass(Index.class.getName()).getMethod("list", Class.class, ClassLoader.class, Class.class);
@SuppressWarnings("unchecked") Iterator<Method> it = ((Iterable<Method>) indexList.invoke(null, cl.loadClass(C.class.getName()), cl, Method.class)).iterator();
assertTrue(it.hasNext());
Method m = it.next();
assertEquals("good", m.getName());
assertTrue(it.hasNext());
m = it.next();
assertEquals("whatever", m.getName());
assertFalse(it.hasNext());
}

}

0 comments on commit c985b86

Please sign in to comment.