GWT generate class methods - java

In my project I have a series of Models which basically just contain data, and have getters and setters for that data (which has to match an API). I am trying to use GWT generators to generate the getters and setters (because they have some logic in them for setting default values and I don't want to have this typed out all the time.
For example, MyBusinessModel.java:
public class MyBusinessModel extends AbstractBusinessModel {
private Integer uid;
private String name;
//... and so on
}
I then create a public abstract class AbstractBusinessModel which has some implemented base methods. I had created a generator for this, AbstractBusinessModelGenerator extends Generator, which automatically creates all the getters and setters, but I keep getting errors about MyBusinessModel not being able to be a superinterface of MyBusinessModelImpl (the generated class) because it's not an interface.
Is there a way for me to generate classes like this (I can't make MyBusinessModel an interface because I need it to have private properties), or can I only generate interfaces (which become classes)?

The answer is to use setSuperClass on the ClassSourceFileComposerFactory instead of addImplementedInterface. I didn't realise that this existed. Now I do.

Related

How to persist enum const in ormlite as a interface type?

I have one enum, which implements an interface. The purpose of this interface is only to create a bound between multiples enums, so that I can later implements a plugin system. (This post may clear the thing a little bit.)
My library enum lloks like this :
public interface Resource extends Displayable<Resource> {
// all the methods that implements my enum
// Displayable is just an interface that I need in my game
// Just doesn't consider it, it can't interfer
}
And an exemple of an enum that implements this interface :
public enum LibraryEnum implements Resource {
// static final fields
// fields and resources
// all implemented methods
}
The thing is that I want to store this as an interface, to enable a sort of enum inheritance, because I'm about to create a plugin system. Any dev would just have to implements this unique interface to add resources in the game. But I don't know how he (or she) would name it (there might be some doubles).
I have a class with the associated field, as shown here :
#DatabaseTable(tableName = "packs")
public class Pack implements Displayable<Pack> {
#DatabaseField(columnName = "id", generatedId = true)
private Long packId;
// Here it is
#DatabaseField
private Resource resource;
// Is there any annotation arguments to add ?
#DatabaseField
private int quantity;
// Some other fiels
// Then constructors and methods
}
I've read the docs a little bit, and it say to create a custom implementation of the DataPersister interface. So I began to do it, but there is so many methods to implement (20?), that I don't know where to began and where to end. Maybe it's the wrong way to do it?
The docs shows an exemple where are the methods aren't even there, and for the type that already exists in java (Date & DateTime).
How can I achieve this? Or is this even possible? If not, is there anyway to do what I want anyway (store unknown const enum fields in the database)?
I've read the docs a little bit, and it say to create a custom implementation of the DataPersister interface. So I began to do it, but there is so many methods to implement (20?), that I don't know where to began and where to end.
First off you should consider RTFM on custom persisters. I've spent a good bit of time on the ORMLite docs.
The right thing to do is to extend a currently implemented persister from ORMLite. For example, you could extend the BaseEnumType if you are persisting an enum. If none of the persisters work then you should extend the generic BaseDataType.
With BaseDataType, all you need to implement is:
public Object parseDefaultString(FieldType fieldType, String defaultStr);
public Object resultToSqlArg(FieldType fieldType, DatabaseResults results, int columnPos);
Although not required, you probably also want to override:
public Object sqlArgToJava(FieldType fieldType, Object sqlArg, int columnPos)
Typically you'd want to override a couple other methods to tweak the behavior of your custom persister.

How to promise data-structures via Java Interfaces?

Lets say I have an Interface "Car" and a concrete implementation of that Interface called "Campervan". I want every Car to have the following variables:
int seats
int passengers
One might think the Interface could look like this:
interface Car{
private Integer passengers;
public Integer seats;
}
But this does not work for several reasons. The Java Interface does not allow variables other than FINAL and PUBLIC.
Since Java Interfaces do not allow variables other than public final ones, how can I realize the promise that all classes implementing the Interface (such as the Campervan) have an Internal Data-Structure including those variables? Public final variables are not always what I want my Interface to promise.
Note: I do understand the concept of Interfaces not including internal states and therefore forcing variables to be final. But does this mean Java Interfaces do not allow to make promises of internal data structure?
The interface is intended for use by other parts of your program, and the internal data structure is for the implementation of the interface to decide. If you want to reuse some internal data structure in a class hierarchy you should use an abstract class.
The main purpose of interfaces is to separate the abstraction from the implementation. By asking classes that implement the interface to follow some sort of implementation, you are defying the concept of the interface. You define the interface, so that you don't care how it is implemented, as long as the classes that implement it do what they promise to do (you should not be worried about how they do it).
In your example you can have this interface:
interface Car{
public Integer getPassengers();
public Integer getSeats();
}
Having the Car interface, you can call the getPassengers() method anytime you need it, without caring what the class (implementor of the interface) is doing to get you the number of passengers.
Solution 1 - Use an abstract class instead:
public abstract class Car{
private Integer passengers;
private Integer seats;
// setters and getters...
}
This of course will not work if your implementing classes already extends some other class:
Solution 2 - Add getters and setters to the interface:
interface Car{
Integer getPassengers();
Integer getSeats();
void setPassengers(Integer value);
void setSeats(Integer value);
}
In addition, you can't mark something as private in an interface. It just makes no sense. The purpose of an interface is making client code able to access members without knowing what object and what implementation it is using. Thus, private members are quite useless in interfaces because client code can't access it any way.

Convert POJO to JavaFX Property

In my project, I have a number of POJOs whose fields are shown in a JavaFX GUI. I currently update the UI using a manual refresh – whenever the POJO changes, I call a refresh() method. I would like to attempt using binding to accomplish this instead, which requires using JavaFX properties. Whilst I can change the object, its internal fields are other objects, which I cannot change (they are populated using GSON, which AFAIK uses 'normal' Java objects – e.g. String, not StringProperty). Furthermore, the object is read only – it only has getters, not setters.
I believe I can use a ReadOnlyJavaBeanObjectPropertyBuilder (yay Java naming?) or a ReadOnlyObjectWrapper to wrap the object as a property. However, the internal fields – which are what I want to bind the Labels to – are not converted to properties. Is there any way of doing such a recursive conversion – convert an entire object which contains normal object fields into a property which contains further properties? Is this even necessary – am I doing something wrong?
EDIT: I suspect any solution would have to use reflection.
I'm not sure but have a look at BeanPathAdapter
Its part of JFXtras-Labs, downloadable jar.
Source is here on GitHub.
How about adapters for the POJOs?
Example
public class Person{
private String name;
private Address addr;
\\getters, setters...
}
And for the JavaFX GUI
public class FXPerson{
public FXPerson(Person p){
this.name = \\build StringProperty
this.fxaddr = \\build ObjectProperty<FXAddress>
}
private StringProperty name;
private ObjectProperty<FXAddress> fxaddr;
}
Downside:
For every POJO you will have to write an adapter. And if a POJO changes (e.g. new property etc.) you will need to update the corresponding adapter.

Change class structure at runtime or get it into a anonymous class

I have
class A
{
String a;
String b;
//..getters, setters
}
Now I have ArrayList<? extends Object> resultData holding objects of class A and some other classes.
Basically I need this list 'resultData' to generate a Json file in some other API.
Now my question is while adding the class A objects to the list & some condition(X) is true I need a modified class A object (or any other anonymous class object) like:
class A
{
String a;
//..getters, setters
}
that is one particular object of class A shouldn't have field String b (before criticising, I'm doing this because I need such modified object for my particular JSon format & I don't want to define a new class definition that is used only once)
my last option is to make anonymous class like this: (& then add it to my List)
Object ob = new Object{
String b;
//..getters, setters
}
Also pls. suggest any other method of creating anonymous class with required structure.
Java is not meant for changing classes or creating new classes at runtime.
It is possible with a lot of effort, like generating java bytecode on the fly using a bytecode library like BCEL(http://commons.apache.org/proper/commons-bcel/) or even generate .java files and run javac to generate bytecode.
You could simply use a hash map like Map<String,Object> that "simulates" an object that can receive arbitrary fields. If you really need totally configurable classes, I would go this way. Of course, you would not have nice getters and setters for each property.
But why would you need nice setters / a nice class anyway? As the structure of the class is determined at runtime, you can write no code that depends on this class, as you do not know how it will look like.
if i'm getting you correctly, you need to get rid off field for serialization, to json format,
if im right, then make your field transient
other solution is to make super class with field which you want to serialize,
and make A to extend it
but modifying class on fly, it is not right way to go

Generate GWT class without using GWT.generate

I have a series of models, each of which has some properties that are used by a generator to generate getters/setters automatically (because there is some logic relating to default values contained therein and I don't intend to write these manually for models with 20 odd fields).
When I'm instantiating the model, I use GWT.create(...), but sometimes I have classes which refer to my model, and these don't know that the setters/getters exist, because they are generated.
For example, I have my model:
public class MyModel extends AbstractModel {
private Integer uid;
private String name;
// ...
}
public interface JsonBinder<MyModel> {
public void bindDataToMode(MyModel model, JSONWrapper json);
}
Now JsonBinder<T> is also a generated class using GWT.create, but it refers to MyModel and not the generated MyModelImpl. Therefore on compile I get errors like setUid(Integer value) is not defined for class MyModel.
Is there a way to have the compiler replace all uses of MyModel with MyModelImpl?
This applies to both generics and method arguments, return types, etc..
No.
In your specific case, I'd rather generate the MyModelImpl et al. upfront, using whichever code generator you want (including, for example, an annotation processor) and then code against the generated classes directly.

Categories