public static String getClassPath() { // 类库信息 String classPath = System.getProperty("java.library.path"); classPath += System.getProperty("java.class.path"); // System.out.println("^^^^^^^^^ java classpath : " + System.getProperty("java.class.path")); String basePath = IOUtil.getBasePath(); if (PropertiesUtil.exist()) { basePath = PropertiesUtil.getInstance().readValue(PropertiesUtil.CLASS_PATH); } // System.out.println("^^^^^^^^^basePath : " + basePath); File file = new File(basePath); if (!file.exists()) { Log.println("Warn: ClassPath=" + basePath + " does not exist!"); } else if (file.isDirectory()) { classPath += basePath + ";"; classPath += IOUtil.listJarFiles(basePath, ";"); } else if (file.getName().toLowerCase().endsWith(".jar")) { classPath += basePath + ";"; } else { classPath += IOUtil.read(basePath).replace("\r\n", "").replace("\n", ""); } return classPath; }
@Override public boolean compile(TestData data) { System.out.println("...enter JavaCompiler comple(data)..."); if (data == null) return false; // 读取源码 String source = data.getStr(JobConst.TARGET_SOURCE); // System.out.println("++++++++++++++++++++++"); // System.out.println("souce code : " + source); // if(source.indexOf("();")!=-1){ // String s = new String(source); // Pattern pattern = Pattern.compile("=\\s*\\w*\\(\\)\\;"); // Matcher matcher = pattern.matcher(s); //// System.out.println(matcher.replaceAll("= 5")); // source = matcher.replaceAll(" = 5;\\/\\/need mocked"); // } String s = new String(source); Pattern pattern = Pattern.compile("valueReturned\\s*=\\s*\\w"); Matcher matcher = pattern.matcher(s); // System.out.println(matcher.replaceAll(s +"\\/\\/need mocked")); source = matcher.replaceAll("valueReturned = 8;\\/\\/need mocked"); // System.out.println("++++++++++++++++++++++"); // 输出源码 data.copyList(getSrc()); // 组合输入文件名, 写入临时输出目录 IOUtil.write(getSrc() + data.getSourceFile().getName(), source); setStaticSourceFileName(data.getSourceFile().getName()); // System.out.println("========++++++" + data.getSourceFile().getName()); // 输出信息 warnFile = getSrc() + WARN; String classPath = data.getStr(JobConst.CLASS_PATH); if (classPath.isEmpty()) { classPath = getClassPath(); } // Debug.println(classPath); // 输出make文件 StringBuilder sb = new StringBuilder(); ArrayList<String> arr = data.getCompiledList(); for (int i = 0; i < arr.size(); i++) { sb.append(getSrc() + arr.get(i) + JobConst.ENTER); } Log.println("1111" + getSrc()); // Debug.println(classPath); IOUtil.write(getSrc() + MAKE, sb.toString()); if (!compile(classPath, getBin(), "@" + getSrc() + MAKE, warnFile)) { data.getResult().addReport(IOUtil.read(warnFile)); // data.put(JobConst.COMPILED_REPORT, IOUtil.read(warnFile)); return false; } else return true; }
public void testCustomLoader() { String path = IOUtil.getBasePath() + "Testing\\"; File f = new File(path); try { CustomLoader l = new CustomLoader(new URL[] {f.toURI().toURL()}); Class c = l.loadClass("TestObject"); assertNotNull(c); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); assertNotNull(null); } }