mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-02-19 08:10:36 +08:00
fit for gradle build in intellij
This commit is contained in:
parent
68075a2ead
commit
5ab1cf7a40
@ -4,7 +4,7 @@ import com.alibaba.testable.processor.constant.ConstPool;
|
|||||||
import com.alibaba.testable.processor.generator.PrivateAccessStatementGenerator;
|
import com.alibaba.testable.processor.generator.PrivateAccessStatementGenerator;
|
||||||
import com.alibaba.testable.processor.model.MemberType;
|
import com.alibaba.testable.processor.model.MemberType;
|
||||||
import com.alibaba.testable.processor.model.TestableContext;
|
import com.alibaba.testable.processor.model.TestableContext;
|
||||||
import com.alibaba.testable.processor.util.StringUtil;
|
import com.alibaba.testable.processor.util.PathUtil;
|
||||||
import com.sun.tools.javac.code.Symbol;
|
import com.sun.tools.javac.code.Symbol;
|
||||||
import com.sun.tools.javac.tree.JCTree.*;
|
import com.sun.tools.javac.tree.JCTree.*;
|
||||||
import com.sun.tools.javac.util.ListBuffer;
|
import com.sun.tools.javac.util.ListBuffer;
|
||||||
@ -14,6 +14,7 @@ import java.io.File;
|
|||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLClassLoader;
|
import java.net.URLClassLoader;
|
||||||
|
|
||||||
@ -26,6 +27,8 @@ public class EnablePrivateAccessTranslator extends BaseTranslator {
|
|||||||
|
|
||||||
private static final String IDEA_PATHS_SELECTOR = "idea.paths.selector";
|
private static final String IDEA_PATHS_SELECTOR = "idea.paths.selector";
|
||||||
private static final String USER_DIR = "user.dir";
|
private static final String USER_DIR = "user.dir";
|
||||||
|
private static final String GRADLE_CLASS_FOLDER = "/build/classes/java/main/";
|
||||||
|
private static final String MAVEN_CLASS_FOLDER = "/target/classes/";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Name of source class
|
* Name of source class
|
||||||
@ -56,6 +59,7 @@ public class EnablePrivateAccessTranslator extends BaseTranslator {
|
|||||||
Class<?> cls = null;
|
Class<?> cls = null;
|
||||||
String sourceClassFullName = pkgName + "." + sourceClass;
|
String sourceClassFullName = pkgName + "." + sourceClass;
|
||||||
try {
|
try {
|
||||||
|
// maven build goes here
|
||||||
cls = Class.forName(sourceClassFullName);
|
cls = Class.forName(sourceClassFullName);
|
||||||
} catch (ClassNotFoundException e) {
|
} catch (ClassNotFoundException e) {
|
||||||
if (System.getProperty(IDEA_PATHS_SELECTOR) != null) {
|
if (System.getProperty(IDEA_PATHS_SELECTOR) != null) {
|
||||||
@ -64,14 +68,18 @@ public class EnablePrivateAccessTranslator extends BaseTranslator {
|
|||||||
String sourceFilePath = sourceFileWrapperString.substring(
|
String sourceFilePath = sourceFileWrapperString.substring(
|
||||||
sourceFileWrapperString.lastIndexOf("[") + 1, sourceFileWrapperString.indexOf("]"));
|
sourceFileWrapperString.lastIndexOf("[") + 1, sourceFileWrapperString.indexOf("]"));
|
||||||
int indexOfSrc = sourceFilePath.lastIndexOf(File.separator + "src" + File.separator);
|
int indexOfSrc = sourceFilePath.lastIndexOf(File.separator + "src" + File.separator);
|
||||||
String targetFolderPath = StringUtil.fitPathString(sourceFilePath.substring(0, indexOfSrc) +
|
String basePath = sourceFilePath.substring(0, indexOfSrc);
|
||||||
"/target/classes/");
|
String targetFolderPath = PathUtil.fitPathString(basePath + MAVEN_CLASS_FOLDER);
|
||||||
cls = new URLClassLoader(new URL[] {new URL(targetFolderPath)}).loadClass(sourceClassFullName);
|
try {
|
||||||
|
cls = loadClass(targetFolderPath, sourceClassFullName);
|
||||||
|
} catch (ClassNotFoundException e2) {
|
||||||
|
targetFolderPath = PathUtil.fitPathString(basePath + GRADLE_CLASS_FOLDER);
|
||||||
|
cls = loadClass(targetFolderPath, sourceClassFullName);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// fit for gradle build
|
// fit for gradle build
|
||||||
String path = StringUtil.fitPathString("file:"
|
String path = PathUtil.fitPathString("file:" + System.getProperty(USER_DIR) + GRADLE_CLASS_FOLDER);
|
||||||
+ System.getProperty(USER_DIR) + "/build/classes/java/main/");
|
cls = loadClass(path, sourceClassFullName);
|
||||||
cls = new URLClassLoader(new URL[] {new URL(path)}).loadClass(sourceClassFullName);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (cls == null) {
|
if (cls == null) {
|
||||||
@ -176,6 +184,11 @@ public class EnablePrivateAccessTranslator extends BaseTranslator {
|
|||||||
return expr;
|
return expr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Class<?> loadClass(String targetFolderPath, String sourceClassFullName)
|
||||||
|
throws ClassNotFoundException, MalformedURLException {
|
||||||
|
return new URLClassLoader(new URL[] {new URL(targetFolderPath)}).loadClass(sourceClassFullName);
|
||||||
|
}
|
||||||
|
|
||||||
private MemberType checkGetterType(JCFieldAccess access) {
|
private MemberType checkGetterType(JCFieldAccess access) {
|
||||||
if (access.selected.getClass().equals(JCIdent.class) &&
|
if (access.selected.getClass().equals(JCIdent.class) &&
|
||||||
privateOrFinalFields.contains(access.name.toString())) {
|
privateOrFinalFields.contains(access.name.toString())) {
|
||||||
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.alibaba.testable.processor.util;
|
||||||
|
|
||||||
|
public class PathUtil {
|
||||||
|
|
||||||
|
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 = "\\";
|
||||||
|
private static final String PROTOCOL_FILE = "file:/";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fit path according to operation system type
|
||||||
|
* @param path original path
|
||||||
|
* @return fitted path
|
||||||
|
*/
|
||||||
|
public static 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.startsWith(PROTOCOL_FILE) ? path : (PROTOCOL_FILE + path);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -7,11 +7,6 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public class StringUtil {
|
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
|
* Join strings
|
||||||
* @param list strings to join
|
* @param list strings to join
|
||||||
@ -32,17 +27,4 @@ public class StringUtil {
|
|||||||
return sb.toString();
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user