Google Cloud Endpoint - cast return model - java

I'm a little confused about the returned object from my GoogleCloudEndpoint.
My Android App uses an Object e.g TestObject with something like this path: com.example.classes.TestObject. My GoogleCloudEndpoint uses this class too and returns it in some methods, but returned objects of those methods are always another generated class like this: `com.example.testApi.model.TestObject.
Now in my App this is very confusing because I have to handle two nearly identical objects with the same name. The only difference between the object types is, that the generated object has private fields with getters & setters, while the original one had public fields. Obviously there is not way to cast those object to the other.
Is there any way to transform this generated object back to the original one? Or should I only use the generated one in my App?

You have to create your own helpers to transform back and forth. The model classes are subclasses of a generic JSON container object.

Related

Downcasting in Java ? Is this a downcasting issue at all?

So I have a POJO class let´s call it: InnerDomainObject.
Then I have an object representing this object, with a few more fields, for communication towards different clients (it s an API DTO): OuterDomainObject
Because the DTO has in fact all of the POJOs fields, I made OuterDomainObject inherit from InnerDomainObject.
Now I need to somehow cast InnerDomainObject to OuterDomainObject --> not possible.
I want to avoid writing a constructor iterating through all the fields.
I want to avoid writing useless code.
I just want OuterDomainObject to be created out of InnerDomainOBject´s values and then add some to it before sending it to the client.
What´s the best way of doing this ?
You sound like you are using the Adapter Pattern. You shouldn't need to cast an InnerDomainObject to an OuterDomainObject. You should use composition: the OuterDomainObject should hold a reference to an InnerDomainObject, which will likely be passed into a constructor. When a client invokes a method on an OuterDomainObject, if that method exists in InnerDomainObject, the OuterDomainObject should call that method on its instance of InnerDomainObject. Instead of casting an InnerDomainObject foo to an OuterDomainObject, just create a new OuterDomainObject and pass in foo: new OuterDomainObject(foo). You will need to write some simple glue code, but the result is very clean.

What is the difference between a class wrapper and Java, and a singleton in Objective-C?

I hear the term "Class Wrapper" float around a lot, and I was curious what it is. I read explanations on this site about them, and they basically you just sound like a Singleton and Objective-C. My two questions are:
1. What is the difference between a singleton in the class wrapper?
2. Are class wrappers ever used in iOS projects, or do we use singletons in the iOS world more often?
I think you are confused on what one of those entities is, they are not similar in the slightest (apart from being a part of software engineering...).
A singleton is a single object (of any class you like) that can be accessed from anywhere in the entire project by any bit of code you like. It's a single point in space that is reference-able from any other point in space.
A "Wrapper Class" is a class that wraps another class (or primitive). Say you want to pass an int to a function or method, that method modifies that int and you want the calling object to see the change. Since ints are passed by value instead of by reference (by default) any changes you make to that int in the called function are ignored by the caller. So the caller sees no change in the int's value. Now if you make an Integer object and make it wrap an int you can now pass that Integer object by reference to functions or methods, modify the Integer object and get the modified value back into the calling function. A Wrapper Class wraps an object (or primitive) into another object to give it different functionality.
Singletons and class wrappers are two different concepts. A singleton refers to a class whose only role is to have one instance, hence the name. Say you wanted to build an address book and store multiple contacts within, it would make sense that there only be one address book instance in your application instead of creating multiple address books if the purpose of your application is only to keep track of contacts within a single address book.
Within Obj-C class wrappers form the concept of bridging a primitive data type inherited from the C language and having object representations for them. You gave the example of a float data type. In the case of numbers the foundation framework provides the abstract NSNumber class to 'wrap' around a primitive data type such as a float, or int and give you an object representation. From there you are able to store them in an array, or dictionary or deal with them as normal objects (send messages, extend, etc). Another great example of a generic class wrapper is the NSValue class that allows you to create object representations for say a CGPoint or CGRect, which are used extensively in iOS.

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

Why can't Constructors objects be serialized in Java?

I have a class that implements Serializable, it's part of a bigger mesh of objects, it contains a Constructor field, but when it's the Constructors turn to be serialized it throws the NotSerializableException.
I guess I'll have to write the logic myself and recreate the constructor myself every time I deserialize the containing object, but why the heck on earth would the designers of Java wanna create such hassle in the first place? I realize that the ClassLoader is needed to figure out the identity of a class, and that the ClassLoader itself will not be serialized and deserialized, but shouldn't the constructor remember the string arguments used to create it?
Yes, as you realized Constructor is not serializable.
You need to make the Constructor field transient and restore it manually, when needed.
Yes, the Java designers could have made the Constructor class serialized down to the class name and argument list, but that would open a huge can of worms, basically boiling down to the fact that in any given JVM there can be an arbitrary number of classes with that name and there's no sane way to know which one to use when deserializing such an object.

Preferred method for initializing a child class parameters in Java?

I have an application that takes some input and generates configuration files as output. Since the exact input or output format could change over time, I defined two interfaces: Importer and Exporter.
Each concrete importer or exporter could have different parameters that need to be initialized to work. For example, if the import data is coming from a CSV file you only need the path of the file, but if the data is coming from a database then you need a connection string, username, password, etc. Same thing for exporters.
My implementation currently is:
public interface Importer {
public void setup(Map<String,String> params);
public List<ConfigEntry> getList();
}
public interface Exporter {
public void setup(Map<String,String> params);
public void writeDocument(List<ConfigEntry> entries) throws IOException;
}
The setup method needs to be called before getList() or writeDocument() can be called. I use a Map to keep parameters because each child class can have different parameters.
Is using JavaBean style parameter initialization a preferred way? That means, adding setConnnectionString(), setCSVFilePath(), setX() to each child class.
What are the advantages, disadvantages of these approaches?
There are two obvious downsides to map-based approach:
Absence of well-defined parameter names. Yes, you could define them as constants somewhere but you'd still need to check that parameter name is valid as passed.
Absence of well-defined parameter types. Even worse then above - if I need to pass an integer I'd have to convert it to String and you'll have to parse it (and deal with possible errors). Can be somewhat mitigated by using Map<String,Object> and auto-bounding but then you'd still need to validate appropriate types.
Setter-based approach has only one downside - it can't be done. That is, it can't be reliably done by using setters ALONE - you need to supplement it with some kind of init() or afterPropertiesSet() method that will be called after all setters and will allow you to perform additional (co-dependent) validation and initialization steps.
Also, something like this practically begs for some kind of Dependency Injection framework. Like Spring, for example.
I wouldn't say that passing a Map (or Properties) object in the constructor is necessarily preferred over child class specific setter, or vice versa. Which approach is best depends on how you are going to instantiate the classes.
If you are going to instantiate the classes directly from Java then the Map approach tends to be neater, especially if you have a good way to assemble the maps. (For example, loading a Properties object from a property file.) The 'setters' approach forces you to write code against each of the child class APIs.
On the other hand, if you are going to instantiate the classes using some container framework that supports "wiring", "inversion of control" or the like (e.g. Spring, PicoContainer, JavaBeans, etc), then setters are generally better. The framework typically takes care of when and how to instantiate the classes and call the setters, using reflection under the hood to do the work.
So the answer is ... it depends ...

Categories