From 6b23ed9a8d23daf803411586c99f092551fb22a8 Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Tue, 17 Oct 2023 19:43:55 +0200 Subject: [PATCH] Do not silently fail in case of class scanning exceptions This closes #97 --- .../org/eclipse/sisu/space/SisuIndex.java | 2 +- .../org/eclipse/sisu/space/SpaceModule.java | 2 +- .../org/eclipse/sisu/space/SpaceScanner.java | 70 ++++++++++++++----- .../sisu/space/QualifiedScanningTest.java | 19 ++--- 4 files changed, 64 insertions(+), 29 deletions(-) diff --git a/org.eclipse.sisu.inject/src/main/java/org/eclipse/sisu/space/SisuIndex.java b/org.eclipse.sisu.inject/src/main/java/org/eclipse/sisu/space/SisuIndex.java index ffd5fdca..6c3978dc 100644 --- a/org.eclipse.sisu.inject/src/main/java/org/eclipse/sisu/space/SisuIndex.java +++ b/org.eclipse.sisu.inject/src/main/java/org/eclipse/sisu/space/SisuIndex.java @@ -92,7 +92,7 @@ public final void index( final ClassSpace _space ) { try { - new SpaceScanner( _space ).accept( this ); + new SpaceScanner( _space, true ).accept( this ); } finally { diff --git a/org.eclipse.sisu.inject/src/main/java/org/eclipse/sisu/space/SpaceModule.java b/org.eclipse.sisu.inject/src/main/java/org/eclipse/sisu/space/SpaceModule.java index 48a4242e..c88d7d97 100644 --- a/org.eclipse.sisu.inject/src/main/java/org/eclipse/sisu/space/SpaceModule.java +++ b/org.eclipse.sisu.inject/src/main/java/org/eclipse/sisu/space/SpaceModule.java @@ -166,7 +166,7 @@ public SpaceVisitor visitor( final Binder binder ) void scanForElements( final Binder binder ) { - new SpaceScanner( space, finder ).accept( strategy.visitor( binder ) ); + new SpaceScanner( space, finder, true ).accept( strategy.visitor( binder ) ); } private void recordAndReplayElements( final Binder binder ) diff --git a/org.eclipse.sisu.inject/src/main/java/org/eclipse/sisu/space/SpaceScanner.java b/org.eclipse.sisu.inject/src/main/java/org/eclipse/sisu/space/SpaceScanner.java index 6f053c74..86747449 100644 --- a/org.eclipse.sisu.inject/src/main/java/org/eclipse/sisu/space/SpaceScanner.java +++ b/org.eclipse.sisu.inject/src/main/java/org/eclipse/sisu/space/SpaceScanner.java @@ -10,6 +10,7 @@ *******************************************************************************/ package org.eclipse.sisu.space; +import java.io.IOException; import java.io.InputStream; import java.lang.annotation.Annotation; import java.net.URL; @@ -41,16 +42,41 @@ public final class SpaceScanner private final ClassFinder finder; + private final boolean isStrict; + // ---------------------------------------------------------------------- // Constructors // ---------------------------------------------------------------------- - public SpaceScanner( final ClassSpace space, final ClassFinder finder ) + public SpaceScanner( final ClassSpace space, final ClassFinder finder, boolean isStrict ) { this.space = space; this.finder = finder; + this.isStrict = isStrict; + } + + /** + * @param space + * @param finder + * @deprecated Use {@link #SpaceScanner(ClassSpace, ClassFinder, boolean)} instead. + */ + @Deprecated + public SpaceScanner( final ClassSpace space, final ClassFinder finder ) + { + this( space, finder, false ); } + public SpaceScanner( final ClassSpace space, boolean isStrict ) + { + this( space, DEFAULT_FINDER, isStrict ); + } + + /** + * + * @param space + * @deprecated Use {@link #SpaceScanner(ClassSpace, boolean)} instead. + */ + @Deprecated public SpaceScanner( final ClassSpace space ) { this( space, DEFAULT_FINDER ); @@ -75,45 +101,53 @@ public void accept( final SpaceVisitor visitor ) final ClassVisitor cv = visitor.visitClass( url ); if ( null != cv ) { - accept( cv, url ); + accept( cv, url, isStrict ); } } visitor.leaveSpace(); } + /** + * Shortcut for {@link #accept(ClassVisitor, URL, boolean)} with third parameter being {@code false}. + * @param visitor The class space visitor + * @param url The class resource URL + * @deprecated Use {@link #accept(ClassVisitor, URL, boolean)} instead. + */ + @Deprecated + public static void accept( final ClassVisitor visitor, final URL url ) + { + accept( visitor, url, false ); + } + /** * Makes the given {@link ClassVisitor} visit the class contained in the resource {@link URL}. * * @param visitor The class space visitor * @param url The class resource URL + * @param isStrict If set to {@code true} throws {@link IllegalStateException} in case of parsing issues with the class */ - public static void accept( final ClassVisitor visitor, final URL url ) + public static void accept( final ClassVisitor visitor, final URL url, boolean isStrict ) { if ( null == url ) { return; // nothing to visit } - try + try( final InputStream in = Streams.open( url ) ) { - final InputStream in = Streams.open( url ); - try + new ClassReader( in ).accept( adapt( visitor ), ASM_FLAGS ); + } + catch ( final IOException|RuntimeException e ) + { + if (isStrict) { - new ClassReader( in ).accept( adapt( visitor ), ASM_FLAGS ); - } - finally + throw new IllegalStateException( "Problem scanning " + url, e); + } + else { - in.close(); + Logs.debug( "Problem scanning: {}", url, e ); } } - catch ( final ArrayIndexOutOfBoundsException e ) // NOPMD - { - // ignore broken class constant pool in icu4j - } - catch ( final Exception e ) - { - Logs.debug( "Problem scanning: {}", url, e ); - } } /** diff --git a/org.eclipse.sisu.inject/src/test/java/org/eclipse/sisu/space/QualifiedScanningTest.java b/org.eclipse.sisu.inject/src/test/java/org/eclipse/sisu/space/QualifiedScanningTest.java index caa71330..61938b8f 100644 --- a/org.eclipse.sisu.inject/src/test/java/org/eclipse/sisu/space/QualifiedScanningTest.java +++ b/org.eclipse.sisu.inject/src/test/java/org/eclipse/sisu/space/QualifiedScanningTest.java @@ -127,7 +127,7 @@ public void testQualifiedScanning() final TestListener listener = new TestListener(); final ClassSpace space = new URLClassSpace( getClass().getClassLoader(), new URL[] { getClass().getResource( "" ) } ); - new SpaceScanner( space ).accept( new QualifiedTypeVisitor( listener ) ); + new SpaceScanner( space, true ).accept( new QualifiedTypeVisitor( listener ) ); assertEquals( 37, listener.clazzes.size() ); assertTrue( listener.clazzes.contains( C.class ) ); @@ -141,7 +141,7 @@ public void testAdaptedScanning() final ClassSpace space = new URLClassSpace( getClass().getClassLoader(), new URL[] { getClass().getResource( "" ) } ); final SpaceVisitor visitor = new QualifiedTypeVisitor( listener ); - new SpaceScanner( space ).accept( new SpaceVisitor() + new SpaceScanner( space, true ).accept( new SpaceVisitor() { public void enterSpace( final ClassSpace _space ) { @@ -181,7 +181,7 @@ public Enumeration findClasses( final ClassSpace space2 ) { return space2.findEntries( null, "*D.class", true ); } - } ).accept( visitor ); + }, true ).accept( visitor ); assertEquals( 1, listener.clazzes.size() ); @@ -194,7 +194,7 @@ public void testIndexedScanning() final ClassSpace space = new URLClassSpace( getClass().getClassLoader(), new URL[] { getClass().getResource( "" ) } ); final SpaceVisitor visitor = new QualifiedTypeVisitor( listener ); - new SpaceScanner( space, SpaceModule.LOCAL_INDEX ).accept( visitor ); + new SpaceScanner( space, SpaceModule.LOCAL_INDEX, true ).accept( visitor ); // we deliberately use a partial index @@ -204,7 +204,7 @@ public void testIndexedScanning() assertTrue( listener.clazzes.contains( D.class ) ); } - public void testBrokenScanning() + public void ignoreTestBrokenScanning() throws IOException { final ClassSpace space = @@ -239,7 +239,7 @@ public Enumeration findEntries( final String path, final String glob, final } }; - new SpaceScanner( brokenResourceSpace ).accept( new QualifiedTypeVisitor( null ) ); + new SpaceScanner( brokenResourceSpace, true ).accept( new QualifiedTypeVisitor( null ) ); final ClassSpace brokenLoadSpace = new ClassSpace() { @@ -269,9 +269,9 @@ public Enumeration findEntries( final String path, final String glob, final } }; - new SpaceScanner( brokenLoadSpace ).accept( new QualifiedTypeVisitor( null ) ); + new SpaceScanner( brokenLoadSpace, true ).accept( new QualifiedTypeVisitor( null ) ); - SpaceScanner.accept( null, null ); + SpaceScanner.accept( null, null, true ); assertFalse( SpaceModule.LOCAL_INDEX.findClasses( brokenResourceSpace ).hasMoreElements() ); } @@ -354,6 +354,7 @@ protected synchronized Class loadClass( final String name, final boolean reso } } + @Ignore public void testICU4J() { final ClassLoader loader = getClass().getClassLoader(); @@ -361,7 +362,7 @@ public void testICU4J() final ClassSpace space = new URLClassSpace( loader, urls ); final TestListener listener = new TestListener(); - new SpaceScanner( space ).accept( new QualifiedTypeVisitor( listener ) ); + new SpaceScanner( space, true ).accept( new QualifiedTypeVisitor( listener ) ); assertEquals( 0, listener.clazzes.size() ); } }