|
21 | 21 | import com.google.cloud.tools.jib.filesystem.DirectoryWalker; |
22 | 22 | import com.google.common.annotations.VisibleForTesting; |
23 | 23 | import com.google.common.base.Preconditions; |
24 | | -import com.google.common.base.Splitter; |
25 | 24 | import java.io.IOException; |
26 | | -import java.lang.reflect.Method; |
| 25 | +import java.io.InputStream; |
27 | 26 | import java.lang.reflect.Modifier; |
28 | 27 | import java.nio.file.Files; |
29 | 28 | import java.nio.file.Path; |
30 | | -import java.util.ArrayDeque; |
31 | 29 | import java.util.ArrayList; |
32 | | -import java.util.Deque; |
33 | | -import java.util.HashMap; |
| 30 | +import java.util.Collections; |
34 | 31 | import java.util.HashSet; |
35 | 32 | import java.util.List; |
36 | | -import java.util.Map; |
37 | 33 | import java.util.Set; |
| 34 | +import javassist.ClassPool; |
| 35 | +import javassist.CtClass; |
| 36 | +import javassist.CtMethod; |
| 37 | +import javassist.NotFoundException; |
38 | 38 | import javax.annotation.Nullable; |
39 | 39 |
|
40 | 40 | /** Infers the main class in an application. */ |
41 | 41 | public class MainClassFinder { |
42 | 42 |
|
43 | | - /** Helper for loading a .class file. */ |
44 | | - @VisibleForTesting |
45 | | - static class ClassFileLoader extends ClassLoader { |
46 | | - |
47 | | - private final Map<Path, Class<?>> definedClasses = new HashMap<>(); |
48 | | - private final Path rootDirectory; |
49 | | - |
50 | | - @VisibleForTesting |
51 | | - ClassFileLoader(Path rootDirectory) { |
52 | | - this.rootDirectory = rootDirectory; |
53 | | - } |
54 | | - |
55 | | - @Nullable |
56 | | - @Override |
57 | | - public Class<?> findClass(String name) { |
58 | | - return findClass(name, getPathFromClassName(name)); |
59 | | - } |
60 | | - |
61 | | - /** |
62 | | - * @param name the name of the class. This should be {@code null} when we call it manually with |
63 | | - * a file, and not {@code null} when it is called internally. |
64 | | - * @param file the .class file defining the class. |
65 | | - * @return the {@link Class} defined by the file, or {@code null} if the class could not be |
66 | | - * defined. |
67 | | - */ |
68 | | - @Nullable |
69 | | - private Class<?> findClass(@Nullable String name, Path file) { |
70 | | - if (definedClasses.containsKey(file)) { |
71 | | - return definedClasses.get(file); |
72 | | - } |
73 | | - |
74 | | - if (!Files.exists(file)) { |
75 | | - // TODO: Log search class failure? |
76 | | - return null; |
77 | | - } |
78 | | - |
79 | | - try { |
80 | | - byte[] bytes = Files.readAllBytes(file); |
81 | | - Class<?> definedClass = defineClass(name, bytes, 0, bytes.length); |
82 | | - definedClasses.put(file, definedClass); |
83 | | - return definedClass; |
84 | | - } catch (IOException | ClassFormatError | SecurityException | NoClassDefFoundError ignored) { |
85 | | - // Not a valid class file |
86 | | - // TODO: Log search class failure when NoClassDefFoundError/SecurityException is caught? |
87 | | - return null; |
88 | | - } |
89 | | - } |
90 | | - |
91 | | - /** Converts a class name (pack.ClassName) to a Path (rootDirectory/pack/ClassName.class). */ |
92 | | - @VisibleForTesting |
93 | | - Path getPathFromClassName(String className) { |
94 | | - Path path = rootDirectory; |
95 | | - Deque<String> folders = new ArrayDeque<>(Splitter.on('.').splitToList(className)); |
96 | | - String fileName = folders.removeLast() + ".class"; |
97 | | - for (String folder : folders) { |
98 | | - path = path.resolve(folder); |
99 | | - } |
100 | | - path = path.resolve(fileName); |
101 | | - return path; |
102 | | - } |
103 | | - } |
104 | | - |
105 | 43 | /** |
106 | 44 | * If {@code mainClass} is {@code null}, tries to infer main class in this order: |
107 | 45 | * |
@@ -139,7 +77,7 @@ public static String resolveMainClass( |
139 | 77 | continue; |
140 | 78 | } |
141 | 79 | visitedRoots.add(root); |
142 | | - mainClasses.addAll(findMainClasses(root)); |
| 80 | + mainClasses.addAll(findMainClasses(root, logger)); |
143 | 81 | } |
144 | 82 |
|
145 | 83 | if (mainClasses.size() == 1) { |
@@ -177,47 +115,58 @@ public static String resolveMainClass( |
177 | 115 | } |
178 | 116 |
|
179 | 117 | /** |
180 | | - * Searches for a .class file containing a main method in a root directory. |
| 118 | + * Finds the classes with {@code public static void main(String[] args)} in {@code rootDirectory}. |
181 | 119 | * |
182 | | - * @return the name of the class if one is found, null if no class is found. |
183 | | - * @throws IOException if searching/reading files fails. |
| 120 | + * @param rootDirectory directory containing the {@code .class} files |
184 | 121 | */ |
185 | 122 | @VisibleForTesting |
186 | | - static List<String> findMainClasses(Path rootDirectory) throws IOException { |
187 | | - List<String> classNames = new ArrayList<>(); |
188 | | - |
189 | | - // Make sure rootDirectory is valid |
| 123 | + static List<String> findMainClasses(Path rootDirectory, BuildLogger buildLogger) |
| 124 | + throws IOException { |
| 125 | + // Makes sure rootDirectory is valid. |
190 | 126 | if (!Files.exists(rootDirectory) || !Files.isDirectory(rootDirectory)) { |
191 | | - return classNames; |
| 127 | + return Collections.emptyList(); |
192 | 128 | } |
193 | 129 |
|
194 | | - // Get all .class files |
195 | | - ClassFileLoader classFileLoader = new ClassFileLoader(rootDirectory); |
196 | | - new DirectoryWalker(rootDirectory) |
197 | | - .filter(Files::isRegularFile) |
198 | | - .filter(path -> path.toString().endsWith(".class")) |
199 | | - .walk( |
200 | | - classFile -> { |
201 | | - Class<?> fileClass = classFileLoader.findClass(null, classFile); |
202 | | - if (fileClass == null) { |
203 | | - return; |
204 | | - } |
205 | | - try { |
206 | | - // Check if class contains {@code public static void main(String[] args)} |
207 | | - Method main = fileClass.getMethod("main", String[].class); |
208 | | - if (main != null |
209 | | - && main.getReturnType() == void.class |
210 | | - && Modifier.isStatic(main.getModifiers()) |
211 | | - && Modifier.isPublic(main.getModifiers())) { |
212 | | - classNames.add(fileClass.getName()); |
| 130 | + List<String> classNames = new ArrayList<>(); |
| 131 | + |
| 132 | + ClassPool classPool = new ClassPool(); |
| 133 | + classPool.appendSystemPath(); |
| 134 | + |
| 135 | + try { |
| 136 | + CtClass[] mainMethodParams = new CtClass[] {classPool.get("java.lang.String[]")}; |
| 137 | + |
| 138 | + new DirectoryWalker(rootDirectory) |
| 139 | + .filter(Files::isRegularFile) |
| 140 | + .filter(path -> path.toString().endsWith(".class")) |
| 141 | + .walk( |
| 142 | + classFile -> { |
| 143 | + try (InputStream classFileInputStream = Files.newInputStream(classFile)) { |
| 144 | + CtClass fileClass = classPool.makeClass(classFileInputStream); |
| 145 | + |
| 146 | + // Check if class contains 'public static void main(String[] args)'. |
| 147 | + CtMethod mainMethod = fileClass.getDeclaredMethod("main", mainMethodParams); |
| 148 | + |
| 149 | + if (CtClass.voidType.equals(mainMethod.getReturnType()) |
| 150 | + && Modifier.isStatic(mainMethod.getModifiers()) |
| 151 | + && Modifier.isPublic(mainMethod.getModifiers())) { |
| 152 | + classNames.add(fileClass.getName()); |
| 153 | + } |
| 154 | + |
| 155 | + } catch (NotFoundException ex) { |
| 156 | + // Ignores main method not found. |
| 157 | + |
| 158 | + } catch (IOException ex) { |
| 159 | + // Could not read class file. |
| 160 | + buildLogger.warn("Could not read class file: " + classFile); |
213 | 161 | } |
214 | | - } catch (NoSuchMethodException | NoClassDefFoundError ignored) { |
215 | | - // main method not found |
216 | | - // TODO: Log search class failure when NoClassDefFoundError is caught? |
217 | | - } |
218 | | - }); |
| 162 | + }); |
| 163 | + |
| 164 | + return classNames; |
219 | 165 |
|
220 | | - return classNames; |
| 166 | + } catch (NotFoundException ex) { |
| 167 | + // Thrown if 'java.lang.String' is not found in classPool. |
| 168 | + throw new RuntimeException(ex); |
| 169 | + } |
221 | 170 | } |
222 | 171 |
|
223 | 172 | private MainClassFinder() {} |
|
0 commit comments