I'm reasonably confident in my first generics container, but stuck on how to word the casting on the client side. This is what was working before I got involved in learning <T> stuff:
CommonNounContainer typeContainer = new Json().fromJson(CommonNounContainer.class, result);
I was looking at having to create a different container for each class, and that doesn't seem like good design. Below is my updated, non-working attempt to read in my new generics container:
JSONContainer<CommonNoun> typeContainer = new Json().fromJson(JSONContainer.class, result);
My IDE doesn't care for this phrasing, noting:
Type safety: The expression of type JSONContainer needs unchecked
conversion to conform to JSONContainer
When executed, my err log reads:
result = {"myObject":{"cid":{"oid":129},"name":"technology","form":1},"children":[]}
com.badlogic.gdx.utils.SerializationException: Field not found: cid (java.lang.Object)
Serialization trace:
{}.myObject.cid
myObject (semanticWeb.rep.concept.JSONContainer)
at com.badlogic.gdx.utils.Json.readFields(Json.java:854)
at com.badlogic.gdx.utils.Json.readValue(Json.java:1011)
at com.badlogic.gdx.utils.Json.readFields(Json.java:863)
at com.badlogic.gdx.utils.Json.readValue(Json.java:1011)
at com.badlogic.gdx.utils.Json.fromJson(Json.java:789)
at com.b2tclient.net.Communicator$2.handleHttpResponse(Communicator.java:95)
at com.badlogic.gdx.net.NetJavaImpl$2.run(NetJavaImpl.java:224)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:830)
I'm sure there's some way I'm supposed to include a reference to the CommonNoun type to the right of the equals sign, but I haven't been able to figure it out. How do I do it? There's lots of applicable posts concerning generics, casting, JSON, and stripping away of class information. One of them I tried to follow that wasn't about the casting above regarded adding the T class as a private variable within the container during construction:
How do I get a class instance of generic type T?
but I ran into similar syntax issues trying to refer to the class correctly, just in a different spot along the process. I have my doubts, too, that I can read this class variable from the JSON file before telling JSON how to classify the information in the file.
Javadoc for the fromJson(Class<T>, String) method:
Type Parameters:
<T>
Parameters:
type May be null if the type is unknown.
json
Returns:
May be null.
I may already have a viable answer submitted by deduper, but, as requested, here are the CommonNounContainer and JSONContainer classes:
import java.util.ArrayList;
public class CommonNounContainer {
private CommonNoun myCommonNoun;
private ArrayList<CommonNounContainer> children;
public CommonNounContainer(CommonNoun concept) {
myCommonNoun = concept;
children = new ArrayList<CommonNounContainer>();
}
//Creates an empty shell. This would be for categories you want to group by, but not display/select in the select box.
public CommonNounContainer() {
children = new ArrayList<CommonNounContainer>();
}
public void addChildren(ArrayList<CommonNounContainer> newChildren) {
children.addAll(newChildren);
}
public void addChild(CommonNoun concept) {
children.add(new CommonNounContainer(concept));
}
public ArrayList<CommonNounContainer> getChildren() {
return children;
}
public CommonNoun getValue() {
return myCommonNoun;
}
public boolean hasChildren() {
if (children.size() > 0) return true;
else return false;
}
public String toString() {
return myCommonNoun.toString();
}
}
public class JSONContainer<T> {
private T myObject;
private ArrayList<JSONContainer<T>> children;
// public Class<T> typeParameterClass;
public JSONContainer() {
}
public JSONContainer(T anObject) {
myObject = anObject;
children = new ArrayList<JSONContainer<T>>();
}
/* public JSONContainer(T anObject, Class<T> typeParameterClass) {
myObject = anObject;
children = new ArrayList<JSONContainer<T>>();
this.typeParameterClass = typeParameterClass;
}
*/
public void addChildren(ArrayList<JSONContainer<T>> newChildren) {
children.addAll(newChildren);
}
public void addChild(T concept) {
children.add(new JSONContainer<T>(concept));
}
public ArrayList<JSONContainer<T>> getChildren() {
return children;
}
public T getValue() {
return myObject;
}
public boolean hasChildren() {
if (children.size() > 0) return true;
else return false;
}
public String toString() {
return myObject.toString();
}
}
Additional classes requested:
public class CommonNoun extends Concept {
/**
*
*/
private static final long serialVersionUID = 6444629581712454049L;
public CommonNoun() {
super();
}
public CommonNoun(String name, ConceptID cidIn) {
super(name, cidIn);
this.form = ConceptDefs.COMMON_NOUN;
}
}
public class Concept implements Serializable {
/**
*
*/
private static final long serialVersionUID = 2561549161503772431L;
private ConceptID cid = null;
private final String name;
Integer form = 0;
// ArrayList<ProperRelationship> myRelationships = null;
/* #Deprecated
public Concept(String name) {
this.name = name;
}*/
public Concept() {
name = "";
}
public Concept(String name, ConceptID cidIn) {
// this(name);
this.name = name;
cid = cidIn;
}
/*
* This should be over-ridden by any subclasses
*/
public Integer getForm() {
return form;
}
public ConceptID getID() {
return cid;
}
public void setID(ConceptID cidIn) {
cid = cidIn;
}
//this doesn't make any sense. Throw exception?
public String getName() {
return name;
}
public boolean isCommon() {
return true;
}
/**
*
* #return
*/
#Override
public String toString() {
return getName() + "(" + cid.toString() + ")";
}
public boolean equals(Concept other) {
return ((getID().equals(other.getID())));
}
}
public class ConceptID implements Serializable {
long oid;
public ConceptID() {
oid = -1;
}
public ConceptID(long oid) {
this.oid = oid;
}
public long getValue() {
return oid;
}
/**
*
* #return
*/
#Override
public String toString() {
return Long.toString(oid);
}
public Long toLong() {
return Long.valueOf(oid);
}
public boolean equals(ConceptID other) {
return (oid == other.getValue());
}
/**
* Factory model for generating ConceptIDs
*
* This one is here as a convenience as many IDs come in as a String from web POSTs
* #param idAsString
* #return
*/
static public ConceptID parseIntoID(String idAsString) {
ConceptID returnID = null;
try {
returnID = new ConceptID( Long.parseLong(idAsString) );
} catch (Exception e) {
System.err.println("Expected the string, " + idAsString + ", to be Long parsable.");
e.printStackTrace();
}
return returnID;
}
TL;DR:
Proposed Fix…
System.out.println( new Json( ).toJson( new JSONContainer<>( ... ) ) to see the correct string format of a JSONContainer's JSON.
Make sure your result input argument to Json.fromJson(Class<T>, String) is in the same format printed out in 1.
e.g. {myObject:{class:CommonNoun,cid:{oid:139},name:Jada Pinkett Smith,form:69},children:[{myObject:{class:CommonNoun,cid:{oid:666},name:Jaden Pinkett Smith,form:-666},children:[]},{myObject:{class:CommonNoun,cid:{oid:69},name:Willow Pinkett Smith,form:69},children:[]}]}
The long answer…
„My IDE doesn't care for this phrasing, noting:“
Type safety: The expression of type JSONContainer needs unchecked conversion to conform to JSONContainer
It's the compiler warning you about heap pollution. The IDE merely translated this compiler warning (which is what you'd see on the command line)…
...Communicator.java uses unchecked or unsafe operations.
...Recompile with -Xlint:unchecked for details.
…into the more user-friendly message the IDE showed you.
It is only a warning; not an error. To make that warning go away, change this: JSONContainer<CommonNoun> typeContainer = ... to this: JSONContainer typeContainer = ...
„When executed, my err log reads:“
result = {"myObject":{"cid":{"oid":129},"name":"technology","form":1},"children":[]}
com.badlogic.gdx.utils.SerializationException: Field not found: cid (java.lang.Object)...
The most likely cause of that error is — like the error message says — either your JSONContainer class or your CommonNoun class does not have the cid field that is present in the JSON string you're trying to deserialize.
I was able to reproduce that error with this…
...
private static final String JADEN_AS_JSON = "{jden:{class:CommonNoun,person:Jaden,place:Hollywood,thing:HashBeen}}";
private static final String JADEN_FAILS_AS_ACTOR = "{jden:{class:CommonNoun,person:Jaden,place:Hollywood,thing:HasBeen, cid:{oid:129} }}";
static public void main( String ... args ){
out.printf( "%1$22s%n", "foo");
JSONContainer< CommonNoun > wtf = new JSONContainer< > ( );
CommonNoun wtBrattyF = new CommonNoun( "Jaden Pinkett Smith", "Hollywood", "HasBeen" );
wtf.setJden( wtBrattyF );
out.printf( "%1$42s%n", wtf );
Json jden = new Json();
out.printf("%1$59s%n", jden.toJson( wtf ) );
JSONContainer wtReifiableF = jden.fromJson(JSONContainer.class, JADEN_AS_JSON); /* This is fine */
out.printf("%1$59s%n", jden.toJson( wtReifiableF ) );
JSONContainer/*< CommonNoun >*/ wtUnReifiableF = jden.fromJson( JSONContainer.class, JADEN_AS_JSON );
wtUnReifiableF = jden.fromJson( JSONContainer.class, JADEN_FAILS_AS_ACTOR ); /* This causes the error you reported */
}
...
Early on it succeeds; but later on it fails…
JSONContainer [ jden: CommonNoun [ person: Jaden Pinkett Smith, place: Hollywood, thing: HasBeen ] ]
{jden:{class:CommonNoun,person:Jaden Pinkett Smith,place:Hollywood,thing:HasBeen}}
{jden:{class:CommonNoun,person:Jaden,place:Hollywood,thing:HashBeen}}
Exception in thread "main" com.badlogic.gdx.utils.SerializationException: Field not found: cid (CommonNoun)
Serialization trace:
{}.jden.cid
jden (JSONContainer)
at com.badlogic.gdx.utils.Json.readFields(Json.java:893)
at com.badlogic.gdx.utils.Json.readValue(Json.java:1074)
at com.badlogic.gdx.utils.Json.readFields(Json.java:902)
at com.badlogic.gdx.utils.Json.readValue(Json.java:1074)
at com.badlogic.gdx.utils.Json.fromJson(Json.java:829)
at DeduperAnswer.main(DeduperAnswer.java:33)
I have now confirmed by experimentation that given the existence of a Cid class…
public class Cid {
int oid;
/* ... getter and setter elided ... */
}
… And given the existence of a CommonNoun class that HAS A Cid…
public class CommonNoun {
Cid cid;
String name;
int form;
/* ... getters and setters elided ... */
}
…Then trying to deserialize a JSONContainer from a result that has the following value, will produce the exact same error you originally reported…
result = {"myObject":{"cid":{"oid":129},"name":"technology","form":1},"children":[]}
If your actual CommonNoun class is implemented like my stand-in above (with a Cid field), then you need to retry your json.fromJson(Class<?>, String) call with your result string formatted like…
{myObject:{class:CommonNoun,cid:{oid:139},name:Jada Pinkett Smith,form:69},children:[{myObject:{class:CommonNoun,cid:{oid:666},name:Jaden Pinkett Smith,form:-666},children:[]},{myObject:{class:CommonNoun,cid:{oid:69},name:Willow Pinkett Smith,form:69},children:[]}]}
public Tipo getTipo() {
return this.Importo < 0.0 ? Tipo.USCITA : Tipo.ENTRATA;
}
public int compareTo(Movimento m) {
if (this.idConto != this.idConto) {
return this.idConto - this.idConto;
}
return this.DataMov.compareTo(this.DataMov);
}
public static enum Tipo {
ENTRATA,// here i have this error : The constructor Movimento.Tipo() is undefined
USCITA;// here is the same : The constructor Movimento.Tipo() is undefined
private Tipo(String string2, int n2) {
}
}
I already have constructor that I need, what else I need to write?
I am not sure how do you want to define enum. There are basically 2 solutions to this:
1. Define no parameter enum
public static enum Tipo {
ENTRATA,
USCITA;
}
2. Define enum with parameters
public static enum Tipo {
ENTRATA("Entrata", 1),
USCITA("Uscita", 2);
private String string;
private int integer;
private Tipo(String string, int integer) {
this.string = string;
this.integer = integer;
}
}
You wrote a constructor that takes two arguments, but no default constructor. That means the compiler will not provide a no-arg constructor. You should provide one or remove the private constructor.
I see no reason for the private constructor with two arguments. You don't have any private data members in your enum.
Why is your enum static? Remove that.
public enum Tipo {
ENTRATA, USCITA;
}
You are written wrong enum.
public enum abc {
ENTRATA("abc", 1),// here i have this error : The constructor Movimento.Tipo() is undefined
USCITA("xyz", 2);// here is the same : The constructor Movimento.Tipo() is undefined
private abc(String string2, int n2) {
}
}
I have the following enum:
public enum ChangeMode {
None(1), Add(2), Update(3), Delete(4);
// String value
public String getStringValue() {
return name();
}
public static ChangeMode getEnumValue(String v) {
return valueOf(v);
}
public static ChangeMode getEnumValue(int intValue) {
for (ChangeMode x : values())
if (Objects.equals(x.intValue, intValue))
return x;
return null;
}
// Int value
private int intValue;
private ChangeMode(int intValue) {
this.intValue = intValue;
}
public int getIntValue() {
return this.intValue;
}
}
Notice the enum values start from 1-4 and not 0-3.
I'm using Wildfly and Jackson.
When I'm getting the value 4 from the client JSON (using HTTP POST) I'm getting the following error:
Can not construct instance of com.ta9.enums.ChangeMode from number
value (4): index value outside legal index range [0..3] at [Source:
org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream#55a6e779;
line: 770, column: 26] (through reference chain:
I'm trying to have spring-data convert my enum type to and int field in cassandra but I'm getting the following exception:
Unexpected runtime exception java.lang.IllegalArgumentException:
Value 2 of type class com.twc.atg.td.dbo.client.ClassCode does not correspond to any CQL3 type
Here is the piece of code I'm using:
#Enumerated(EnumType.ORDINAL)
#Column("class_code")
public ClassCode classCode;
Since it is not supported by spring-data-cassandra, you can implement this logic in getters/setters.
#Table
public class Writer {
...
public enum WriterType {
POET, DETECTIVE, JOUNALIST
}
...
#Column(value = "writer_type")
private Integer writerType;
...
public WriterType getWriterType() {
return WriterType.values()[writerType];
}
public void setWriterType(WriterType writerType) {
this.writerType = writerType.ordinal();
}
i define a enum like below:
public enum EventType {
UPDATE(0), ADD(1), REMOVE(2), RESPONSE(3);
private Integer id;
public Integer id() {
return this.id();
}
/**
* constructor method
*/
EventType(Integer id) {
this.id = id;
}
public static EventType getInstance(Integer id) {
switch (id) {
case 0:
return UPDATE;
case 1:
return ADD;
case 2:
return REMOVE;
case 3:
return RESPONSE;
default:
return null;
}
}
}
I want to create a enum instance by callback getInstance method, the JNI code like below:
jclass eventType_cls = (*env)->FindClass(env,"com/example/hellojni/EventType");
jmethodID midInstance = (*env)->GetStaticMethodID(env,eventType_cls,"getInstance","(I)[Lcom/example/hellojni/EventType;");
it pass the compiler, but when run to the JNI GetStaticMethodID method, the platform throws a error like below:
java.lang.NoSuchMethodError: no static method with name='getInstance'
signature='(I)Lcom/example/hellojni/EventType;'
in class Lcom/example/hellojni/EventType;
I don't know what's different enum with other class, do you have any idea?
The signature that you use in JNI for the method is looking for a method that looks like this:
public static EventType[] getInstance(int id) {
}
So the return type of the signature is definitely wrong. It should not have the [ there.
Now you have two possibilities either change the input type to int on the Java side or you change the type signature to (Ljava/lang/Integer;)Lcom/example/hellojni/EventType;.
Now you will realize that the second alternative is not very easy to handle since to access the value of the Integer you would have to use JNI methods and all the other fuzz.
You should also rethink why you would ever want to use the Integer wrapper instead of the primitive int in you Java code. There is simply no reason for it.
Rewrite your enum to something look like this:
public enum EventType {
UPDATE(0), ADD(1), REMOVE(2), RESPONSE(3);
private int id;
public int id() {
return this.id();
}
/**
* constructor method
*/
EventType(int id) {
this.id = id;
}
public static EventType getInstance(int id) {
switch (id) {
case 0:
return UPDATE;
case 1:
return ADD;
case 2:
return REMOVE;
case 3:
return RESPONSE;
default:
return null;
}
}
}
And change the JNI method lookup to this:
jmethodID midInstance = (*env)->GetStaticMethodID(env,eventType_cls,"getInstance","(I)Lcom/example/hellojni/EventType;");