尝试合并Transform
This commit is contained in:
@@ -9,9 +9,8 @@ import com.plugin.component.extension.module.ProjectInfo
|
||||
import com.plugin.component.extension.option.debug.DebugDependenciesOption
|
||||
import com.plugin.component.extension.option.publication.PublicationDependenciesOption
|
||||
import com.plugin.component.extension.option.publication.PublicationOption
|
||||
import com.plugin.component.transform.InjectCodeTransform
|
||||
import com.plugin.component.transform.MethodCostTransform
|
||||
import com.plugin.component.transform.ScanCodeTransform2
|
||||
import com.plugin.component.transform.ComponentTransform
|
||||
import com.plugin.component.utils.JarUtil
|
||||
import com.plugin.component.utils.ProjectUtil
|
||||
import com.plugin.component.utils.PublicationUtil
|
||||
@@ -228,9 +227,9 @@ class ComponentPlugin implements Plugin<Project> {
|
||||
if (it instanceof AppPlugin) {
|
||||
if (projectInfo.isDebugModule() || projectInfo.isMainModule()) {
|
||||
Logger.buildOutput("plugin is AppPlugin and isDebugModule or isMainModule")
|
||||
Logger.buildOutput("registerTransform", "ScanCodeTransform2")
|
||||
Logger.buildOutput("registerTransform", "ComponentTransform")
|
||||
// Logger.buildOutput("registerTransform", "InjectCodeTransform")
|
||||
childProject.extensions.findByType(BaseExtension.class).registerTransform(new ScanCodeTransform2(childProject))
|
||||
childProject.extensions.findByType(BaseExtension.class).registerTransform(new ComponentTransform(childProject))
|
||||
// childProject.extensions.findByType(BaseExtension.class).registerTransform(new InjectCodeTransform(childProject))
|
||||
if (Runtimes.enbaleMethodCost()) {
|
||||
Logger.buildOutput("registerTransform", "MethodCostTransform")
|
||||
|
||||
@@ -36,7 +36,7 @@ import java.util.Set;
|
||||
* desc :
|
||||
* version: 1.0
|
||||
*/
|
||||
public class ScanCodeTransform2 extends Transform {
|
||||
public class ComponentTransform extends Transform {
|
||||
|
||||
private final Logger logger;
|
||||
|
||||
@@ -49,15 +49,15 @@ public class ScanCodeTransform2 extends Transform {
|
||||
}
|
||||
|
||||
private Project project;
|
||||
protected ScanCodeWeaver2 bytecodeWeaver;
|
||||
protected ComponentWeaver bytecodeWeaver;
|
||||
private WaitableExecutor waitableExecutor;
|
||||
private boolean emptyRun = false;
|
||||
|
||||
public ScanCodeTransform2(Project project) {
|
||||
public ComponentTransform(Project project) {
|
||||
this.project = project;
|
||||
this.logger = project.getLogger();
|
||||
this.waitableExecutor = WaitableExecutor.useGlobalSharedThreadPool();
|
||||
this.bytecodeWeaver = new ScanCodeWeaver2();
|
||||
this.bytecodeWeaver = new ComponentWeaver();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -77,7 +77,7 @@ public class ScanCodeTransform2 extends Transform {
|
||||
|
||||
@Override
|
||||
public boolean isIncremental() {
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ import java.util.zip.ZipOutputStream;
|
||||
* desc :
|
||||
* version: 1.0
|
||||
*/
|
||||
public class ScanCodeWeaver2 implements IWeaver {
|
||||
public class ComponentWeaver implements IWeaver {
|
||||
|
||||
private static final FileTime ZERO = FileTime.fromMillis(0);
|
||||
private static final String FILE_SEP = File.separator;
|
||||
@@ -38,7 +38,7 @@ public class ScanCodeWeaver2 implements IWeaver {
|
||||
|
||||
protected ClassLoader classLoader;
|
||||
|
||||
public ScanCodeWeaver2() {
|
||||
public ComponentWeaver() {
|
||||
}
|
||||
|
||||
|
||||
@@ -47,6 +47,7 @@ public class ScanCodeWeaver2 implements IWeaver {
|
||||
}
|
||||
|
||||
public final void weaveJar(File inputJar, File outputJar) throws IOException {
|
||||
String filePath = inputJar.getAbsolutePath();
|
||||
ZipFile inputZip = new ZipFile(inputJar);
|
||||
ZipOutputStream outputZip = new ZipOutputStream(new BufferedOutputStream(java.nio.file.Files.newOutputStream(outputJar.toPath())));
|
||||
Enumeration<? extends ZipEntry> inEntries = inputZip.entries();
|
||||
@@ -64,7 +65,7 @@ public class ScanCodeWeaver2 implements IWeaver {
|
||||
if (!isWeavableClass(className)) {
|
||||
newEntryContent = org.apache.commons.io.IOUtils.toByteArray(originalFile);
|
||||
} else {
|
||||
newEntryContent = weaveSingleClassToByteArray(originalFile);
|
||||
newEntryContent = weaveSingleClassToByteArray(filePath, originalFile);
|
||||
}
|
||||
CRC32 crc32 = new CRC32();
|
||||
crc32.update(newEntryContent);
|
||||
@@ -87,12 +88,13 @@ public class ScanCodeWeaver2 implements IWeaver {
|
||||
if (!inputBaseDir.endsWith(FILE_SEP)) inputBaseDir = inputBaseDir + FILE_SEP;
|
||||
String className = inputFile.getAbsolutePath().replace(inputBaseDir, "").replace(FILE_SEP, ".");
|
||||
|
||||
String filePath = inputFile.getAbsolutePath();
|
||||
beforeWeaveClass(outputFile.getAbsolutePath(), className);
|
||||
|
||||
if (isWeavableClass(className)) {
|
||||
FileUtils.touch(outputFile);
|
||||
InputStream inputStream = new FileInputStream(inputFile);
|
||||
byte[] bytes = weaveSingleClassToByteArray(inputStream);
|
||||
byte[] bytes = weaveSingleClassToByteArray(filePath, inputStream);
|
||||
FileOutputStream fos = new FileOutputStream(outputFile);
|
||||
fos.write(bytes);
|
||||
fos.close();
|
||||
@@ -118,6 +120,15 @@ public class ScanCodeWeaver2 implements IWeaver {
|
||||
return classWriter.toByteArray();
|
||||
}
|
||||
|
||||
public byte[] weaveSingleClassToByteArray(String filePath, InputStream inputStream) throws IOException {
|
||||
ClassReader classReader = new ClassReader(inputStream);
|
||||
ClassWriter classWriter = new ExtendClassWriter(classLoader, ClassWriter.COMPUTE_MAXS);
|
||||
ScanCodeAdapter classWriterWrapper = new ScanCodeAdapter(classWriter);
|
||||
classWriterWrapper.setFilePath(filePath);
|
||||
classReader.accept(classWriterWrapper, ClassReader.EXPAND_FRAMES);
|
||||
return classWriter.toByteArray();
|
||||
}
|
||||
|
||||
|
||||
private static final String sComponentManagerPath = "com.plugin.component.ComponentManager";
|
||||
|
||||
@@ -13,11 +13,16 @@ import org.objectweb.asm.Type
|
||||
class ScanCodeAdapter extends ClassVisitor {
|
||||
|
||||
private String className
|
||||
private String filePath
|
||||
|
||||
ScanCodeAdapter(ClassVisitor classVisitor) {
|
||||
super(Opcodes.ASM7, classVisitor)
|
||||
}
|
||||
|
||||
void setFilePath(String filePath) {
|
||||
this.filePath = filePath
|
||||
}
|
||||
|
||||
@Override
|
||||
void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
|
||||
className = name
|
||||
|
||||
Reference in New Issue
Block a user