Skip to content

Commit

Permalink
FileNotFoundException when executed under Tomcat from Eclipse
Browse files Browse the repository at this point in the history
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 <sentinel@atteo.com>
  • Loading branch information
sentinelt committed Mar 26, 2013
1 parent 72d8d59 commit 80ef2cc
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 5 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions classindex/src/main/java/org/atteo/evo/classindex/ClassIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -151,8 +152,18 @@ private static Iterable<Class<?>> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void inheritedAnnotation() {
public void packageSubclasses() {
Iterable<Class<?>> packageSubclasses = ClassIndex.getPackageClasses(
ClassIndexTest.class.getPackage().getName());
assertEquals(8, Iterables.size(packageSubclasses));
assertEquals(9, Iterables.size(packageSubclasses));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -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<URL> getResources(final String name) throws IOException {
return new Enumeration<URL>() {
private boolean dummyReturned = false;
private Enumeration<URL> 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<Class<?>> annotated = ClassIndex.getAnnotated(Component.class);
assertEquals(2, Iterables.size(annotated));
} finally {
Thread.currentThread().setContextClassLoader(original);
}
}
}

0 comments on commit 80ef2cc

Please sign in to comment.