Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
wangjie-fourth committed Sep 3, 2023
1 parent 6775583 commit c98ea02
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static void agentmain(String agentArgs, Instrumentation inst) {
private static void transformClass(Instrumentation instrumentation) {
Class<?> targetCls;
ClassLoader targetClassLoader;
for (ClassModifyInfo classModifyInfo : MockDataService.needReTransformClassNameList) {
for (ClassModifyInfo classModifyInfo : MockDataService.NEED_RE_TRANSFORM_CLASS_NAME_LIST) {
String className = classModifyInfo.getFullClassName();
// see if we can get the class using forName
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.github.fourth.mocksystem.agent.entity;


import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import java.util.List;

@SuppressFBWarnings({"EI_EXPOSE_REP", "EI_EXPOSE_REP2"})
public class ClassModifyInfo {
private String fullClassName;
private List<MethodModifyInfo> methodNameList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.github.fourth.mocksystem.agent.service;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
Expand All @@ -11,8 +12,9 @@
import java.lang.instrument.ClassFileTransformer;
import java.security.ProtectionDomain;

import static org.github.fourth.mocksystem.agent.service.MockDataService.needReTransformClassNameList;
import static org.github.fourth.mocksystem.agent.service.MockDataService.NEED_RE_TRANSFORM_CLASS_NAME_LIST;

@SuppressFBWarnings({"EI_EXPOSE_REP"})
public class CommonTransformer implements ClassFileTransformer {

/** The class loader of the class we want to transform */
Expand All @@ -26,7 +28,7 @@ public CommonTransformer(ClassLoader targetClassLoader) {
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) {
byte[] byteCode = classfileBuffer;
// 判断这个class需要不需要转换
ClassModifyInfo classModifyInfo = needReTransformClassNameList.stream().filter(x -> className.equals(x.getFullClassName().replaceAll("\\.", "/"))).findFirst().orElse(null);
ClassModifyInfo classModifyInfo = NEED_RE_TRANSFORM_CLASS_NAME_LIST.stream().filter(x -> className.equals(x.getFullClassName().replaceAll("\\.", "/"))).findFirst().orElse(null);
if (classModifyInfo == null) {
return byteCode;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.github.fourth.mocksystem.agent.service;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.github.fourth.mocksystem.agent.entity.ClassModifyInfo;
import org.github.fourth.mocksystem.agent.entity.MethodModifyInfo;
import org.github.fourth.mocksystem.testapplication.testcase.AddBeforeAfterCodeCase;
Expand All @@ -9,7 +10,9 @@
import java.util.List;

public class MockDataService {
public static List<ClassModifyInfo> needReTransformClassNameList = new ArrayList<>();

@SuppressFBWarnings("MS_MUTABLE_COLLECTION")
public static final List<ClassModifyInfo> NEED_RE_TRANSFORM_CLASS_NAME_LIST = new ArrayList<>();

static {
ClassModifyInfo classModifyInfo = new ClassModifyInfo();
Expand All @@ -20,7 +23,7 @@ public class MockDataService {
methodModifyInfo.setRunBeforeCode(AddBeforeAfterCodeCase.BEFORE_CODE);
methodModifyInfo.setRunAfterCode(AddBeforeAfterCodeCase.AFTER_CODE);
classModifyInfo.getMethodNameList().add(methodModifyInfo);
needReTransformClassNameList.add(classModifyInfo);
NEED_RE_TRANSFORM_CLASS_NAME_LIST.add(classModifyInfo);
}

static {
Expand All @@ -33,7 +36,7 @@ public class MockDataService {
methodModifyInfo.setRunAfterCode(null);
methodModifyInfo.setMockResponseDataStr(MockMethodResponse.MOCK_RESPONSE_DATA_STR);
classModifyInfo.getMethodNameList().add(methodModifyInfo);
needReTransformClassNameList.add(classModifyInfo);
NEED_RE_TRANSFORM_CLASS_NAME_LIST.add(classModifyInfo);
}


Expand Down
12 changes: 11 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,17 @@
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>

<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-annotations</artifactId>
<version>4.7.3</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
Expand Down

0 comments on commit c98ea02

Please sign in to comment.