|
1 | | -package com.realtimetech.reflection.classfile; |
2 | | - |
3 | | -import java.io.ByteArrayOutputStream; |
4 | | -import java.io.Closeable; |
5 | | -import java.io.File; |
6 | | -import java.io.FileInputStream; |
7 | | -import java.io.FileNotFoundException; |
8 | | -import java.io.IOException; |
9 | | -import java.io.InputStream; |
10 | | -import java.net.URISyntaxException; |
11 | | -import java.util.ArrayList; |
12 | | -import java.util.Collections; |
13 | | -import java.util.List; |
14 | | -import java.util.jar.JarEntry; |
15 | | -import java.util.jar.JarFile; |
16 | | -import java.util.zip.ZipEntry; |
17 | | - |
18 | | -import com.realtimetech.reflection.classfile.file.ClassFile; |
19 | | -import com.realtimetech.reflection.classfile.file.ClassFileStream; |
20 | | -import com.realtimetech.reflection.classfile.file.ClassFileStreamSet; |
21 | | - |
22 | | -public class ClassFileReader { |
23 | | - public enum ClassType { |
24 | | - JAR, FOLDER |
25 | | - } |
26 | | - |
27 | | - private static ClassType getClassType(Class<?> clazz) { |
28 | | - String rootFolderPath = getClassRootPath(clazz); |
29 | | - if (rootFolderPath != null) { |
30 | | - File rootFolder = new File(rootFolderPath); |
31 | | - File classFile = new File(rootFolder.getPath() + "/" + getClassPath(clazz)); |
32 | | - |
33 | | - if (classFile.exists()) { |
34 | | - return ClassType.FOLDER; |
35 | | - } |
36 | | - |
37 | | - return ClassType.JAR; |
38 | | - } |
39 | | - |
40 | | - return null; |
41 | | - } |
42 | | - |
43 | | - private static ClassFileStreamSet getClassFilesFromFolder(Class<?> clazz) throws FileNotFoundException { |
44 | | - ClassFileStreamSet classFileStreamSet = new ClassFileStreamSet(); |
45 | | - |
46 | | - if (clazz.getSimpleName().isEmpty()) |
47 | | - return classFileStreamSet; |
48 | | - |
49 | | - String rootFolderPath = getClassRootPath(clazz); |
50 | | - if (rootFolderPath != null) { |
51 | | - File rootFolder = new File(rootFolderPath); |
52 | | - File classFile = new File(rootFolder.getPath() + "/" + getClassPath(clazz)); |
53 | | - |
54 | | - List<File> allClassList = new ArrayList<File>(); |
55 | | - |
56 | | - if (classFile.exists()) { |
57 | | - File folder = classFile.getParentFile(); |
58 | | - allClassList.add(classFile); |
59 | | - if (folder.isDirectory()) { |
60 | | - File[] listFiles = folder.listFiles(); |
61 | | - |
62 | | - for (File listFile : listFiles) { |
63 | | - if (listFile.getName().startsWith(clazz.getSimpleName() + "$")) { |
64 | | - allClassList.add(listFile); |
65 | | - } |
66 | | - } |
67 | | - } |
68 | | - } |
69 | | - for (File file : allClassList) { |
70 | | - if (file.exists()) { |
71 | | - InputStream inputStream = new FileInputStream(file); |
72 | | - String className = clazz.getName() + ".class"; |
73 | | - className = className.replace(clazz.getSimpleName() + ".class", file.getName()).replace(".class", |
74 | | - ""); |
75 | | - classFileStreamSet.getCloseableList().add(inputStream); |
76 | | - classFileStreamSet.getClassFileStreamList() |
77 | | - .add(new ClassFileStream(inputStream, file.length(), className)); |
78 | | - } |
79 | | - } |
80 | | - } |
81 | | - return classFileStreamSet; |
82 | | - } |
83 | | - |
84 | | - private static ClassFileStreamSet getClassFilesFromJar(Class<?> clazz) throws IOException { |
85 | | - ClassFileStreamSet classFileStreamSet = new ClassFileStreamSet(); |
86 | | - |
87 | | - if (clazz.getSimpleName().isEmpty()) |
88 | | - return classFileStreamSet; |
89 | | - |
90 | | - JarFile jarFile = new JarFile(getClassRootPath(clazz)); |
91 | | - List<JarEntry> jarEntrys = Collections.list(jarFile.entries()); |
92 | | - |
93 | | - List<ZipEntry> allClassList = new ArrayList<ZipEntry>(); |
94 | | - allClassList.add(jarFile.getEntry(getClassPath(clazz))); |
95 | | - for (JarEntry jarEntry : jarEntrys) { |
96 | | - if (!jarEntry.isDirectory()) { |
97 | | - if (jarEntry.getName().startsWith(clazz.getName().replace(".", "/") + "$") |
98 | | - && jarEntry.getName().toLowerCase().endsWith(".class")) { |
99 | | - allClassList.add(jarEntry); |
100 | | - } |
101 | | - } |
102 | | - } |
103 | | - |
104 | | - for (ZipEntry zipEntry : allClassList) { |
105 | | - InputStream inputStream = jarFile.getInputStream(zipEntry); |
106 | | - |
107 | | - String className = zipEntry.getName().substring(0, zipEntry.getName().length() - 6); |
108 | | - className = className.replace('/', '.'); |
109 | | - |
110 | | - classFileStreamSet.getCloseableList().add(inputStream); |
111 | | - classFileStreamSet.getClassFileStreamList() |
112 | | - .add(new ClassFileStream(inputStream, zipEntry.getSize(), className)); |
113 | | - } |
114 | | - |
115 | | - classFileStreamSet.getCloseableList().add(jarFile); |
116 | | - |
117 | | - return classFileStreamSet; |
118 | | - } |
119 | | - |
120 | | - private static String getClassRootPath(Class<?> clazz) { |
121 | | - try { |
122 | | - return clazz.getProtectionDomain().getCodeSource().getLocation().toURI().getPath(); |
123 | | - } catch (URISyntaxException e) { |
124 | | - return null; |
125 | | - } catch (Exception e) { |
126 | | - return null; |
127 | | - } |
128 | | - } |
129 | | - |
130 | | - private static String getClassPath(Class<?> clazz) { |
131 | | - try { |
132 | | - return clazz.getName().replace(".", "/") + ".class"; |
133 | | - } catch (Exception e) { |
134 | | - return null; |
135 | | - } |
136 | | - } |
137 | | - |
138 | | - public static ClassFile[] getClassBytes(Class<?> clazz) throws IOException { |
139 | | - ClassType classType = getClassType(clazz); |
140 | | - |
141 | | - if (classType == null) |
142 | | - return new ClassFile[] {}; |
143 | | - |
144 | | - ClassFileStreamSet classFileStreamSet = null; |
145 | | - |
146 | | - List<ClassFile> classFileList = new ArrayList<ClassFile>(); |
147 | | - |
148 | | - switch (classType) { |
149 | | - case FOLDER: |
150 | | - classFileStreamSet = getClassFilesFromFolder(clazz); |
151 | | - break; |
152 | | - case JAR: |
153 | | - classFileStreamSet = getClassFilesFromJar(clazz); |
154 | | - break; |
155 | | - } |
156 | | - |
157 | | - for (ClassFileStream classFileStream : classFileStreamSet.getClassFileStreamList()) { |
158 | | - ClassFile readClassFile = getClassFile(classFileStream.getInputStream(), (int) classFileStream.getSize(), |
159 | | - classFileStream.getClassName()); |
160 | | - |
161 | | - if (readClassFile != null) { |
162 | | - classFileList.add(readClassFile); |
163 | | - } |
164 | | - |
165 | | - } |
166 | | - |
167 | | - for (Closeable closeable : classFileStreamSet.getCloseableList()) { |
168 | | - try { |
169 | | - closeable.close(); |
170 | | - } catch (Exception e) { |
171 | | - } |
172 | | - } |
173 | | - |
174 | | - return classFileList.toArray(new ClassFile[classFileList.size()]); |
175 | | - } |
176 | | - |
177 | | - private static byte[] readAllInputStream(InputStream inputStream) throws IOException { |
178 | | - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
179 | | - |
180 | | - int readBytes; |
181 | | - byte[] data = new byte[16384]; |
182 | | - |
183 | | - while ((readBytes = inputStream.read(data, 0, data.length)) != -1) { |
184 | | - byteArrayOutputStream.write(data, 0, readBytes); |
185 | | - } |
186 | | - |
187 | | - return byteArrayOutputStream.toByteArray(); |
188 | | - } |
189 | | - |
190 | | - private static ClassFile getClassFile(InputStream inputStream, int size, String className) { |
191 | | - try { |
192 | | - return new ClassFile(className, ClassFileReader.readAllInputStream(inputStream)); |
193 | | - } catch (IOException e) { |
194 | | - return null; |
195 | | - } |
196 | | - } |
197 | | - |
198 | | -} |
| 1 | +package com.realtimetech.reflection.classfile; |
| 2 | + |
| 3 | +import java.io.ByteArrayOutputStream; |
| 4 | +import java.io.Closeable; |
| 5 | +import java.io.File; |
| 6 | +import java.io.FileInputStream; |
| 7 | +import java.io.FileNotFoundException; |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.InputStream; |
| 10 | +import java.net.URISyntaxException; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.Collections; |
| 13 | +import java.util.List; |
| 14 | +import java.util.jar.JarEntry; |
| 15 | +import java.util.jar.JarFile; |
| 16 | +import java.util.zip.ZipEntry; |
| 17 | + |
| 18 | +import com.realtimetech.reflection.classfile.file.ClassFile; |
| 19 | +import com.realtimetech.reflection.classfile.file.ClassFileStream; |
| 20 | +import com.realtimetech.reflection.classfile.file.ClassFileStreamSet; |
| 21 | + |
| 22 | +public class ClassFileReader { |
| 23 | + public enum ClassType { |
| 24 | + JAR, FOLDER |
| 25 | + } |
| 26 | + |
| 27 | + private static ClassType getClassType(Class<?> clazz) { |
| 28 | + String rootFolderPath = getClassRootPath(clazz); |
| 29 | + if (rootFolderPath != null) { |
| 30 | + File rootFolder = new File(rootFolderPath); |
| 31 | + File classFile = new File(rootFolder.getPath() + "/" + getClassPath(clazz)); |
| 32 | + |
| 33 | + if (classFile.exists()) { |
| 34 | + return ClassType.FOLDER; |
| 35 | + } |
| 36 | + |
| 37 | + return ClassType.JAR; |
| 38 | + } |
| 39 | + |
| 40 | + return null; |
| 41 | + } |
| 42 | + |
| 43 | + private static ClassFileStreamSet getClassFilesFromFolder(Class<?> clazz) throws FileNotFoundException { |
| 44 | + ClassFileStreamSet classFileStreamSet = new ClassFileStreamSet(); |
| 45 | + |
| 46 | + if (clazz.getSimpleName().isEmpty()) |
| 47 | + return classFileStreamSet; |
| 48 | + |
| 49 | + String rootFolderPath = getClassRootPath(clazz); |
| 50 | + if (rootFolderPath != null) { |
| 51 | + File rootFolder = new File(rootFolderPath); |
| 52 | + File classFile = new File(rootFolder.getPath() + "/" + getClassPath(clazz)); |
| 53 | + |
| 54 | + List<File> allClassList = new ArrayList<File>(); |
| 55 | + |
| 56 | + if (classFile.exists()) { |
| 57 | + File folder = classFile.getParentFile(); |
| 58 | + allClassList.add(classFile); |
| 59 | + if (folder.isDirectory()) { |
| 60 | + File[] listFiles = folder.listFiles(); |
| 61 | + |
| 62 | + for (File listFile : listFiles) { |
| 63 | + if (listFile.getName().startsWith(clazz.getSimpleName() + "$")) { |
| 64 | + allClassList.add(listFile); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + for (File file : allClassList) { |
| 70 | + if (file.exists()) { |
| 71 | + InputStream inputStream = new FileInputStream(file); |
| 72 | + String className = clazz.getName() + ".class"; |
| 73 | + className = className.replace(clazz.getSimpleName() + ".class", file.getName()).replace(".class", |
| 74 | + ""); |
| 75 | + classFileStreamSet.getCloseableList().add(inputStream); |
| 76 | + classFileStreamSet.getClassFileStreamList() |
| 77 | + .add(new ClassFileStream(inputStream, file.length(), className)); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + return classFileStreamSet; |
| 82 | + } |
| 83 | + |
| 84 | + private static ClassFileStreamSet getClassFilesFromJar(Class<?> clazz) throws IOException { |
| 85 | + ClassFileStreamSet classFileStreamSet = new ClassFileStreamSet(); |
| 86 | + |
| 87 | + if (clazz.getSimpleName().isEmpty()) |
| 88 | + return classFileStreamSet; |
| 89 | + |
| 90 | + JarFile jarFile = new JarFile(getClassRootPath(clazz)); |
| 91 | + List<JarEntry> jarEntrys = Collections.list(jarFile.entries()); |
| 92 | + |
| 93 | + List<ZipEntry> allClassList = new ArrayList<ZipEntry>(); |
| 94 | + allClassList.add(jarFile.getEntry(getClassPath(clazz))); |
| 95 | + for (JarEntry jarEntry : jarEntrys) { |
| 96 | + if (!jarEntry.isDirectory()) { |
| 97 | + if (jarEntry.getName().startsWith(clazz.getName().replace(".", "/") + "$") |
| 98 | + && jarEntry.getName().toLowerCase().endsWith(".class")) { |
| 99 | + allClassList.add(jarEntry); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + for (ZipEntry zipEntry : allClassList) { |
| 105 | + InputStream inputStream = jarFile.getInputStream(zipEntry); |
| 106 | + |
| 107 | + String className = zipEntry.getName().substring(0, zipEntry.getName().length() - 6); |
| 108 | + className = className.replace('/', '.'); |
| 109 | + |
| 110 | + classFileStreamSet.getCloseableList().add(inputStream); |
| 111 | + classFileStreamSet.getClassFileStreamList() |
| 112 | + .add(new ClassFileStream(inputStream, zipEntry.getSize(), className)); |
| 113 | + } |
| 114 | + |
| 115 | + classFileStreamSet.getCloseableList().add(jarFile); |
| 116 | + |
| 117 | + return classFileStreamSet; |
| 118 | + } |
| 119 | + |
| 120 | + private static String getClassRootPath(Class<?> clazz) { |
| 121 | + try { |
| 122 | + return clazz.getProtectionDomain().getCodeSource().getLocation().toURI().getPath(); |
| 123 | + } catch (URISyntaxException e) { |
| 124 | + return null; |
| 125 | + } catch (Exception e) { |
| 126 | + return null; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private static String getClassPath(Class<?> clazz) { |
| 131 | + try { |
| 132 | + return clazz.getName().replace(".", "/") + ".class"; |
| 133 | + } catch (Exception e) { |
| 134 | + return null; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + public static ClassFile[] getClassBytes(Class<?> clazz) throws IOException { |
| 139 | + ClassType classType = getClassType(clazz); |
| 140 | + |
| 141 | + if (classType == null) |
| 142 | + return new ClassFile[] {}; |
| 143 | + |
| 144 | + ClassFileStreamSet classFileStreamSet = null; |
| 145 | + |
| 146 | + List<ClassFile> classFileList = new ArrayList<ClassFile>(); |
| 147 | + |
| 148 | + switch (classType) { |
| 149 | + case FOLDER: |
| 150 | + classFileStreamSet = getClassFilesFromFolder(clazz); |
| 151 | + break; |
| 152 | + case JAR: |
| 153 | + classFileStreamSet = getClassFilesFromJar(clazz); |
| 154 | + break; |
| 155 | + } |
| 156 | + |
| 157 | + for (ClassFileStream classFileStream : classFileStreamSet.getClassFileStreamList()) { |
| 158 | + ClassFile readClassFile = getClassFile(classFileStream.getInputStream(), (int) classFileStream.getSize(), |
| 159 | + classFileStream.getClassName()); |
| 160 | + |
| 161 | + if (readClassFile != null) { |
| 162 | + classFileList.add(readClassFile); |
| 163 | + } |
| 164 | + |
| 165 | + } |
| 166 | + |
| 167 | + for (Closeable closeable : classFileStreamSet.getCloseableList()) { |
| 168 | + try { |
| 169 | + closeable.close(); |
| 170 | + } catch (Exception e) { |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + return classFileList.toArray(new ClassFile[classFileList.size()]); |
| 175 | + } |
| 176 | + |
| 177 | + private static byte[] readAllInputStream(InputStream inputStream) throws IOException { |
| 178 | + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
| 179 | + |
| 180 | + int readBytes; |
| 181 | + byte[] data = new byte[16384]; |
| 182 | + |
| 183 | + while ((readBytes = inputStream.read(data, 0, data.length)) != -1) { |
| 184 | + byteArrayOutputStream.write(data, 0, readBytes); |
| 185 | + } |
| 186 | + |
| 187 | + return byteArrayOutputStream.toByteArray(); |
| 188 | + } |
| 189 | + |
| 190 | + private static ClassFile getClassFile(InputStream inputStream, int size, String className) { |
| 191 | + try { |
| 192 | + return new ClassFile(className, ClassFileReader.readAllInputStream(inputStream)); |
| 193 | + } catch (IOException e) { |
| 194 | + return null; |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | +} |
0 commit comments