开发者

Java Reflection Problem

开发者 https://www.devze.com 2023-02-12 16:50 出处:网络
Hi I am currently doing my final year project; I need to develop an algorithm visualization tool. I need to cater for user-defined algo; that is animate the algorithm the user types in a text-editor p

Hi I am currently doing my final year project; I need to develop an algorithm visualization tool. I need to cater for user-defined algo; that is animate the algorithm the user types in a text-editor provided in my tool.

I am using the Java Compiler API to compile the code that the user has typed and saved. My tool offers a set of classes that the user can use in his/her algo.

For example:

myArray(this class is provided by my tool)

import java.awt.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.accessibility.AccessibleContext;
import javax.swing.*;
public class myArray extends JComponent {

int size = 0;
int count = 0;
int[]hold;
Thread th;



public myArray(int[]arr)//pass user array as parameter
{
    //th = new Thread();

    size=arr.length;
    hold = arr;//make a copy of the array so as to use later in swap operation

}

public int length()
{
        return hold.length;
}


public void setAccessibleContext(AccessibleContext accessibleContext) {
    this.accessibleContext = accessibleContext;
}


public void paintComponent(Graphics g)
{
    super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            this.setPreferredSize(new Dimension(360,100));
    for(int i=1; i<=size; i++)
    {
        g2d.drawRect((i*30), 30, 30, 50);
    }

    for(int i=1; i<=size; i++)
    {
        g2d.drawString(Integer.toString(hold[i-1]), (i*30)+15, 30+25);
    }
}






public void set(int i, int j)//position of the two elements to swap in the array
{
    try {
        th.sleep(2000);//sleep before swapping because else user won't see original array since it would swap and then sleep
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    int temp = hold[i];
    hold[i] = hold[j];
    hold[j] = temp;
            hold[i]=j;
    this.repaint();//can use eapint with a class that extends JPanel
}

    public void swap(int i, int j)//position of the two elements to swap in the array
{
    try {
        th.sleep(2000);//sleep before swapping because else user won't see original array since it would swap and then sleep
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    int temp = hold[i];
    hold[i] = hold[j];
    hold[j] = temp;
            this.repaint();//can use eapint with a class that extends JPanel
}


public int get(int pos)
{
    return hold[pos];

}

}

This is a portion of my GUI that will cause the compilation:

    JavaCompiler jc = null;
    StandardJavaFileManager sjfm = null;
    File javaFile = null;
    String[] options = null;
    File outputDir = null;
    URL[] urls = null;
    URLClassLoader ucl = null;
    Class clazz = null;
    Method method = null;
    Object object = null;



       try
        {
         jc = ToolProvider.getSystemJavaCompiler();
         sjfm = jc.getStandardFileManager(null, null, null);
         File[] files = new File[1];
         //files[0] = new File("C:/Users/user/Documents/NetBeansProjects/My_Final_Year_Project/myArray.java");
         //files[1] = new File("C:/Users/user/Documents/NetBeansProjects/My_Final_Year_Project/Tool.java");
         files[0] = new File("C:/Users/user/Documents/NetBeansProjects/My_Final_Year_Project/userDefined.java");
        // getJavaFileObjects’ param is a vararg
        Iterable fileObjects = sjfm.getJa开发者_StackOverflowvaFileObjects(files);
        jc.getTask(null, sjfm, null, null, null, fileObjects).call();
        // Add more compilation tasks
        sjfm.close();
        options = new String[]{"-d", "C:/Users/user/Documents/NetBeansProjects/My_Final_Year_Project"};
        jc.getTask(null, sjfm, null, Arrays.asList(options), null, fileObjects).call();

        outputDir = new File("C:/Users/user/Documents/NetBeansProjects/My_Final_Year_Project");
        urls = new URL[]{outputDir.toURL()};
        ucl = new URLClassLoader(urls);
        clazz = ucl.loadClass("userDefined");


        method = clazz.getMethod("user", null);
        object = clazz.newInstance();
        Object ob = method.invoke(object, null);


        }

This is an example of a user-defined algo(userDefined.java):

import java.awt.*;
import javax.swing.*;


public class userDefined
{
public void user()
{
    int [] numArr  = {1,3,1,-1,5,-5,0,7,12,-36};
    myArray myArray = new myArray(numArr);

    JFrame frame = new JFrame("Rectangles");

    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setSize(360, 300);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
        frame.add(myArray);

    for (int i=myArray.length(); i>1; i--)
    {
        for (int j=0; j<i-1; j++)
        {
            if (myArray.get(j) > myArray.get(j+1))
            {
                myArray.swap(j, j+1);
            }
        }
    }

}

}

The problem I am getting is that if I try to use reflection like above; I only get a white window which does not show the animation) but just displays the result at the very end.

However if I use this instead of reflection(and change the method void user() to static void main(string args) in userDefined.java):

        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        if(compiler.run(null, null, null, "userDefined.java") != 0) {
        System.err.println("Could not compile.");
        System.exit(0);
        }
        try {
        Runtime rt = Runtime.getRuntime();
        Process pr = rt.exec("java "+"userDefined");
        BufferedReader input = new BufferedReader(new              InputStreamReader(pr.getInputStream()));
        String line=null;
        while((line=input.readLine()) != null) {
        System.out.println(line);
        }
        } catch(Exception e) {
        System.out.println(e.toString());
        e.printStackTrace();

it woks provided that after first compilation I place the myArray class in the same folder as the userDefined.java. In this case I can see the animation take place correctly.

How do I use reflection to invoke the main method instead of using an instance of the class. Please I really need some help with this. Thanks!


You a violating / missusing the first rule of swing: acces swing components only in the EDT (Event Dispatch Thread).

When you start your program using the main method, you are violating that rule. This happens to work, but might have all kinds of weird effects. This is not a theoretic warning, it happend to me and it is not nice.

When you run it using reflection from your code, you are most likely in the EDT, so your algorithm runs completely before the GUI gets updated again (which also happens on the EDT). Thats why you see only the final result of the algorithm.

The correct way to do this would be: Run the algorithm in a seperate thread and make sure all changes to your myArray Component happen in the EDT, using SwingUtilities.invokeAndWait or SwingUtilities.invokeLater

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号