创建新实例时java.lang.IllegalAccessException

时间:2014-12-30 09:53:53

标签: java

我尝试使用动态加载和make实例加载class square。它给了我这个例外:

java.lang.IllegalAccessException: Class model.DynamicLinkage can not access a member of class model.Square with modifiers ""
    at sun.reflect.Reflection.ensureMemberAccess(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)

这是abtract:

public abstract class Shape {

protected Color color;
protected int lowerY;
protected int leftX;
protected int rightX;

public void draw(Graphics g ,int x , int y){

}
public void setColor(Color c){

}
public  Color getColor(){
    return color;

}
public  int getLowerY(){
    return lowerY;

}
public  int getLeftX(){
    return leftX;

}
public  int getrightX(){
    return rightX;

}

}

我要加载的Square类:

public class Square extends Shape{


public Square(){
    System.out.println("deliver");
}

public void setColor(Color c){
    color = c;
}

public Color getColor(){
    return color;
}


public int getLowerY(){
    return lowerY;
}
public void draw(Graphics g ,int x , int y){
    int sideLength = 40;
    lowerY = y + sideLength/2;
    leftX = x - sideLength/2;
    rightX = x + sideLength/2;
    g.setColor(color);
    g.fillRect(x - sideLength/2, y - sideLength/2, sideLength, sideLength);
}

public int getLeftX() {
    return leftX;
}


public int getrightX() {
    return rightX;
}
}

装载程序类:

public class DynamicLinkage {
ClassLoader loader;
Class loadedClass;


public void setClass(String name){
    if(name.equals("Square")){
        loader = LoadSquare.class.getClassLoader();
        LoadSquare s=new LoadSquare(loader);
        loadedClass=s.loadClass();
    }
    else if (name.equals("Circle")){
        loader = LoadCircle.class.getClassLoader();
        LoadCircle c=new LoadCircle(loader);
        loadedClass=c.loadClass();
    }
    else if(name.equals("Plate")){
        loader = LoadPlate.class.getClassLoader();
        LoadPlate p=new LoadPlate(loader);
        loadedClass=p.loadClass();
    }

}
public Class getLoadedClass(){
    return loadedClass;
}
public static void main(String[]args){
    DynamicLinkage d=new DynamicLinkage();
    d.setClass("Square");
    Class l=d.getLoadedClass();
    Color c;
    try {
        Shape s=(Shape) l.newInstance();
        //System.out.println();
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

这是加载类方

的LoadSquare
public class LoadSquare extends ClassLoader{
public LoadSquare(ClassLoader parent) {
    super(parent);
}

public Class loadClass(){

    try {
        String url;

        url = "file:///F:/Square.class";
        URL myUrl = new URL(url);
        URLConnection connection = myUrl.openConnection();
        InputStream input = connection.getInputStream();
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        int data = input.read();

        while (data != -1) {
            buffer.write(data);
            data = input.read();
        }

        input.close();

        byte[] classData = buffer.toByteArray();

        try {
            return defineClass(null, classData, 0, classData.length);

        } catch (Error e) {
            e.printStackTrace();
            return null;
        }

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;

}

}

0 个答案:

没有答案