I have a special problem in coding a dynamic menu in Java.
What I want:
I want a menubar that looks for special files (example: addon01_men.class) and adds the content of this file to the menu as a new item.
Problem:
To invoke the methode/class in the file, I've to invoke it in the main file. But the addon-class is needed in this case, because java checks the presence of the methode/class. So the program will not start, if "addon01_men.class" is missing.
My solutions didn't work (perhaps in my fault):
- Reflections
- Override
I don't want to use dynamic classes because it's necessary to compile when the program starts - because not all PCs that can run java, can compile java.
I'd be pleased when someone has a working idea.
Thank you.
You should start simple like this example using Reflections:
http://www.mkyong.com/java/how-to-use-reflection-to-call-java-method-at-runtime/
For want you want, you need look for a directory containing the class files like addon01_men.class (think like they're plugins), load them and create instances.
Related
I am currently working on an open source project. I am new in this field. My work is adding a new feature to the currently existing codebase.
However, I do not know where to add my code in the project. The projects contains hundreds of files, classes, and thousands of methods. It is not literally possible to go through each and every line of code.
Eg: Suppose I am working on a project called Calculator. I have the whole source code. Now I type 2 + 3 and hit enter. I get the result 5 which is displayed. But, I need to find where this exact calculation takes place in the source code. For instance, if a method is defined as :
public int add(int a, int b) {
return (a+b);
}
So I need to find this method, whose name is not known to me.
I am currently using the Eclipse IDE.
Can you help me? Or am I approaching this completely the wrong way? If so, how should I go about contributing to open source projects?
I'm not sure if this is exactly what you are looking for but you can access a thread's stackTrace that contains the information about the currently called methods. Therefore you could watch this for getting to know which methods are called.
See this question for how to access the stackTrace.
To access the running code to implement some sort of log mechanism you could use instrumentation to get your code into the running program if you don't have direct access to the code.
This logging method might also be worth a look
I have a problem, and don't know where start. I want to drop files (only .zip) from any place in windows into a Swing application (into a JList). How can I do this?
In the list I will display only absolute path, and file may be in array or something like that. Java 1.6
The keyword you are missing is TransferSupport
https://docs.oracle.com/javase/tutorial/uiswing/dnd/transfersupport.html
https://docs.oracle.com/javase/tutorial/uiswing/dnd/dropmodes.html
But basically you have to make a TransferHandler which has methods to import data via drag and drop. But you have to transform the data into whatever you want (in your case take the File (probably as a DataFlavor#javaFileListFlavor) and convert them to paths
https://docs.oracle.com/javase/tutorial/uiswing/dnd/dropmodedemo.html
One could try to implement that oneself, but as the comment from Hovercraft Full Of Eels mentions there are many caveats to take care of.
I personally use the code I found here: http://iharder.sourceforge.net/current/java/filedrop/
It is a simple java class FileDrop which deals with all the special cases that can occur, and provides an easy interface for dealing with dropped files. The class is public domain and thus free to use for any purpose.
After a few time enjoying the help of this site, the time to integrate myself here has come!
Well, I'm starting a personal project using java (under Windows 7), and I'm getting started with SWT. After a search of hours, I'm not satisfied with the short information I reached.
The project is an application where I will have a main window, from where the user can access to different modules (Customers Management, products management...).
What I want is to set properly:
A) I18n languagues
B) User preferences
"Properly" means a good,proper and easy access from all the components of the program to that data to use it, having in mind to do it through the most "standardized" way too.
I already created a package called "LanguagueResources", where I have the MessageBundle_xx_XX.properties, and I defined the following attributes on my MainMenu Class
protected String languague="en";
protected String country="UK";
protected Locale currentLocale=new Locale(languague, country);
protected ResourceBundle
messages=ResourceBundle.getBundle("MessageBundle",currentLocale);
With this, my main menu works fine with the different languagues. But what happens when I open a new window? maybe I can declare it this again (too much repeated code), or maybe I can pass some data when I call the new window instance (this doesn't look stylish). Same with other possible preferences settings.
This also makes me wonder how I must construct the program structre.I mean, is correct to start with a SWT Application window (Main Menu), and from there, call other SWT Application windows which will be the different modules (Customers, products)? Maybe must I establish an independent Main class where I will call MainMenu class, and where I will define the languague resources and preferences resources?
Also, I would like to know if the user preferences must be saved like the languages (.properties file)
I think I can do it trough many ways, but I would like to know which is the recommended,standarized and easiest way to do it.
I hope I explained well.Thanks in advance!
I have a multilingual web application that gets all of the translations from a single object, for example lang.getTranslation("Login") and the object is responsible for finding the translation in an xml file in the user's language.
What I'd like to do is a script / custom static analysis that outputs all the missing translations and translations that are no more used in the application. I believe I should start by programmatically finding every call to the getTranslation method and the string parameter, storing the data in a special structure and comparing it to all the translation files.
Is there a library that will allow me to do this easily? I already found Javassist but I can't use it to read the parameter values. I also tried grepping, but I'm not sure if that's a robust solution (in case there will be a call to another class that has a getTranslation method). I know Qt has a similar mechanism for finding translatable strings in the code, but that's a totally different technology..
I'm asking this because I'm quite sure there's a good existing solution for this and I don't want to reinvent the wheel.
Ok, here's how I did it. Not the optimal solution, but works for me. I created a utility program in Java to find all the method calls and compare the parameters to existing translations.
Find all classes in my project's root package using the Reflections library
Find all getTranslation method calls of the correct class in the classes using the Javassist library and create a list of them (contains: package, class, row number)
Read the appropriate .java files in the project directory from the given row until the ';' character
Extract the parameter value and add it to a list
Find the missing translations and output them
Find the redundant translations and output them
It took me a while to do this, but at least I now have a reusable utility to keep the translation files up to date.
I would like to create a functionality ( for myself ), wherein on clicking a button ( or say firing any event or anything that can trigger my program ), a popup will be displayed which will ask the name of the Class, objects it have and few more thing. Then on pressing OK, it will create a java file with skeleton of predefined methods, inherit known interface and ...
So, basically how to do that? Do i need to create a plugin for eclipse or there is something else in eclipse for it.
PS Please change the title. I am unable to think of any better one.
As others said, you want to create a wizard, then you want to augment the New Class Wizard, which is doing something similar to what you want (but the default wizard don't allow you to to add fields and custom methods).
To create a wizard, you can use the "New File Wizard" extension template: Create a plug-in, then, go to the extensions tab, select Add..., and select the "Extension Wizards" tab. That will get you started on Eclipse wizards.
Once you've learned the basics of creating Wizards and pages, then, include the org.eclipse.jdt.ui and org.eclipse.jdt.core in your plug-in dependencies. Open the following type (Ctrl-Shift-T): "NewClassWizardPage". This is the page that is displayed when you select New > Class in the Package Explorer.
You can probably either copy this page and the parent pages to help you get started or just extend it (in my experience, internal Eclipse wizards such as this one are difficult to extend because they have lots of fields and methods that are package/private, so I usually end up copying the code as a starting point... don't forget to keep the license though!).
You more or less want to add your own wizzard to the 'new class' dialog .. right?
This was the first site I found when typing "creating your own new wizzard eclipse" in Google: http://www.eclipse.org/articles/article.php?file=Article-JFaceWizards/index.html
I may be mis-understanding the question, but it sounds like you are re-implementing the New Class Wizard that exists already.
It lets you name the class, the containing package. Can assign a superclass and/or interface and can also choose if you want to include the contructors for the superclass.
A new .java file is created with all known methods from declared interfaces and also any abstract methods from the superclass.
Edt: Title was changed whilst I was writing this reply to "How to creat a customer 'new class wizard; for Eclipse". It makes my answer slightly redundant but I'm not seeing any new functionality being added in the question.