|
18 | 18 | import java.io.FileInputStream;
|
19 | 19 | import java.io.IOException;
|
20 | 20 | import java.util.Collection;
|
| 21 | +import java.util.Collections; |
21 | 22 | import java.util.HashMap;
|
| 23 | +import java.util.HashSet; |
| 24 | +import java.util.List; |
22 | 25 | import java.util.Map;
|
23 | 26 |
|
24 | 27 | public class BaseTransform {
|
@@ -66,9 +69,11 @@ public void startTransform() {
|
66 | 69 | case NOTCHANGED:
|
67 | 70 | break;
|
68 | 71 | case ADDED:
|
69 |
| - case CHANGED: |
70 | 72 | foreachJar(dest, jarInput);
|
71 | 73 | break;
|
| 74 | + case CHANGED: |
| 75 | + diffJar(dest, jarInput); |
| 76 | + break; |
72 | 77 | case REMOVED:
|
73 | 78 | try {
|
74 | 79 | deleteScan(dest);
|
@@ -108,56 +113,87 @@ private void foreachClass(DirectoryInput directoryInput) throws IOException {
|
108 | 113 | break;
|
109 | 114 | case ADDED:
|
110 | 115 | case CHANGED:
|
| 116 | + try { |
| 117 | + FileUtils.touch(destFile); |
| 118 | + } catch (Exception ignored) { |
| 119 | + Files.createParentDirs(destFile); |
| 120 | + } |
111 | 121 | modifySingleFile(dir, file, dest);
|
112 | 122 | break;
|
113 | 123 | case REMOVED:
|
114 |
| - deleteSingleFile(destFile, dest); |
| 124 | + Log.info(entry); |
| 125 | + deleteDirectory(destFile, dest); |
115 | 126 | break;
|
116 | 127 | }
|
117 | 128 | }
|
118 | 129 | } else {
|
119 | 130 | changeFile(dir, dest);
|
120 | 131 | }
|
121 |
| - Log.info("||-->结束遍历特定目录 ${dest.absolutePath}"); |
122 | 132 | }
|
123 | 133 |
|
124 |
| - private void deleteSingleFile(File destFile, File dest) { |
| 134 | + private void deleteDirectory(File destFile, File dest) { |
125 | 135 | try {
|
126 |
| - byte[] bytes = IOUtils.toByteArray(new FileInputStream(destFile)); |
127 |
| - String absolutePath = destFile.getAbsolutePath().replace(dest.getAbsolutePath() + File.separator, ""); |
128 |
| - String className = ClassUtils.path2Classname(absolutePath); |
129 |
| - if (deleteCallBack != null) { |
130 |
| - deleteCallBack.delete(className, bytes); |
| 136 | + if (destFile.isDirectory()) { |
| 137 | + for (File classFile : com.android.utils.FileUtils.getAllFiles(destFile)) { |
| 138 | + deleteSingle(classFile, dest); |
| 139 | + } |
| 140 | + } else { |
| 141 | + deleteSingle(destFile, dest); |
131 | 142 | }
|
| 143 | + } catch (Exception e) { |
| 144 | + e.printStackTrace(); |
| 145 | + } |
| 146 | + try { |
132 | 147 | if (destFile.exists()) {
|
133 | 148 | FileUtils.forceDelete(destFile);
|
134 | 149 | }
|
135 |
| - } catch (Exception e) { |
| 150 | + } catch (IOException e) { |
136 | 151 | e.printStackTrace();
|
137 | 152 | }
|
| 153 | + } |
138 | 154 |
|
| 155 | + private void deleteSingle(File classFile, File dest) { |
| 156 | + try { |
| 157 | + if (classFile.getName().endsWith(".class")) { |
| 158 | + String absolutePath = classFile.getAbsolutePath().replace(dest.getAbsolutePath() + |
| 159 | + File.separator, ""); |
| 160 | + String className = ClassUtils.path2Classname(absolutePath); |
| 161 | + byte[] bytes = IOUtils.toByteArray(new FileInputStream(classFile)); |
| 162 | + if (deleteCallBack != null) { |
| 163 | + deleteCallBack.delete(className, bytes); |
| 164 | + } |
| 165 | + } |
| 166 | + } catch (Exception e) { |
| 167 | + e.printStackTrace(); |
| 168 | + } |
139 | 169 | }
|
140 | 170 |
|
141 | 171 | private void modifySingleFile(File dir, File file, File dest) throws IOException {
|
142 | 172 | try {
|
143 |
| - FileUtils.touch(dest); |
144 |
| - } catch (Exception ignored) { |
145 |
| - //maybe mkdirs fail for some strange reason, try again. |
146 |
| - Files.createParentDirs(dest); |
147 |
| - } |
148 |
| - String absolutePath = file.getAbsolutePath().replace(dir.getAbsolutePath() + File.separator, ""); |
149 |
| - String className = ClassUtils.path2Classname(absolutePath); |
150 |
| - byte[] bytes = IOUtils.toByteArray(new FileInputStream(file)); |
151 |
| - byte[] modifiedBytes = callBack.process(className, bytes, this); |
152 |
| - if (modifiedBytes != null) { |
153 |
| - File modified = ClassUtils.saveFile(file, modifiedBytes); |
154 |
| - String key = file.getAbsolutePath().replace(dir.getAbsolutePath(), ""); |
155 |
| - File target = new File(dest.getAbsolutePath() + key); |
156 |
| - if (target.exists()) { |
157 |
| - target.delete(); |
| 173 | + String absolutePath = file.getAbsolutePath().replace(dir.getAbsolutePath() + File.separator, ""); |
| 174 | + String className = ClassUtils.path2Classname(absolutePath); |
| 175 | + if (!ClassUtils.checkClassName(className)) { |
| 176 | + byte[] bytes = IOUtils.toByteArray(new FileInputStream(file)); |
| 177 | + byte[] modifiedBytes = null; |
| 178 | + try { |
| 179 | + modifiedBytes = callBack.process(className, bytes, this); |
| 180 | + } catch (Exception ignored) { |
| 181 | + |
| 182 | + } |
| 183 | + if (modifiedBytes == null) { |
| 184 | + modifiedBytes = bytes; |
| 185 | + } |
| 186 | + File modified = ClassUtils.saveFile(file, modifiedBytes); |
| 187 | + String key = file.getAbsolutePath().replace(dir.getAbsolutePath(), ""); |
| 188 | + File target = new File(dest.getAbsolutePath() + key); |
| 189 | + if (target.exists()) { |
| 190 | + target.delete(); |
| 191 | + } |
| 192 | + FileUtils.copyFile(modified, target); |
| 193 | + modified.delete(); |
158 | 194 | }
|
159 |
| - FileUtils.copyFile(modified, target); |
160 |
| - modified.delete(); |
| 195 | + } catch (Exception e) { |
| 196 | + e.printStackTrace(); |
161 | 197 | }
|
162 | 198 | }
|
163 | 199 |
|
@@ -193,17 +229,36 @@ private void changeFile(File dir, File dest) throws IOException {
|
193 | 229 | }
|
194 | 230 | }
|
195 | 231 |
|
196 |
| - private void foreachJar(File dest, JarInput jarInput) throws IOException { |
197 |
| - File modifiedJar = JarUtils.modifyJarFile(jarInput.getFile(), context.getTemporaryDir(), |
198 |
| - callBack, this); |
199 |
| - if (modifiedJar == null) { |
200 |
| - modifiedJar = jarInput.getFile(); |
| 232 | + private void foreachJar(File dest, JarInput jarInput) { |
| 233 | + try { |
| 234 | + File modifiedJar = JarUtils.modifyJarFile(jarInput.getFile(), context.getTemporaryDir(), |
| 235 | + callBack, this); |
| 236 | + if (modifiedJar == null) { |
| 237 | + modifiedJar = jarInput.getFile(); |
| 238 | + } |
| 239 | + FileUtils.copyFile(modifiedJar, dest); |
| 240 | + } catch (Exception e) { |
| 241 | + e.printStackTrace(); |
201 | 242 | }
|
202 |
| - FileUtils.copyFile(modifiedJar, dest); |
203 |
| - Log.info("||-->结束遍历jar"); |
204 | 243 | }
|
205 | 244 |
|
206 | 245 |
|
| 246 | + private void diffJar(File dest, JarInput jarInput) { |
| 247 | + try { |
| 248 | + HashSet<String> oldJarFileName = JarUtils.scanJarFile(dest); |
| 249 | + HashSet<String> newJarFileName = JarUtils.scanJarFile(jarInput.getFile()); |
| 250 | + SetDiff diff = new SetDiff<>(oldJarFileName, newJarFileName); |
| 251 | + List<String> removeList = diff.getRemovedList(); |
| 252 | + Log.info("diffList:" + removeList); |
| 253 | + if (removeList.size() > 0) { |
| 254 | + JarUtils.deleteJarScan(dest, removeList, deleteCallBack); |
| 255 | + } |
| 256 | + foreachJar(dest, jarInput); |
| 257 | + } catch (Exception e) { |
| 258 | + e.printStackTrace(); |
| 259 | + } |
| 260 | + } |
| 261 | + |
207 | 262 | private void deleteScan(File dest) {
|
208 | 263 | try {
|
209 | 264 | JarUtils.deleteJarScan(dest, deleteCallBack);
|
|
0 commit comments