|
|
|
@ -29,7 +29,9 @@ import java.util.Collections;
|
|
|
|
|
import java.util.Comparator;
|
|
|
|
|
import java.util.Deque;
|
|
|
|
|
import java.util.Enumeration;
|
|
|
|
|
import java.util.LinkedHashSet;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
import java.util.jar.JarEntry;
|
|
|
|
|
import java.util.jar.JarFile;
|
|
|
|
|
|
|
|
|
@ -85,6 +87,18 @@ public abstract class MainClassFinder {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Find a single main class from a given folder.
|
|
|
|
|
* @param rootFolder the root folder to search
|
|
|
|
|
* @return the main class or {@code null}
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
public static String findSingleMainClass(File rootFolder) throws IOException {
|
|
|
|
|
MainClassesCallback callback = new MainClassesCallback();
|
|
|
|
|
MainClassFinder.doWithMainClasses(rootFolder, callback);
|
|
|
|
|
return callback.getMainClass();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Perform the given callback operation on all main classes from the given root
|
|
|
|
|
* folder.
|
|
|
|
@ -93,7 +107,7 @@ public abstract class MainClassFinder {
|
|
|
|
|
* @return the first callback result or {@code null}
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
public static <T> T doWithMainClasses(File rootFolder, ClassNameCallback<T> callback)
|
|
|
|
|
static <T> T doWithMainClasses(File rootFolder, ClassNameCallback<T> callback)
|
|
|
|
|
throws IOException {
|
|
|
|
|
if (!rootFolder.exists()) {
|
|
|
|
|
return null; // nothing to do
|
|
|
|
@ -160,6 +174,20 @@ public abstract class MainClassFinder {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Find a single main class in a given jar file.
|
|
|
|
|
* @param jarFile the jar file to search
|
|
|
|
|
* @param classesLocation the location within the jar containing classes
|
|
|
|
|
* @return the main class or {@code null}
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
public static String findSingleMainClass(JarFile jarFile, String classesLocation)
|
|
|
|
|
throws IOException {
|
|
|
|
|
MainClassesCallback callback = new MainClassesCallback();
|
|
|
|
|
MainClassFinder.doWithMainClasses(jarFile, classesLocation, callback);
|
|
|
|
|
return callback.getMainClass();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Perform the given callback operation on all main classes from the given jar.
|
|
|
|
|
* @param jarFile the jar file to search
|
|
|
|
@ -167,7 +195,7 @@ public abstract class MainClassFinder {
|
|
|
|
|
* @return the first callback result or {@code null}
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
public static <T> T doWithMainClasses(JarFile jarFile, String classesLocation,
|
|
|
|
|
static <T> T doWithMainClasses(JarFile jarFile, String classesLocation,
|
|
|
|
|
ClassNameCallback<T> callback) throws IOException {
|
|
|
|
|
List<JarEntry> classEntries = getClassEntries(jarFile, classesLocation);
|
|
|
|
|
Collections.sort(classEntries, new ClassEntryComparator());
|
|
|
|
@ -293,4 +321,30 @@ public abstract class MainClassFinder {
|
|
|
|
|
T doWith(String className);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Find a single main class, throwing an {@link IllegalStateException} if multiple
|
|
|
|
|
* candidates exist.
|
|
|
|
|
*/
|
|
|
|
|
private static class MainClassesCallback implements ClassNameCallback<Object> {
|
|
|
|
|
|
|
|
|
|
private final Set<String> classNames = new LinkedHashSet<String>();
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Object doWith(String className) {
|
|
|
|
|
this.classNames.add(className);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getMainClass() {
|
|
|
|
|
if (this.classNames.size() > 1) {
|
|
|
|
|
throw new IllegalStateException(
|
|
|
|
|
"Unable to find a single main class from the following candidates "
|
|
|
|
|
+ this.classNames);
|
|
|
|
|
}
|
|
|
|
|
return this.classNames.isEmpty() ? null : this.classNames.iterator().next();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|