From 80ef2cca5e596facafddd7de37adf7d461161312 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C5=82awek=20Piotrowski?= Date: Tue, 26 Mar 2013 21:52:28 +0100 Subject: [PATCH] FileNotFoundException when executed under Tomcat from Eclipse MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When Executed under Tomcat started from Eclipse with "Serve modules without publishing" opiton turned on, ClassLoader.getResources() method returnes the same resource two times: first with incorrect path and second time with the correct one. This change ignores any resources which throws FileNotFoundException when opened. Fixes: #5 Change-Id: I89c281db81a38b65b66d030ebec04ecbbcbbba9f Tested-by: Mr. Jenkins Reviewed-by: SÅ‚awek Piotrowski --- README.md | 8 +- .../org/atteo/evo/classindex/ClassIndex.java | 15 +++- .../atteo/evo/classindex/ClassIndexTest.java | 2 +- .../evo/classindex/TomcatFromEclipseTest.java | 85 +++++++++++++++++++ 4 files changed, 105 insertions(+), 5 deletions(-) create mode 100644 classindex/src/test/java/org/atteo/evo/classindex/TomcatFromEclipseTest.java diff --git a/README.md b/README.md index 8902ca2..0072ee4 100644 --- a/README.md +++ b/README.md @@ -7,10 +7,14 @@ Evo Class Index is an annotation processor which at compile-time generates an in Changes ======= +Version 1.4 + +- Fix FileNotFoundException when executed under Tomcat from Eclipse + Version 1.3 -- Ignore classes which don't exist at runtime (#4) - This fixes some issues Eclipse +- Ignore classes which don't exist at runtime (#4). + This fixes some issues in Eclipse. - Allow to create custom processors which index subclasses and packages Version 1.2 diff --git a/classindex/src/main/java/org/atteo/evo/classindex/ClassIndex.java b/classindex/src/main/java/org/atteo/evo/classindex/ClassIndex.java index b33f938..b7c333c 100644 --- a/classindex/src/main/java/org/atteo/evo/classindex/ClassIndex.java +++ b/classindex/src/main/java/org/atteo/evo/classindex/ClassIndex.java @@ -14,6 +14,7 @@ package org.atteo.evo.classindex; import java.io.BufferedReader; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.lang.annotation.Annotation; @@ -151,8 +152,18 @@ private static Iterable> readIndexFile(String resourceFile) { while (resources.hasMoreElements()) { URL resource = resources.nextElement(); - BufferedReader reader = new BufferedReader(new InputStreamReader(resource.openStream(), - Charsets.UTF_8)); + BufferedReader reader; + try { + reader = new BufferedReader(new InputStreamReader(resource.openStream(), + Charsets.UTF_8)); + } catch (FileNotFoundException e) { + // When executed under Tomcat started from Eclipse with "Serve modules without + // publishing" option turned on, getResources() method above returns the same + // resource two times: first with incorrect path and second time with correct one. + // So ignore the one which does not exist. + // See: https://github.com/atteo/evo-classindex/issues/5 + continue; + } String line = reader.readLine(); while (line != null) { diff --git a/classindex/src/test/java/org/atteo/evo/classindex/ClassIndexTest.java b/classindex/src/test/java/org/atteo/evo/classindex/ClassIndexTest.java index f3aeeda..3d5a423 100644 --- a/classindex/src/test/java/org/atteo/evo/classindex/ClassIndexTest.java +++ b/classindex/src/test/java/org/atteo/evo/classindex/ClassIndexTest.java @@ -47,7 +47,7 @@ public void inheritedAnnotation() { public void packageSubclasses() { Iterable> packageSubclasses = ClassIndex.getPackageClasses( ClassIndexTest.class.getPackage().getName()); - assertEquals(8, Iterables.size(packageSubclasses)); + assertEquals(9, Iterables.size(packageSubclasses)); } @Test diff --git a/classindex/src/test/java/org/atteo/evo/classindex/TomcatFromEclipseTest.java b/classindex/src/test/java/org/atteo/evo/classindex/TomcatFromEclipseTest.java new file mode 100644 index 0000000..1d32b7c --- /dev/null +++ b/classindex/src/test/java/org/atteo/evo/classindex/TomcatFromEclipseTest.java @@ -0,0 +1,85 @@ +/* + * Copyright 2013 Atteo. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.atteo.evo.classindex; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Enumeration; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +import com.google.common.collect.Iterables; + +public class TomcatFromEclipseTest { + private static class WeirdClassLoader extends ClassLoader { + private ClassLoader wrappedClassLoader; + + public WeirdClassLoader(ClassLoader wrappedClassLoader) { + this.wrappedClassLoader = wrappedClassLoader; + } + + @Override + public Enumeration getResources(final String name) throws IOException { + return new Enumeration() { + private boolean dummyReturned = false; + private Enumeration original = wrappedClassLoader.getResources(name); + + @Override + public boolean hasMoreElements() { + if (!dummyReturned) { + return true; + } + return original.hasMoreElements(); + } + + @Override + public URL nextElement() { + if (!dummyReturned) { + dummyReturned = true; + try { + return new URL("file:///some/non/existing/file/path/qpwoeur103841s"); + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } + } + return original.nextElement(); + } + }; + } + } + + /* + * When executed under Tomcat started from Eclipse with "Serve modules without + * publishing" option turned on, ClassLoader::getResources() method returns the same + * resource two times: first with incorrect path and second time with correct one. + * So ignore the one which does not exist. + */ + @Test + public void testIncorrectEntriesReturned() { + ClassLoader original = Thread.currentThread().getContextClassLoader(); + ClassLoader weird = new WeirdClassLoader(original); + Thread.currentThread().setContextClassLoader(weird); + + try { + Iterable> annotated = ClassIndex.getAnnotated(Component.class); + assertEquals(2, Iterables.size(annotated)); + } finally { + Thread.currentThread().setContextClassLoader(original); + } + } +}