@@ -65,7 +65,7 @@ public void findClasses(PrettyAnnotationHandler handler)
6565 }
6666
6767 // call recursive directory processing method
68- processDirectory (classesFolderUrl , handler );
68+ processDirectory (classesFolderUrl , classesFolderUrl , handler );
6969
7070 }
7171 catch (MalformedURLException e )
@@ -88,20 +88,23 @@ public void findClasses(PrettyAnnotationHandler handler)
8888 * @throws MalformedURLException
8989 * for invalid URLs
9090 */
91- @ SuppressWarnings ("unchecked" )
92- protected void processDirectory (URL directoryUrl , PrettyAnnotationHandler handler ) throws MalformedURLException
91+ protected void processDirectory (URL classesFolderUrl , URL directoryUrl , PrettyAnnotationHandler handler ) throws MalformedURLException
9392 {
93+
94+ // only the path of the classes folder URL is required in this method
95+ String classesFolderPath = classesFolderUrl .getPath ();
96+
9497 // log directory name on trace level
9598 if (log .isTraceEnabled ())
9699 {
97100 log .trace ("Processing directory: " + directoryUrl .toString ());
98101 }
99102
100- // build directory name relative to webapp root
101- String relativeName = getWebappRelativeName (directoryUrl , CLASSES_FOLDER );
102-
103+ // get the directory name relative to the '/WEB-INF/classes/' folder
104+ String relativeDirectoryName = getPathRelativeToClassesFolder (directoryUrl . getPath (), classesFolderPath );
105+
103106 // call getResourcePaths to get directory entries
104- Set paths = servletContext .getResourcePaths (relativeName );
107+ Set <?> paths = servletContext .getResourcePaths (CLASSES_FOLDER + relativeDirectoryName );
105108
106109 // loop over all entries of the directory
107110 for (Object relativePath : paths )
@@ -114,8 +117,11 @@ protected void processDirectory(URL directoryUrl, PrettyAnnotationHandler handle
114117 if (entryUrl .getPath ().endsWith (".class" ))
115118 {
116119
117- // get class name
118- String className = getClassName (entryUrl .toString (), CLASSES_FOLDER );
120+ // the name of the entry relative to the '/WEB-INF/classes/' folder
121+ String entryRelativeName = getPathRelativeToClassesFolder (entryUrl .getPath (), classesFolderPath );
122+
123+ // build class name from relative name
124+ String className = getClassName (entryRelativeName );
119125
120126 // check filter
121127 if (mustProcessClass (className ))
@@ -174,10 +180,48 @@ protected void processDirectory(URL directoryUrl, PrettyAnnotationHandler handle
174180 {
175181
176182 // walk down the directory
177- processDirectory (entryUrl , handler );
183+ processDirectory (classesFolderUrl , entryUrl , handler );
178184
179185 }
180186 }
181187 }
182188
189+ /**
190+ * This method will create a path relative to the '/WEB-INF/classes/' folder
191+ * for the given path. It will first try to find the '/WEB-INF/classes/'
192+ * suffix in the path to do so. If this suffix cannot be found (can happen
193+ * when using the jetty-maven-plugin with 'jetty:run' goal), the method will
194+ * try to build the relative name by stripping the path of the
195+ * '/WEB-INF/classes/' folder which must be supplied to the method. The
196+ * method will throw an {@link IllegalArgumentException} if the relative path
197+ * could not be build.
198+ *
199+ * @param path
200+ * The path to build the relative path for
201+ * @param classesFolderPath
202+ * the known path of the '/WEB-INF/classes/' folder.
203+ * @return the relative name of the path
204+ */
205+ private String getPathRelativeToClassesFolder (String path , String classesFolderPath )
206+ {
207+
208+ // first try to find the '/WEB-INF/classes' suffix
209+ String result = stripKnownPrefix (path , CLASSES_FOLDER );
210+
211+ // alternative: try to strip the full path of the '/WEB-INF/classes/' folder
212+ if (result == null )
213+ {
214+ result = stripKnownPrefix (path , classesFolderPath );
215+ }
216+
217+ // none of the two methods worked?
218+ if (result == null )
219+ {
220+ throw new IllegalArgumentException ("Unable to build path relative to '/WEB-INF/classes/' from: " +path );
221+ }
222+
223+ return result ;
224+
225+ }
226+
183227}
0 commit comments