0

I am new in the forum and pretty new about Java coding.

Anyway I am implementing my Java code to dynamically compile and run different classes which are not know a priori and which could change over time (not too frequently). I fount very useful the example proposed here based on javax.tools but, since my work should run on real-time later, I would like to avoid as much as possible to use Java reflection. Do you know if exists a way to run the compiled code without use reflation? There are some variable that I can retrieve after the compilation which points to the class and then use it to instantiate the class?

thanks is advance Luca

Puce
  • 37,247
  • 13
  • 80
  • 152
user2497897
  • 45
  • 2
  • 6
  • Java reflection is very powerfull ... libraries and frameworks exploit for the abilities that it can give, its not that slow or unreliable if used correctly. to provide an alternative ?? i don't know ... it depends on how dynamic do you want the code and other requirements and restrictions – DarthCoder Jun 18 '13 at 16:34
  • basically the program should be used to acquire and process data from sensors in general purposes. The compilation shouldn't be too frequently but the processing and acquisition maybe could have high frequency. But I am not an expert of reflation I just sow the possible problem on the [site](http://docs.oracle.com/javase/tutorial/reflect/) so I do not know exactly its limitation. – user2497897 Jun 18 '13 at 16:45
  • hmmm true ... but why would you use reflection to acquire information from sensors ?? just pre-comiple them separately just load them into your project if so you want to do them dynamically – DarthCoder Jun 18 '13 at 16:47
  • I did not tell it, to avoid unnecessary information, but the code works in combination with Protege and OWL api. Basically I want to use algorithms wrote in Java and controlling the data flow with a semantic application. So the compilation is usually made once (dynamically or pre-compiled, as you told) but the usage of the code is continuous and should be independently from what I am writing to build such framework. This means that I don't know a priori the name of the class or the methods that will be used (they will be defined on the ontology by users). – user2497897 Jun 18 '13 at 17:01
  • if its pre-compiled its ok ... all the connector or driver classes esp ones implemented as JDBC drivers are precompiled classes .. are loaded into the memory using class.forName and they are usually high frequency so assuming that its ok – DarthCoder Jun 18 '13 at 17:12
  • I am sorry but is not very clear for me. Are you meaning that if the class is already compiled (so the file.class is already available at run time) I can use class.forName to get an instance of the class? so practically (in pseudo-code) if a class named "Cl" has a method named "Mh" I can write: Class ins = class.forName( Cl); ins.Mh(); – user2497897 Jun 18 '13 at 17:26
  • precise ... look at the answer bellow ... that would do the trick ..!!! there is a time delay in loading ... but you need that .. hence you can overlook that – DarthCoder Jun 18 '13 at 17:46

1 Answers1

0

Hey so look i dunno if this is right or this is what you want but i would divide the framework as such ...

The interface interface is just for easier understanding.

public interface fun {
void fun();
}

so lets say someone builds the code for your app or framework ... give him your interface and tell him to put functionality in that method .. which would be something like this ..

public class TestDestination implements test.fun {
public void fun(){

    System.out.println("Hello");

}
}

then all you have to do is load this class ... you can get the name from user input xml etc etc... this will be your executer

public class TestLoad {
public static void main(String[] args) {
    try {
        Class t1 = Class.forName("test.temp.TestDestination");
        fun temp = (fun) t1.newInstance();
        temp.fun();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

i am not sure if this is exactly what you want ... but it would be ideal to keep the loader on a separate thread and load all classes that you want in the beginning of your program or lazy load it ... your choice hope this helps i am assuming that class.forName is efficient ... correct me if wrong

DarthCoder
  • 354
  • 1
  • 11
  • ok so call to t1.newInstance is ok look at [example](http://stackoverflow.com/questions/435553/java-reflection-performance) so its ok to use this method ... but you may want to tweak it as per requirement ... but reflection is not scary to use .. in-terms of efficiency if you do it this way ... – DarthCoder Jun 18 '13 at 17:24
  • it took awhile for me to understand it, but it is basically what I was looking for, thanks a lot!!!! I would like to ask you two last further questions: Does it work if I compile dynamically the class TestDestination? There are some way to call also the method by name without increase too much the computational effort? – user2497897 Jun 18 '13 at 19:16
  • see, the problem for that delay is to load the concerned the class into the memory. this cannot be avoided regardless, what you need to understand is that .. when you say `java ` java loads it into the memory then calls main. that delay cannot be avoided right ?? so what you are telling java here to do is apart from loading the essentials load an external class given in that `class.forName("blah")` so to do that it needs find the class on filesystem and load it if you are creating an instance etc. coming to the question other question about dynamic compilation – DarthCoder Jun 18 '13 at 20:00
  • I don't get it .. are you trying to change the class or something ?? and then compile it ?? how often do you see this happen ?? if it needs to be just loaded it is good ... are you looking for something like [ejb](http://en.wikipedia.org/wiki/Enterprise_JavaBeans) or you are lookig for something similar ?? note EJB isn't just for a web-application alone ... if you think you can't handle loading unloading execution properly use the frameworks to do it – DarthCoder Jun 18 '13 at 20:06