我正在尝试使用特定的URL加载一个类,并通过向它们传递相同的值来调用该类中的方法。
不完全确定如何做到这一点。
我的网址看起来像这样:"文件:/ C:/ Users / Retro / Desktop / best-project-2 / mutants / traditional_mutants / Complex_cconvolve(Complex,Complex)/AOIS_136/FFT.class" ;
我试图像这样加载类
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
主要目标是加载具有相同名称和方法的多个类。
感谢您的帮助。
答案 0 :(得分:1)
我不明白你的观点。为什么不编译此类文件的jar文件并将其作为依赖项包含在项目中(如see here)?对这种情况使用类加载器和反射是缓慢且不安全的。
答案 1 :(得分:0)
FTT.class我也有扩展名为java的同一个文件
public class FFT {
public FFT() {
}
public static Complex[] fft(Complex[] var0) {
int var1 = var0.length;
if (var1 == 1) {
return new Complex[]{var0[0]};
} else if (var1 % 2 != 0) {
throw new RuntimeException("N is not a power of 2");
} else {
Complex[] var2 = new Complex[var1 / 2];
for(int var3 = 0; var3 < var1 / 2; ++var3) {
var2[var3] = var0[2 * var3];
}
Complex[] var11 = fft(var2);
Complex[] var4 = var2;
for(int var5 = 0; var5 < var1 / 2; ++var5) {
var4[var5] = var0[2 * var5 + 1];
}
Complex[] var12 = fft(var4);
Complex[] var6 = new Complex[var1];
for(int var7 = 0; var7 < var1 / 2; ++var7) {
double var8 = (double)(-2 * var7) * 3.141592653589793D / (double)var1;
Complex var10 = new Complex(Math.cos(var8), Math.sin(var8));
var6[var7] = var11[var7].plus(var10.times(var12[var7]));
var6[var7 + var1 / 2] = var11[var7].minus(var10.times(var12[var7]));
}
return var6;
}
}
public static Complex[] ifft(Complex[] var0) {
int var1 = var0.length;
Complex[] var2 = new Complex[var1];
int var3;
for(var3 = 0; var3 < var1; ++var3) {
var2[var3] = var0[var3].conjugate();
}
var2 = fft(var2);
for(var3 = 0; var3 < var1; ++var3) {
var2[var3] = var2[var3].conjugate();
}
for(var3 = 0; var3 < var1; ++var3) {
var2[var3] = var2[var3].times(1.0D / (double)var1);
}
return var2;
}
public static Complex[] cconvolve(Complex[] var0, Complex[] var1) {
if (var0.length != var1.length) {
throw new RuntimeException("Dimensions don't agree");
} else {
int var2 = var0.length;
Complex[] var3 = fft(var0);
Complex[] var4 = fft(var1);
Complex[] var5 = new Complex[var2];
for(int var6 = 0; var6 < var2; ++var6) {
var5[var6] = var3[var6].times(var4[var6]);
}
return ifft(var5);
}
}
public static Complex[] convolve(Complex[] var0, Complex[] var1) {
Complex var2 = new Complex(0.0D, 0.0D);
Complex[] var3 = new Complex[2 * var0.length];
int var4;
for(var4 = 0; var4 < var0.length; ++var4) {
var3[var4--] = var0[var4];
}
for(var4 = var0.length; var4 < 2 * var0.length; ++var4) {
var3[var4] = var2;
}
Complex[] var6 = new Complex[2 * var1.length];
int var5;
for(var5 = 0; var5 < var1.length; ++var5) {
var6[var5] = var1[var5];
}
for(var5 = var1.length; var5 < 2 * var1.length; ++var5) {
var6[var5] = var2;
}
return cconvolve(var3, var6);
}
public static void main(String[] var0) {
int var1 = Integer.parseInt(var0[0]);
Complex[] var2 = new Complex[var1];
int var3;
for(var3 = 0; var3 < var1; ++var3) {
var2[var3] = new Complex((double)var3, 0.0D);
var2[var3] = new Complex(-2.0D * Math.random() + 1.0D, 0.0D);
}
System.out.println("x");
System.out.println("-------------------");
for(var3 = 0; var3 < var1; ++var3) {
System.out.println(var2[var3]);
}
System.out.println();
Complex[] var8 = fft(var2);
System.out.println("y = fft(x)");
System.out.println("-------------------");
for(int var4 = 0; var4 < var1; ++var4) {
System.out.println(var8[var4]);
}
System.out.println();
Complex[] var9 = ifft(var8);
System.out.println("z = ifft(y)");
System.out.println("-------------------");
for(int var5 = 0; var5 < var1; ++var5) {
System.out.println(var9[var5]);
}
System.out.println();
Complex[] var10 = cconvolve(var2, var2);
System.out.println("c = cconvolve(x, x)");
System.out.println("-------------------");
for(int var6 = 0; var6 < var1; ++var6) {
System.out.println(var10[var6]);
}
System.out.println();
Complex[] var11 = convolve(var2, var2);
System.out.println("d = convolve(x, x)");
System.out.println("-------------------");
for(int var7 = 0; var7 < var11.length; ++var7) {
System.out.println(var11[var7]);
}
System.out.println();
}
}
CallMutant.java //
完整链接
list.get(5)= C:\ Users \ Retro \ Desktop \ best-project-2 \ mutants \ traditional_mutants \ Complex_cconvolve(Complex,Complex)\ AOIS_136 \ FFT.class package com.company.fileIterator;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;
public class CallMutant {
public static void main(String[] args) throws IOException {
FolderIterator folderTestRunner = new FolderIterator();
List<String> list = folderTestRunner.getFilesPath();
URL url;
try{
url = new URL("file:///"+list.get(5));
URLClassLoader ucl = new URLClassLoader(new URL[]{url});
Class clazz = ucl.loadClass("FFT");
Object o = clazz.newInstance();
System.out.println(o.toString());
} catch (MalformedURLException e){
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
}
答案 2 :(得分:0)
好的,所以我最终使用了反射。我创建了一个临时文件,复制了原始FFT类中的所有代码,更改了类名与文件名相同,最后编译了新文件并调用了必要的方法。
这就完成了工作。
public class CallMutant {
public static void main(String[] args) throws IOException {
// get mutant list
List<String> list = FolderIterator.getFilesPath();
for (int i = 0; i < list.size(); i++) {
System.out.println("Iter: "+ i);
File source = new File(list.get(i));
File dest = File.createTempFile("FFT", ".java");
dest.deleteOnExit();
System.out.format("Canonical filename: %s\n", dest.getCanonicalFile());
//copy file using Stream
try {
copyFileUsingStream(source, dest);
} catch (IOException e) {
e.printStackTrace();
}
// generate the source code, using the source filename as the class name
String classname = dest.getName().split("\\.")[0];
Path path = Paths.get(String.valueOf(dest));
Charset charset = StandardCharsets.UTF_8;
String content = new String(Files.readAllBytes(path), charset);
content = content.replaceAll("public class FFT", "public class " + classname);
Files.write(path, content.getBytes(charset));
// compile the source file
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
File parentDirectory = dest.getParentFile();
fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(parentDirectory));
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(dest));
compiler.getTask(null, fileManager, null, null, null, compilationUnits).call();
fileManager.close();
// load the compiled class
URLClassLoader classLoader = URLClassLoader.newInstance(new URL[]{parentDirectory.toURI().toURL()});
Class<?> helloClass = null;
try {
helloClass = classLoader.loadClass(classname);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("No class found");
}
// call a method on the loaded class
Method helloMethod = null;
try {
Complex[] params2 = new Complex[1];
helloMethod = helloClass.getDeclaredMethod("main", String[].class);
} catch (NoSuchMethodException e) {
System.out.println("No Method found");
e.printStackTrace();
}
try {
String[] params = {"1"};
helloMethod.invoke(helloClass.newInstance(), (Object) params);
}catch (IllegalAccessException | InvocationTargetException | InstantiationException e) {
e.printStackTrace();
}
}
}
private static void copyFileUsingStream(File source, File dest) throws IOException {
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(source);
os = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
os.write("import com.company.originals.Complex;\n".getBytes());
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
} finally {
is.close();
os.close();
}
}
}