add agent module

This commit is contained in:
金戟 2020-05-30 00:28:07 +08:00
parent 4f7fb190db
commit 0410990a90
28 changed files with 207 additions and 61 deletions

1
.gitignore vendored
View File

@ -26,6 +26,7 @@ target/
*.java~
*.properties~
*.xml~
dependency-reduced-pom.xml
# system ignore
.DS_Store

56
agent/pom.xml Executable file
View File

@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.alibaba</groupId>
<artifactId>testable-agent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>8.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Premain-Class>com.alibaba.testable.PreMain</Premain-Class>
<Can-Retransform-Classes>true</Can-Retransform-Classes>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<relocations>
<relocation>
<pattern>org.objectweb.asm</pattern>
<shadedPattern>agent.org.objectweb.asm</shadedPattern>
</relocation>
</relocations>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,31 @@
package com.alibaba.testable;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
public class ClassPrinter extends ClassVisitor {
public ClassPrinter(ClassWriter cw) {
super(Opcodes.ASM8, cw);
}
@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
System.out.println(name + " extends " + superName + " {");
super.visit(version, access, name, signature, superName, interfaces);
}
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
System.out.println(" " + name + desc);
return super.visitMethod(access, name, desc, signature, exceptions);
}
@Override
public void visitEnd() {
System.out.println("}");
super.visitEnd();
}
}

View File

@ -0,0 +1,12 @@
package com.alibaba.testable;
import java.lang.instrument.Instrumentation;
public class PreMain {
public static void premain(String agentArgs, Instrumentation inst) {
inst.addTransformer(new TestableFileTransformer());
}
}

View File

@ -0,0 +1,28 @@
package com.alibaba.testable;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain;
public class TestableFileTransformer implements ClassFileTransformer {
private static String targetClass = "com/alibaba/testable/TransClass";
@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
ProtectionDomain protectionDomain, byte[] classfileBuffer)
throws IllegalClassFormatException {
if (targetClass.equals(className)) {
// Print class structure with ASM
ClassReader reader = new ClassReader(classfileBuffer);
ClassWriter writer = new ClassWriter(reader, 0);
ClassPrinter visitor = new ClassPrinter(writer);
reader.accept(visitor, 0);
return writer.toByteArray();
}
return null;
}
}

53
core/pom.xml Normal file
View File

@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<name>Testable</name>
<description>Unit test enhancement toolkit</description>
<groupId>com.alibaba</groupId>
<artifactId>testable-core</artifactId>
<version>0.0.2-SNAPSHOT</version>
<properties>
<plugin.compiler.version>3.8.1</plugin.compiler.version>
<plugin.surefire.version>3.0.0-M4</plugin.surefire.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.compiler.level>1.7</project.compiler.level>
</properties>
<dependencies>
<dependency>
<groupId>sun.jdk</groupId>
<artifactId>tools</artifactId>
<version>${java.version}</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${plugin.compiler.version}</version>
<configuration>
<source>${project.compiler.level}</source>
<target>${project.compiler.level}</target>
<encoding>${project.build.sourceEncoding}</encoding>
<compilerArgument>-proc:none</compilerArgument>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${plugin.surefire.version}</version>
</plugin>
</plugins>
</build>
</project>

View File

@ -2,9 +2,10 @@ package com.alibaba.testable.generator;
import com.alibaba.testable.model.TestableContext;
import java.io.File;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* Generate global n.e class code
@ -18,13 +19,17 @@ public class StaticNewClassGenerator extends BaseGenerator {
}
public String fetch() {
ClassLoader classLoader = this.getClass().getClassLoader();
File file = new File(classLoader.getResource("e.java").getFile());
if (!file.exists()) {
cx.logger.error("Failed to fetch testable new stand-in.");
}
InputStream in = getClass().getResourceAsStream("/e.java");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
try {
return new String(Files.readAllBytes(file.toPath()));
String line;
StringBuffer buffer = new StringBuffer();
while ((line = reader.readLine()) != null)
{
buffer.append(line).append('\n');
}
reader.close();
return buffer.toString();
} catch (IOException e) {
cx.logger.error("Failed to generate testable new stand-in.");
return "";

View File

@ -170,7 +170,8 @@ public class EnableTestableInjectTranslator extends BaseTranslator {
args.addAll(param);
JCMethodInvocation apply = cx.treeMaker.Apply(List.<JCExpression>nil(), snMethod, args.toList());
for (JCMethodDecl m : methods) {
if (m.restype != null && m.name.equals(methodName) && paramEquals(m.params, param)) {
if (m.restype != null && !m.restype.toString().equals(ConstPool.VOID) &&
m.name.equals(methodName) && paramEquals(m.params, param)) {
JCTypeCast cast = cx.treeMaker.TypeCast(m.restype, apply);
return cx.treeMaker.Parens(cast);
}

View File

@ -15,6 +15,7 @@ public final class ConstPool {
public static final String NE_ADD_F = NE_PKG_CLS + ".af";
public static final String TYPE_TO_CLASS = "class";
public static final String REF_THIS = "this";
public static final String VOID = "void";
public static final String TESTABLE_PRIVATE_ACCESSOR = "com.alibaba.testable.accessor.PrivateAccessor";
public static final String ANNOTATION_TESTABLE_INJECT = "com.alibaba.testable.annotation.TestableInject";
public static final String ANNOTATION_JUNIT5_SETUP = "org.junit.jupiter.api.BeforeEach";

62
pom.xml Normal file → Executable file
View File

@ -1,58 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<modelVersion>4.0.0</modelVersion>
<name>Testable</name>
<description>Unit test enhancement toolkit</description>
<groupId>com.alibaba</groupId>
<artifactId>testable</artifactId>
<version>0.0.2-SNAPSHOT</version>
<groupId>com.alibaba</groupId>
<artifactId>testable-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<plugin.compiler.version>3.8.1</plugin.compiler.version>
<plugin.surefire.version>3.0.0-M4</plugin.surefire.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.compiler.level>1.7</project.compiler.level>
</properties>
<dependencies>
<dependency>
<groupId>com.squareup</groupId>
<artifactId>javapoet</artifactId>
<version>1.12.1</version>
</dependency>
<dependency>
<groupId>sun.jdk</groupId>
<artifactId>tools</artifactId>
<version>${java.version}</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${plugin.compiler.version}</version>
<configuration>
<source>${project.compiler.level}</source>
<target>${project.compiler.level}</target>
<encoding>${project.build.sourceEncoding}</encoding>
<compilerArgument>-proc:none</compilerArgument>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${plugin.surefire.version}</version>
</plugin>
</plugins>
</build>
<modules>
<module>core</module>
<module>agent</module>
</modules>
</project>