I am trying to use SPOON developed by INRIA to retrieve all the methods in a program as well as all the method calls. I am able to do so for normal methods, however, I am not able to retrieve nested methods and I am not able to retrieve nested method calls either.
Here is a fragment of code that I am parsing, In this case, I would like spoon to collect the method run() which is nested without the main, I would also like to retrieve the call from run to the constructor of the class ElbowLiner, could you please give me directions on how to achieve this. I used getAll(true) to retrieve everything including the nested method calls but it did not work, I was not able to retrieve run() in the code fragment below and I was not able to retrieve the method call either from run() to the constructor of ElbowLiner
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// Create the two text areas
TextAreaFigure ta = new TextAreaFigure();
ta.setBounds(new Point2D.Double(10,10),new Point2D.Double(100,100));
TextAreaFigure tb = new TextAreaFigure();
tb.setBounds(new Point2D.Double(210,110),new Point2D.Double(300,200));
// Create an elbow connection
ConnectionFigure cf = new LineConnectionFigure();
cf.setLiner(new ElbowLiner());
// Connect the figures
cf.setStartConnector(ta.findConnector(Geom.center(ta.getBounds()), cf));
cf.setEndConnector(tb.findConnector(Geom.center(tb.getBounds()), cf));
// Add all figures to a drawing
Drawing drawing = new DefaultDrawing();
drawing.add(ta);
drawing.add(tb);
drawing.add(cf);
// Show the drawing
JFrame f = new JFrame("My Drawing");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400,300);
DrawingView view = new DefaultDrawingView();
view.setDrawing(drawing);
f.getContentPane().add(view.getComponent());
f.setVisible(true);
}
});
}
The easiest way in Spoon to retrieve all methods from a model is to use a processor of CtMethod you can try a code like this one:
public class MyProcessForMethods extends AbstractProcessor<CtMethod> {
public void process(CtMethod myMethod) {
System.out.println(mymethod.getSimpleName());
}
}
and to use it:
Launcher launcher = new Launcher();
launcher.addInputResource("/path/to/your/source");
launcher.addProcessor(new MyProcessForMethods());
launcher.run();
The method process() will be called each time a new CtMethod is find in the model: then it will process as well method nested from inner types and normal methods.
Don't hesitate to open an issue on Spoon Github repository and to provide more insight on how you're using Spoon right now.
Related
I decided to split the last part of that question here into a new question here: https://softwareengineering.stackexchange.com/questions/411738/extension-of-classes-where-to-put-behaviour-how-much-direct-access-is-allowe
If i have a lib and i want to use it, i wrote mostly a own class. This class has one method. In that there is the code how to instantiate the lib/framework. Sometimes there are a few more methods, with them i not only instantiate the class but use it. For example if i want to start a http-server i have there a start-method.
class Container
{
TheLib theLib;
public void init() //or a constructor
{
//some init of the theLib
}
public void start() //
{
theLib.doSomething(...)
theLib.doSomethingmore(...);
theLib.start(...);
}
//important!
public TheLib getTheLib()
{
return this.theLib; //after i started configured it and so on, i want of course use all methods,
which the lib have in some other parts in my application
}
}
But it seems not to be the best solution.
Are there any better solutions, that OO is?
Often i also use only one method, a own class for this seems to be here a big overhead?
Exposing the lib breaks encapsulation? Tell-Dont-Ask is also violated?
Everything depend on what you actually need or how you have access to your 'the lib' instance.
public class Container {
private TheLib theLib;
/* #1: Do you already created the instance before? */
public Container(TheLib theLib) {
this.theLib = theLib;
}
/* #2: Do you need to created the instance each time? */
public Container() {
this.theLib = new TheLib();
}
public void start() {
theLib.doSomething(...)
theLib.doSomethingmore(...);
theLib.start(...);
}
public TheLib getTheLib() {
return this.theLib;
}
public static void main(String[] args) {
/* #1 */
TheLib theLib = ...;
Container container = new Container(theLib);
/* #2 */
Container container = new Container();
/* Continue the flow of your program */
container.start();
container.getTheLib().doSomethingEvenMore();
}
}
Or maybe you actually need only one instance of your 'Container' class. In this case, you should look on how to make a singleton: Java Singleton and Synchronization
Anwser: Often i also use only one method, a own class for this seems to be here a big overhead?
Well, in Java, you cannot do formal programming like in C, so everything line of code that you write, or will be using, has to be in a class of some sort.
If your piece of code is small and don't really need an object, static function might do the work.
I am trying to extract the calls from the method run() to the constructors. Here is the code I am trying to parse
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// Create the two text areas
TextAreaFigure ta = new TextAreaFigure();
ta.setBounds(new Point2D.Double(10,10),new Point2D.Double(100,100));
TextAreaFigure tb = new TextAreaFigure();
tb.setBounds(new Point2D.Double(210,110),new Point2D.Double(300,200));
// Create an elbow connection
ConnectionFigure cf = new LineConnectionFigure();
cf.setLiner(new ElbowLiner());
// Connect the figures
cf.setStartConnector(ta.findConnector(Geom.center(ta.getBounds()), cf));
cf.setEndConnector(tb.findConnector(Geom.center(tb.getBounds()), cf));
// Add all figures to a drawing
Drawing drawing = new DefaultDrawing();
drawing.add(ta);
drawing.add(tb);
drawing.add(cf);
// Show the drawing
JFrame f = new JFrame("My Drawing");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400,300);
DrawingView view = new DefaultDrawingView();
view.setDrawing(drawing);
f.getContentPane().add(view.getComponent());
f.setVisible(true);
}
});
}
Here is the code I am using to extract the calls from method run() to the constructors. The problem that I have is that the last line: String constructorClassName= cons.getExecutable().getDeclaringType().toString(); is returning the wrong class name, instead of getting "jhot.draw.TextAreaFigure()" as the name I am getting "jhot.mini.samples.TextAreaFigure()". The file that I am parsing is located under "jhot.mini.samples" while the constructor is declared within "jhot.draw.TextAreaFigure()". I am not sure if this is a bug in spoon or if I am using the wrong API to retrieve the constructor calls.
for(CtMethod<?> method :clazz.getMethods()) {
List<CtConstructorCall> ctNewClasses = method.getElements(new TypeFilter<CtConstructorCall>(CtConstructorCall.class));
for( CtConstructorCall myclass: ctNewClasses) {
//CONSTRUCTOR
if(myclass instanceof CtConstructorCall<?>) {
System.out.println("yes");
List<CtMethod> methoddeclared = myclass.getElements(new TypeFilter<CtMethod>(CtMethod.class));
for(CtMethod<?> meth: methoddeclared) {
methodinside=meth.getSignature();
methodinsideclass=clazz.getQualifiedName();
String mymethod=methodinsideclass+"."+methodinside;
ResultSet methodsinside = st.executeQuery("SELECT methods.* from methods where methods.fullmethod='"+mymethod+"'");
//while(callingmethodsrefined.next()){
if(methodsinside.next()) {
MethodIDINSIDE = methodsinside.getString("id");
CLASSNAMEINSIDE = methodsinside.getString("classname");
CLASSIDINSIDE = methodsinside.getString("classid");
//System.out.println("CALLEE METHOD ID: "+ CALLEEID);
}
List<CtConstructorCall> constructors = meth.getElements(new TypeFilter<CtConstructorCall>(CtConstructorCall.class));
for(CtConstructorCall<?> cons: constructors) {
String constructorClassName= cons.getExecutable().getDeclaringType().toString();
}
}
}
}
I am not sure if this is a bug in spoon or if I am using the wrong API to retrieve the constructor calls.
I'm one of the contributor of Spoon. It looks to me that you're using the right API, but I'm not sure because your example looks a bit messy here.
I think it would be easier if you open an issue on Spoon Github repository and specify:
the project you're working on if it's open-source
how you launch Spoon (the version of Spoon, arguments, etc)
what do you expect exactly
Then we could investigate to check exactly what happens there. Thanks!
In a method of a custom class View:
public class View {
private Timer timer;
...
private double[][] allLevels;
...
I have a method with an abstract call that needs to point to the variable allLevels. The variable is produced by another class GameLogic, but in the Main of the application. In the Main, the return argument from a public method is then passed to the View:
public class Game extends ApplicationAdapter {
View view;
GameLogic gameLogic;
#Override
public void create () {
System.out.println("Creating");
this.gameLogic = new GameLogic();
this.gameLogic.prepareStimulus();
}
#Override
public void render () {
Gdx.gl.glClearColor(0.5f, 0.5f, 0.5f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
this.view = new View();
this.view.presentStimulus(this.gameLogic.allLevels);
}
}
Because of a very complex game/business logic I thought I would try to separate and encapsulate as much as possible in a MVC-ish pattern. The Main uses libgdx, which requires create and render to be separate.
My main problem is that I am unable to reach the variable in the class View from the abstract call to the class scope.
public void presentStimulus(double[][] allLevels){
...
timer = new Timer();
...
timer.scheduleTask(new Task(){
#Override
public void run(){
DO SOMETHING WITH that.allLevels[0][0]
}
}, .....);
I have looked at a similar issue, but I guess my question is more basic.
The IDE is unable to autocomplete the reference to the properties using the keyword "this". How can I make the Run() method access the property of the instance of the outer class?
The problem ( I rather doubt it) is here -
public void presentStimulus(double[][] allLevels){
...
timer = new Timer();
...
timer.scheduleTask(new Task(){
#Override
public void run(){
DO SOMETHING WITH that.allLevels[0][0]
}
}, .....);
You see, in this code time.scheduleTask, you are just only creating a new Task and not executing it. It is executed after a while with another thread I supposed, thus it is run in a different context, which does not have allLevels value at the time of execution. So there is no way you can access that allLevels in that run method unless you use closure. I am not sure whether java supports closure or not, but here is a similar answer that might help you - Closure in Java 7
You can use some other solutions, like save the hash and allLevels in a separate static dictionary that is accessible globally and then pick the value from there.
I am using GWT and would like to call the Popup.Hide() method in the Library class MenuBar but it is private and the Object is private too...
so what is the best way to get the method:
If I create a complete new class which inherits from MenuBar, will not work I have to create 10 other classes which depend on the MenuBar, for example MenuItem, since MenuItems also hold private static variables, which seems too involved.
unzip the .jar and change the method to public.
another way?
there's no Hide() uppercase method in java, did you read the javadoc? hide() is also a lowercase method. Do you use eclipse with the Google plugin?
Button b1 = new Button("Click me to show popup");
b1.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
// Instantiate the popup and show it.
new MyPopup().show();
}
});
http://www.gwtproject.org/javadoc/latest/com/google/gwt/user/client/ui/PopupPanel.html
I am building a user interface in netBeans (coding by hand, more flexible) with multiple toolbars.
What I am trying to do is create an actionListener for each button. I am retrieving names of the functions from XML and parse them to string. I will write implementations for those functions in a separate class, but my problem is the following:
How do I make the link between the function name and the string containing it's name?
Example: String is Open(), function will be Open(someParameter) and in the definitions class there will be static void Open(param).
First of all, consider my comment about your idea of dynamic button behavior resolved from strings being a wrong approach. However if you still need exactly what you asked, what you need is Reflection API.
Here's an example:
Class c = SomeClassWithMethods.class;
Method m = c.getMethod("someMethodName", String.class, Integer.class, Integer.TYPE);
m.invoke(baseObjectFromWhichToCallTheMethod, "stringParam", 10, 5);
Added:
Another option, which is a little bit prettier than reflection, but still a messy design, would be to use a map to link those Strings to methods. The code is a bit longer, but from the Java perspective it is much better than using reflection for your task (unless you have some specific requirement of which I'm not aware). This is how it would work:
//Interface whose instances will bind strings to methods
interface ButtonClickHandler {
void onClick();
}
class SomeClassYouNeed {
//One of the methods that will be bound to "onButtonOneClick()"
public void onButtonOneClick() {
log.info("ButtonOneClick method is called");
}
public void onButtonTwoClick() {
log.info("ButtonTwoClick method is called");
}
//Map that will hold your links
private static Map<String, ButtonClickHandler> buttonActionMap;
//Static constructor to initialize the map
static {
buttonActionMap = new Map<String, ButtonClickHandler>();
buttonActionMap.put("onButtonOneClick()",new ButtonClickHandler() {
#Override
public void onClick() {
onButtonOneClick();
}
});
buttonActionMap.put("onButtonTwoClick()",new ButtonClickHandler() {
#Override
public void onClick() {
onButtonTwoClick();
}
});
}
public void callByName(String methodName) {
final ButtonClickHandler handler = buttonActionMap.get(methodName);
if (handler == null) {
throw new IllegalArgumentException("No handler found by name: "+methodName);
}
handler.onClick();
}
}
After you call callByName("onButtonTwoClick()") it will fetch the respective instance of ButtonClickHandler which will use the static method onButtonTwoClick() to process the click of the button.
It seems to me that you are looking for the equivalent of JS "eval" function in Java. This might help. Nevertheless it is generally not a good idea as #Max stated, you might want to rethink your design.
If i have understood your question correctly you are trying to generate your code files based on some strings taken from a XML file. I can suggest you this library to generate your codes.
For tutorials you can visit this link.
You may even use the Java Reflection API. Here is a link for the tutorial.
Its upto you, that which of the above two you use.