java - Best way to display object properties - java

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.

Related

Why do they use interface in this guide

So I am pretty new in Java and I'm trying to get started with web applications using JSP and servlets. I've came across this CRUD web app guide A simple CRUD Tutorial Using Java Servlet / JSP. The thing is, I don't understand why they have to create the StudentDAO interface. I know this would be easy as pie to understand for most of you that's why I'm asking here. All I want is an answer if StudentDAO interface is really needed since we only declare the methods there, and override all of them in a class called studentDAOImplementation anyway.
I know I should read some more about Java interfaces but I was hoping to get explanation on why interface is needed in this example.
This does not really have a simple answer. The sketch of the answer would be - because you want to be independent of how your application actually stores/retrieves the data in the database. The interface provides the functional specification of what the DAO (Data Access Object) should be able to do - it is up to the specific implementation to actually do it. For example, for testing purposes, you might want to setup a stub DAO that does not really use the database, but instead gives you prefabricated objects. In a real-world complex application, you might want to vary the DAO depending on what database engine is really used and so on and so on. So generally, this is an instance of decoupling the functional specification from the implementation.

Creating fields dynamically java

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.

Best Practice (Design Pattern) for copying and augmenting Objects

I'm using an API providing access to a special server environment. This API has a wide range of Data objects you can retrieve from it. For Example APICar
Now I'd like to have "my own" data object (MyCar) containing all information of that data object but i'd like to either leave out some properties, augment it, or simply rename some of them.
This is because i need those data objects in a JSON driven client application. So when someone changes the API mentioned above and changes names of properties my client application will break immediatly.
My question is:
Is there a best practice or a design pattern to copy objects like this? Like when you have one Object and want to transfer it into another object of another class? I've seen something like that in eclipse called "AdapterFactory" and was wondering if it's wide used thing.
To make it more clear: I have ObjectA and i need ObjectB. ObjectA comes from the API and its class can change frequently. I need a method or an Object or a Class somewhere which is capable of turning an ObjectA into ObjectB.
I think you are looking for Design Pattern Adapter
It's really just wrapping an instance of class A in an instance of class B, to provide a different way of using it / different type.
"I think" because you mention copying issues, so it may not be as much a class/type thing as a persistence / transmission thing.
Depending on your situation you may also be interested in dynamic proxying, but that's a Java feature.

Where to put business logic in Eclipse RCP program

I'm writing a small application in RCP to wrap around the business logic in another (non-RCP) simulation library. I can access and use the library fine from any of my plugins, but I don't know where I should put the instance of the Simulation library so that, say, one of the command handlers can make calls to it.
From reading the docs it sounds like I should be storing 'global' information like this in the workbench - but I still don't really understand how to do that.
Help?
First, the business layer (BL) can and should reside in its' own plugin. That will provide decent decoupling between the layers.
Second, you should carefully decide what the interface should be and which classes are exposed. Ideally, you should mostly expose interfaces and data objects.
Finally, decide how the "hand shake" works. E.g., how to obtain the initial interface to the BL. Since it is a Plugin, it could have an Activator which loads it. You could add a method in the activator which returns the BL interface.
If you are looking for something more decoupled, you could create an extension point or deploy the BL as an OSGi service, but that's a bit of an overkill for you need.
If I understand you correctly, I see two ways:
Store the instance in the model plug-in itself, using ‘SimulationFactory.getInstance(String myAppId)‘. The passed String is a constant in you app that is always used, when obtaining the reference.
Define a new class e.g. GlobalAccess in you app that is initilized with an instance of your model and has some getter (whether you use a single instance again or only provide public static methods is a matter of taste).
The seocond way is similar to some classes in eclipse like platfom or platformui, where you can obtain initial references and navigate through the workbench.
edit
i just found a tutorial that might help you:
Passing Data between Plug-ins

Serialize Java objects into Java code

Does somebody know a Java library which serializes a Java object hierarchy into Java code which generates this object hierarchy? Like Object/XML serialization, only that the output format is not binary/XML but Java code.
Serialised data represents the internal data of objects. There isn't enough information to work out what methods you would need to call on the objects to reproduce the internal state.
There are two obvious approaches:
Encode the serialised data in a literal String and deserialise that.
Use java.beans XML persistence, which should be easy enough to process with your favourite XML->Java source technique.
I am not aware of any libraries that will do this out of the box but you should be able to take one of the many object to XML serialisation libraries and customise the backend code to generate Java. Would probably not be much code.
For example a quick google turned up XStream. I've never used it but is seems to support multiple backends other than XML - e.g. JSON. You can implement your own writer and just write out the Java code needed to recreate the hierarchy.
I'm sure you could do the same with other libraries, in particular if you can hook into a SAX event stream.
See:
HierarchicalStreamWriter
Great question. I was thinking about serializing objects into java code to make testing easier. The use case would be to load some data into a db, then generate the code creating an object and later use this code in test methods to initialize data without the need to access the DB.
It is somehow true that the object state doesn't contain enough info to know how it's been created and transformed, however, for simple java beans there is no reason why this shouldn't be possible.
Do you feel like writing a small library for this purpose? I'll start coding soon!
XStream is a serialization library I used for serialization to XML. It should be possible and rather easy to extend it so that it writes Java code.

Categories