public class Untitled1 { private final DirectLoader classLoader = new DirectLoader();
private static class DirectLoader extends ClassLoader { private DirectLoader() { super(ClassWork.class.getClassLoader()); }
private Class load(String name , byte[] data) { return super.defineClass(name , data , 0 , data.length); } }
public Class loadClass(String className) { try { byte[] bytes = getBytes("classes/demo/WFProcess.class"); System.out.println("size=" + bytes.length); return classLoader.load(className , bytes); } catch(Exception ex) { throw new RuntimeException(ex); } }
private byte[] getBytes(String filename) throws IOException { File file = new File(filename); long len = file.length();
byte raw[] = new byte[(int)len];
FileInputStream fin = new FileInputStream(file);
int r = fin.read(raw); if(r != len) { throw new IOException("Can't read all, " + r + " != " + len); }
fin.close(); return raw; }
public static void main(String[] args) { try { Untitled1 u = new Untitled1(); Class clas = u.loadClass("demo.WFProcess"); Object o = clas.newInstance(); System.out.println("class=" + o.getClass()); if(o instanceof WFProcess) { System.out.println("WFProcess"); } if(o instanceof Cloneable) { System.out.println("Cloneable"); } System.out.println("OK"); } catch(Exception ex) { ex.printStackTrace(); }
}
}
public final class WFProcess implements Cloneable { private int id = -1;
private String name;
private String description;
private String attributes;
private boolean valid;
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getDescription() { return description; }
public void setDescription(String description) { this.description = description; }
public String getAttributes() { return attributes; }
public void setAttributes(String attributes) { this.attributes = attributes; }
public boolean getValid() { return valid; }
public void setValid(boolean valid) { this.valid = valid; }
public void test() { this.setId(2); this.setAttributes("cc"); Object o=new Object(); }
}
|
大家的运行结果是什么?为什么只打印出 Cloneable