i was wondering if their is any way to dynamically create a static field for a class during run-time using reflection or a related API. If needed i can use the java native interface to accomplish this task if someone could tell me the appropriate steps. I don't want to use any data structures such as a hash-map, as i am doing this completely for fun. Please don't suggest using maps as i am not using this for any real program this is a completely theoretical situation. Thanks.
You could do this during class load time using bytecode manipulation.
This is a very complex solution though, so I'd consider other options.
It also does not help too much to have a new field that is not known at compile-time, because you cannot compile anything against it. If you are going to use reflection to access it, you might as well use a Map in the first place.
Java doesn't support metaprogramming or runtime programming in a way that is particularly nice or effective.
You could use a decorator pattern. You could pass the object that you want to add a static field to into a wrapper object. This wrapper would have the static field and the calls to the wrapper object would relate to the wrapped object.
If you could provide more details about the functionality you're looking for I could try to provide a better solution. You might be better off looking into another language that does support runtime programming if you absolutely need it to be done in that way.
Related
Is it safe to use the javafx.util.Callback on a server machine? As I understand, and I may be wrong, not all JavaFX components are available on a server installation where graphics arent available.
Should I create my own callback interface, or just use one from javafx?
At first, I didn’t like using it either, because this interface was losing quality in my code. I no longer had self-explaining interface names. But in the end, I realized that the advantages overweigh the lack of quality. The advantages being:
We end up writing less code. No specialized interface, no default implementations.
The developer using the API does not have to remember different factories, instead, he can focus on the object that he wants to create and the parameters that are available to him.
The Callback interface is a functional interface. We can use Lambda expressions, which makes the code more elegant and we once again have to write less code.
Therefore I would suggest using javafx.util.Callback instead of custom one
I'm just starting to learn and the books say that I can use "pre-coded" classes and objects. How do I call these classes/objects in the beginning of my program? I understand basically what they are, and the idea that I can use these classes/objects in place of writing fresh code every time, but I cannot seem to figure out where I find these things and how I implement them.
You certainly talk about the Java classes that come in JRE/JDK.
Those are used by including the jar in your classpath and provides the "default" java classes.
Like String in java.util package.
If you want to look at them, in the JDK you'll find the sources of these class.
"Pre-coded", or pre-written Java classes, are pretty much the same concept as the Java API - someone has written the code for you, was kind enough to document how you can use the code, and you may create instances (as necessary) through the prescribed way.
Say, for instance, I want an ArrayList holding Strings. I would then code ArrayList<String> words = new ArrayList<String>(). You wouldn't have to go through the process of writing a dynamic self-expanding vector.
Reflection or hashmaps? Suppose you are writting a graphical application where you have objects lying around and users have the posibility of clicking on them. That said, you want to display the clicked object information somehow.
I end up reading Accessible interface from accessibility java package and AccessibleContext class... Before that, I wrote testing code using hash maps for properties instead of class fields. And lastly, I thought in Reflection after reading that JavaBeans use instrospection.
What would you recommend in such applications?
Using reflection and introspection sounds like a better approach than using HashMaps. Instead of using this API directly, I would recommend an abstraction layer like Commons BeanUtils, which will allow you to work with classes adhering to the JavaBean standard in an easy manner.
Here's the User Guide complete with examples.
I'm developing an Eclipse RCP application. I have been told that the UI part will be created by a designer (?) with Window Builder PRO so I have to create the business logic for every view our designer creates.
My first idea was to create a Controller class for each view and handle the application logic there with a little help from Reflection (I don't know what kind of Widgets the designer will create). I thought that this will be dynamic hence rejoicing but our leader told me that we won't use Reflection.
I heard a very short explanation which did not make any sense for me so my question is:
Why using Reflection considered bad / non-maintainable?
Java is a strongly typed language, with pros and cons. For all the cons, it gives you very good compile-time checking. Bugs/errors found at compile time are the cheapest to find and fix.
Reflection is a runtime thing. You can't have the compiler check anything you do using reflection, therefore if you use reflection you lose a lot of the pros of java. Specifically, you can write code that compiles but explodes at runtime. The term sometimes used for reflective type code is "stringly typed" - you are using the names for methods (Strings) rather than referring to actual methods.
Reflection is used of course, but whenever you use it, remember that you are subverting the strength of java. To use it extensively as in your case might actually be OK, as long as your units tests cover enough cases to give you confidence that your use of it is safe. I would sanction its use if the code that actually used it was a fairly small "utility" base of code.
Is it possible to add "hooks" to a class via bytecode enhancement that execute code whenever a class field is read or written? For example, I'd like to automatically set a "dirty" flag whenever a new value is assigned to a field.
If so, which libraries are best suited to implement this functionality?
I'd suggest you to read about AspectJ. Probably this is the tool you are looking for.
Here is how to generate getters and setters using the ASM framework. That should get you started.
http://asm.ow2.org/doc/faq.html#Q9
You can even let your bytecode-rewriter hook into the class-loader and do the rewriting on the fly.