fit windows path split

This commit is contained in:
金戟 2020-12-30 16:03:36 +08:00
parent cd7097ded9
commit 783b127bbb
4 changed files with 34 additions and 10 deletions

View File

@ -23,6 +23,8 @@ import java.lang.instrument.ClassFileTransformer;
import java.security.ProtectionDomain;
import java.util.*;
import static com.alibaba.testable.agent.constant.ConstPool.DOT;
import static com.alibaba.testable.agent.constant.ConstPool.SLASH;
import static com.alibaba.testable.agent.util.ClassUtil.toDotSeparateFullClassName;
/**
@ -77,7 +79,7 @@ public class TestableClassTransformer implements ClassFileTransformer {
return;
}
try {
String dumpFile = StringUtil.joinPath(dumpDir, className.replaceAll("/", ".") + ".class");
String dumpFile = StringUtil.joinPath(dumpDir, className.replaceAll(SLASH, DOT) + ".class");
LogUtil.verbose("Dump class: " + dumpFile);
FileOutputStream stream = new FileOutputStream(dumpFile);
stream.write(bytes);

View File

@ -1,5 +1,7 @@
package com.alibaba.testable.agent.util;
import java.io.File;
/**
* @author flin
*/
@ -26,7 +28,7 @@ public class StringUtil {
* @return joined full file path
*/
public static String joinPath(String folder, String file) {
return (folder.endsWith("/") ? folder : (folder + "/")) + file;
return (folder.endsWith(File.separator) ? folder : (folder + File.separator)) + file;
}
}

View File

@ -4,11 +4,13 @@ import com.alibaba.testable.processor.constant.ConstPool;
import com.alibaba.testable.processor.generator.PrivateAccessStatementGenerator;
import com.alibaba.testable.processor.model.MemberType;
import com.alibaba.testable.processor.model.TestableContext;
import com.alibaba.testable.processor.util.StringUtil;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.tree.JCTree.*;
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Name;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
@ -61,12 +63,14 @@ public class EnablePrivateAccessTranslator extends BaseTranslator {
String sourceFileWrapperString = clazz.sourcefile.toString();
String sourceFilePath = sourceFileWrapperString.substring(
sourceFileWrapperString.lastIndexOf("[") + 1, sourceFileWrapperString.indexOf("]"));
String targetFolderPath = sourceFilePath.substring(0, sourceFilePath.lastIndexOf("/src/")) +
"/target/classes/";
int indexOfSrc = sourceFilePath.lastIndexOf(File.separator + "src" + File.separator);
String targetFolderPath = StringUtil.fitPathString(sourceFilePath.substring(0, indexOfSrc) +
"/target/classes/");
cls = new URLClassLoader(new URL[] {new URL(targetFolderPath)}).loadClass(sourceClassFullName);
} else {
// fit for gradle build
String path = "file:" + System.getProperty(USER_DIR) + "/build/classes/java/main/";
String path = StringUtil.fitPathString("file:"
+ System.getProperty(USER_DIR) + "/build/classes/java/main/");
cls = new URLClassLoader(new URL[] {new URL(path)}).loadClass(sourceClassFullName);
}
}

View File

@ -7,18 +7,21 @@ import java.util.List;
*/
public class StringUtil {
private static final String PREFIX_WIN = "win";
private static final String PROPERTY_OS_NAME = "os.name";
private static final String PATH_SPLIT_UNIX = "/";
private static final String PATH_SPLIT_WIN = "\\";
/**
* Join strings
* @param list strings to join
* @param list strings to join
* @param conjunction connection character
* @return joined string
*/
static public String join(List<String> list, String conjunction)
{
static public String join(List<String> list, String conjunction) {
StringBuilder sb = new StringBuilder();
boolean first = true;
for (String item : list)
{
for (String item : list) {
if (first) {
first = false;
} else {
@ -29,4 +32,17 @@ public class StringUtil {
return sb.toString();
}
/**
* Fit path according to operation system type
* @param path original path
* @return fitted path
*/
static public String fitPathString(String path) {
String os = System.getProperty(PROPERTY_OS_NAME);
if (os.toLowerCase().startsWith(PREFIX_WIN)) {
return path.replaceAll(PATH_SPLIT_UNIX, PATH_SPLIT_WIN);
}
return path;
}
}