How do I read this Generics correctly from JSON? - java

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:[]}]}

Related

How to avoid multiple inheritance in java

I am in a situation, where I'm trying to implement a (relatively simple) abstract syntax tree. All of the nodes inherit from a type called SimpleNode containing some code to store line and column information and accepting a visitor.
Now, some of the nodes should also be nameable, while others should have a property "accessible" (eg. public or private). Some nodes should even support both interfaces.
I'd preferably implement this using virtual inheritance and write two classes NameableNode and AccessibleNode, but Java doesn't support MI.
Eg NameableNode might have field "name" and implement simple getters and setters for this field. Similarly, AccessibleNode might also have a field "accessibility" and getters/setters.
What is a good way to implement this and avoid introducing code duplication in a huge part of the code base?
Small code example:
public class SimpleNode {
private int line = 0;
private int column = 0;
/* Getters and setters for line/column. */
/* ... */
}
public class NameableNode extends SimpleNode {
private String name = "";
/* Getters and setters for name */
}
public class AccessibleNode extends SimpleNode {
private boolean isPublic = false;
/* Getters and setters for accessibility */
}
You're looking for composition. There are many flavors of this - I will propose one that, from my understanding of what you're trying to build, should suit your purpose.
First, let's create some interfaces for yours Nodes:
public interface Nameable {
/* Getters and setters for name */
}
public interface Accessible {
/* Getters and setters for accessibility */
}
Next, you probably don't want to repeat the same implementation for every Node, so let's create those implementations:
public class NameDelegate() {
private String name = "";
/* Getters and setters for name */
}
public class AccessDelegate() {
private boolean isPublic = false;
/* Getters and setters for accessibility */
}
Now, let's put everything together:
public class SomeNodeA extends SimpleNode implements Nameable {
private NameDelegate nameDelegate;
public SomeNodeA(NameDelegate nameDelegate) {
this.nameDelegate = nameDelegate;
}
#Override
public String getName() {
return nameDelegate.getName();
}
#Override
public String setName(String name) {
nameDelegate.setName(name);
}
}
You can also have both behaviours in a single class:
public class SomeNodeB extends SimpleNode implements Nameable, Accessible {
private NameDelegate nameDelegate;
private AccessDelegate accessDelegate;
public SomeNodeB(NameDelegate nameDelegate, AccessDelegate accessDelegate) {
this.nameDelegate = nameDelegate;
this.accessDelegate = accessDelegate;
}
#Override
public String getName() {
return nameDelegate.getName();
}
#Override
public String setName(String name) {
nameDelegate.setName(name);
}
#Override
public boolean getAccessibility() {
return accessDelegate.getAccessibility();
}
/* etc... */
}
The idea is, you can package the state and the functionality of the different "features" into individual delegates, and expose them as corresponding interfaces in your Nodes.
Also, when operating on the Nodes, if you need to know whether a given instance of a Node supports a specific feature, you can use instanceof - e.g.:
if (someNode instanceof Nameable) {
// do naming stuff
}
In this case I would use the composition approach over inheritance:
public class Node {
private int line = 0;
private int column = 0;
/* Getters and setters for line/column. */
/* ... */
private String name = null;
public String getName() {
return this.name;
}
public void setName(String name) {
this._name = name;
}
private Boolean _isPublic = null;
public String isPublic() {
return this.name;
}
public void setIsPublic(boolean isPublic) {
this._isPublic = isPublic;
}
public boolean hasAccessibility() {
return this._isPublic != null;
}
public boolean hasName() {
return this._name != null;
}
}
Another solution that I like a bit more is creating these attributes dynamically using a HashMap and an enum that indicates all the possible attributes of a node. This way is more generic, as it requires to write less code for supporting new attributes, But it is also less typesafe(ish), as the additional attributes need to be casted at runtime:
import java.util.HashMap;
enum NodeAttribute {
NAME,
ACCESSIBILTY
}
enum NodeAccessibility {
PUBLIC,
PRIVATE
}
public class Node {
private int line = 0;
private int column = 0;
// Notice that this Object usage might involve some boxing for attributes of premitive type
private HashMap<NodeAttribute, Object> additionalAttributes = new HashMap<NodeAttribute, Object>();
/* Getters and setters for line/column. */
/* ... */
public boolean hetAttribute(NodeAttribute attribute) {
return this.additionalAttributes.containsKey(attribute);
}
public <T> T getAttributeValue(NodeAttribute attribute, Class<T> attributeClass) {
Object attributeValue = this.additionalAttributes.get(attribute);
// You may want to wrap the ClassCastException that may be raisen here to a more specfic error
T castedAttributeValue = attributeClass.cast(attributeValue);
return castedAttributeValue;
}
public void setAttributeValue(NodeAttribute attribute, Object value) {
// Notice that this implemintation allows changing the type of an existing attribute,
// If this is invalid behavior in your case you can throw an exception instead
this.additionalAttributes.put(attribute, value);
}
}
// Example usage
public class Program {
public static void main(String[] args) {
Node nodeWithNameOnly = new Node();
nodeWithNameOnly.setAttributeValue(NodeAttribute.NAME, 'node1');
Node nodeWithBoth = new Node();
nodeWithBoth.setAttributeValue(NodeAttribute.NAME, 'node2');
nodeWithBoth.setAttributeValue(NodeAttribute.ACCESSIBILTY, NodeAccessibility.PRIVATE);
Program.doStuffWithNode(nodeWithNameOnly);
/* output:
Node name: node1
*/
Program.doStuffWithNode(nodeWithBoth);
/* output:
Node name: node2
Node is public: False
*/
}
public static void doStuffWithNode(Node node) {
if (nodeWithNameOnly.hetAttribute(NodeAttribute.NAME)) {
String nodeName = nodeWithNameOnly.getAttributeValue(NodeAttribute.NAME, String.class);
system.out.println("Node name: " + nodeName);
}
if (nodeWithNameOnly.hetAttribute(NodeAttribute.ACCESSIBILTY)) {
NodeAccessibility nodeAccessibilty =
nodeWithNameOnly.getAttributeValue(NodeAttribute.ACCESSIBILTY, NodeAccessibility.class);
boolean nodeIsPublic = nodeAccessibilty == NodeAccessibility.PUBLIC;
system.out.println("Node is public: " + String.valueOf(nodeIsPublic));
}
}
}
In any case, this is the main rule of thumb - Inheritance should be used for an "is a" relation, whereas composition should be used for an "has a" relation.
For instance:
Fish extends Animal because a Fish is an Animal.
Post holds comments because a Post has comments.
And in our case, a node has a name and an accessibility level so it should hold them.

Why does IntelliJ tell me that my class never returns null?

private void addCompoundsFrom(Verse verse) {
Optional<List<Compound>> compounds = Optional.of(verse.getCompounds());
if (compounds.isPresent()) {
for (Compound compound : compounds.get()) {
addCompoundsFrom(compound);
}
}
}
The IntelliJ inspector tells me that the if-statement is always true. How can it know that? This is the Compounds class:
public class Compounds extends PositionalIds {
#XmlElement(name = "verse")
private List<Verse> verses;
public List<Verse> getVerses() {
return verses;
}
}
#XmlTransient
public abstract class PositionalIds {
#XmlAttribute(name = "start")
private String startId;
#XmlAttribute(name = "end")
private String endId;
public String getStartId() {
return startId;
}
public String getEndId() {
return endId;
}
}
And the Verse class:
public class Verse extends PositionalIds {
#XmlElement(name = "compound")
private List<Compound> compounds;
#XmlAttribute(name = "notation")
private String notation;
public List<Compound> getCompounds() {
return compounds;
}
public String getNotation() {
return notation;
}
}
If I stop using Optional to wrap the verse.getCompounds() result and instead just do a null check, the inspection message goes away.
I'm using Java 8.
The Optional class has two methods:
Optional.of -> throws exception if the parameter is null
Optional.ofNullable -> creates an empty optional if the parameter is null
Hence if your method returns null, the of() method will throw an exception and empty optional will never reach your if statement
Optional.of(verse.getCompounds()); returns an Optional that contains a valid value.
The isPresent check that follows will always be true because the Optional compounds will never not have a value, since you just set it to a valid value on the line above.

DynamoDBMappingException: no HASH key value present

I have a class named SampleEntity i.e a POJO which will help me create my dynamoDB table.The hash key and range key have been defined clearly in POJO object but still i get an exception that the hash key is not being defined
#DynamoDBTable(tableName = "sampletable1")
public class SampleEntity {
public static final String HASH_KEY = "f1_hash";
public static final String RANGE_KEY = "f2_range";
#DynamoDBAttribute(attributeName = HASH_KEY)
#DynamoDBHashKey
private Integer feild1;
#DynamoDBAttribute(attributeName = RANGE_KEY)
#DynamoDBRangeKey
private String field2;
#DynamoDBAttribute(attributeName = "f3")
private String feild3;
#DynamoDBAttribute(attributeName = "f4")
private String feild4;
#DynamoDBAttribute(attributeName = "f5")
private String feild5;
public Integer getFeild1() {
return feild1;
}
public void setFeild1(Integer feild1) {
this.feild1 = feild1;
}
public String getField2() {
return field2;
}
public void setField2(String field2) {
this.field2 = field2;
}
public String getFeild3() {
return feild3;
}
public void setFeild3(String feild3) {
this.feild3 = feild3;
}
public String getFeild4() {
return feild4;
}
public void setFeild4(String feild4) {
this.feild4 = feild4;
}
public String getFeild5() {
return feild5;
}
public void setFeild5(String feild5) {
this.feild5 = feild5;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof SampleEntity)) return false;
SampleEntity that = (SampleEntity) o;
if (!getFeild1().equals(that.getFeild1())) return false;
if (!getField2().equals(that.getField2())) return false;
if (!getFeild3().equals(that.getFeild3())) return false;
if (!getFeild4().equals(that.getFeild4())) return false;
return getFeild5().equals(that.getFeild5());
}
#Override
public int hashCode() {
int result = getFeild1().hashCode();
result = 31 * result + getField2().hashCode();
result = 31 * result + getFeild3().hashCode();
result = 31 * result + getFeild4().hashCode();
result = 31 * result + getFeild5().hashCode();
return result;
}
}
This my class and i am issuing a create table request on this class but i got DynamoDBMappingException that is no HASH key value present.
server = ServerRunner.createServerFromCommandLineArgs(new String[]{"-inMemory", "-port", "8005"});
server.start();
dynamoDBClient = new AmazonDynamoDBClient(new BasicAWSCredentials("any", "thing")).withEndpoint("http://localhost:8005");
dbMapper = new DynamoDBMapper(dynamoDBClient);
CreateTableRequest createTableRequest = ddbMapper.generateCreateTableRequest(SampleEntity.class);
createTableRequest.setProvisionedThroughput(new ProvisionedThroughput(5L, 5L));
dynamoDBClient.createTable(createTableRequest);
SampleLoginEntity data= new SampleLoginEntity();
data.setLogin(123);
data.setField2("range");
data.setFeild3("abc");
dbMapper.save(data);
There are two possible issues I can see (one that I ran into recently), but your setup is a little different than mine.
You're using both #DynamoDBAttribute and #DynamoDBHashKey on a single item - that's not necessary, and might be messing it up, though I don't have time to test it right now. You should be able to just do, #DynamoDBHashKey(attributeName=HASH_KEY) and you'll be fine. I think as is, you might be declaring an attribute as "f1_hash", and a hash key named as "field1", both mapping to the same internal value (though I could be wrong).
The issue I was having though is actually a result of this error message being really poorly worded - it will throw this exception when you call dbMapper.save() with an object with the hash key value set to null, though if your setLogin() was supposed to be setField1() that shouldn't be the issue here.
I got the same exeption when tried to get the record by the null hashCode:
myClassMapper.load(MyClass.class, null))
Try to set:
data.setField1("some_hash");

Editing a complex Java object in a Tapestry 5 web application

I am using Tapestry 5.3.6 for a web application and I want the user to edit an instance of a Java class (a "bean", or POJO) using a web form (which immediately suggests the use of beaneditform) - however the Java class to be edited has a fairly complex structure. I am looking for the simplest way of doing this in Tapestry 5.
Firstly, lets define some utility classes e.g.
public class ModelObject {
private URI uri;
private boolean modified;
// the usual constructors, getters and setters ...
}
public class Literal<T> extends ModelObject {
private Class<?> valueClass;
private T value;
public Literal(Class<?> valueClass) {
this.valueClass = valueClass;
}
public Literal(Class<?> valueClass, T value) {
this.valueClass = valueClass;
this.value = value;
}
// the usual getters and setters ...
}
public class Link<T extends ModelObject> extends ModelObject {
private Class<?> targetClass;
private T target;
public Link(Class<?> targetClass) {
this.targetClass = targetClass;
}
public Link(Class<?> targetClass, T target) {
this.targetClass = targetClass;
this.target = target;
}
// the usual getters and setters ...
}
Now you can create some fairly complex data structures, for example:
public class HumanBeing extends ModelObject {
private Literal<String> name;
// ... other stuff
public HumanBeing() {
name = new Literal<String>(String.class);
}
// the usual getters and setters ...
}
public class Project extends ModelObject {
private Literal<String> projectName;
private Literal<Date> startDate;
private Literal<Date> endDate;
private Literal<Integer> someCounter;
private Link<HumanBeing> projectLeader;
private Link<HumanBeing> projectManager;
// ... other stuff, including lists of things, that may be Literals or
// Links ... e.g. (ModelObjectList is an enhanced ArrayList that remembers
// the type(s) of the objects it contains - to get around type erasure ...
private ModelObjectList<Link<HumanBeing>> projectMembers;
private ModelObjectList<Link<Project>> relatedProjects;
private ModelObjectList<Literal<String>> projectAliases;
// the usual constructors, getters and setters for all of the above ...
public Project() {
projectName = new Literal<String>(String.class);
startDate = new Literal<Date>(Date.class);
endDate = new Literal<Date>(Date.class);
someCounter = new Literal<Integer>(Integer.class);
projectLeader = new Link<HumanBeing>(HumanBeing.class);
projectManager = new Link<HumanBeing>(HumanBeing.class);
projectMembers = new ModelObjectList<Link<HumanBeing>>(Link.class, HumanBeing.class);
// ... more ...
}
}
If you point beaneditform at an instance of Project.class, you will not get very far before you have to supply a lot of custom coercers, translators, valueencoders, etc - and then you still run into the problem that you can't use generics when "contributing" said coercers, translators, valueencoders, etc.
I then started writing my own components to get around these problems (e.g. ModelObjectDisplay and ModelObjectEdit) but this would require me to understand a lot more of the guts of Tapestry than I have time to learn ... it feels like I might be able to do what I want using the standard components and liberal use of "delegate" etc. Can anyone see a simple path for me to take with this?
Thanks for reading this far.
PS: if you are wondering why I have done things like this, it is because the model represents linked data from an RDF graph database (aka triple-store) - I need to remember the URI of every bit of data and how it relates (links) to other bits of data (you are welcome to suggest better ways of doing this too :-)
EDIT:
#uklance suggested using display and edit blocks - here is what I had already tried:
Firstly, I had the following in AppPropertyDisplayBlocks.tml ...
<t:block id="literal">
<t:delegate to="literalType" t:value="literalValue" />
</t:block>
<t:block id="link">
<t:delegate to="linkType" t:value="linkValue" />
</t:block>
and in AppPropertyDisplayBlocks.java ...
public Block getLiteralType() {
Literal<?> literal = (Literal<?>) context.getPropertyValue();
Class<?> valueClass = literal.getValueClass();
if (!AppModule.modelTypes.containsKey(valueClass))
return null;
String blockId = AppModule.modelTypes.get(valueClass);
return resources.getBlock(blockId);
}
public Object getLiteralValue() {
Literal<?> literal = (Literal<?>) context.getPropertyValue();
return literal.getValue();
}
public Block getLinkType() {
Link<?> link = (Link<?>) context.getPropertyValue();
Class<?> targetClass = link.getTargetClass();
if (!AppModule.modelTypes.containsKey(targetClass))
return null;
String blockId = AppModule.modelTypes.get(targetClass);
return resources.getBlock(blockId);
}
public Object getLinkValue() {
Link<?> link = (Link<?>) context.getPropertyValue();
return link.getTarget();
}
AppModule.modelTypes is a map from java class to a String to be used by Tapestry e.g. Link.class -> "link" and Literal.class -> "literal" ... in AppModule I had the following code ...
public static void contributeDefaultDataTypeAnalyzer(
MappedConfiguration<Class<?>, String> configuration) {
for (Class<?> type : modelTypes.keySet()) {
String name = modelTypes.get(type);
configuration.add(type, name);
}
}
public static void contributeBeanBlockSource(
Configuration<BeanBlockContribution> configuration) {
// using HashSet removes duplicates ...
for (String name : new HashSet<String>(modelTypes.values())) {
configuration.add(new DisplayBlockContribution(name,
"blocks/AppPropertyDisplayBlocks", name));
configuration.add(new EditBlockContribution(name,
"blocks/AppPropertyEditBlocks", name));
}
}
I had similar code for the edit blocks ... however none of this seemed to work - I think because the original object was passed to the "delegate" rather than the de-referenced object which was either the value stored in the literal or the object the link pointed to (hmm... should be [Ll]inkTarget in the above, not [Ll]inkValue). I also kept running into errors where Tapestry couldn't find a suitable "translator", "valueencoder" or "coercer" ... I am under some time pressure so it is difficult to follow these twisty passages through in order to get out of the maze :-)
I would suggest to build a thin wrapper around the Objects you would like to edit though the BeanEditForm and pass those into it. So something like:
public class TapestryProject {
private Project project;
public TapestryProject(Project proj){
this.project = proj;
}
public String getName(){
this.project.getProjectName().getValue();
}
public void setName(String name){
this.project.getProjectName().setValue(name);
}
etc...
}
This way tapestry will deal with all the types it knows about leaving you free of having to create your own coersions (which is quite simple in itself by the way).
You can contribute blocks to display and edit your "link" and "literal" datatypes.
The beaneditform, beaneditor and beandisplay are backed by the BeanBlockSource service. BeanBlockSource is responsible for providing display and edit blocks for various datatypes.
If you download the tapestry source code and have a look at the following files:
tapestry-core\src\main\java\org\apache\tapestry5\corelib\pages\PropertyEditBlocks.java
tapestry-core\src\main\resources\org\apache\tapestry5\corelib\pages\PropertyEditBlocks.tml
tapestry-core\src\main\java\org\apache\tapestry5\services\TapestryModule.java
You will see how tapestry contributes EditBlockContribution and DisplayBlockContribution to provide default blocks (eg for a "date" datatype).
If you contribute to BeanBlockSource, you could provide display and edit blocks for your custom datatypes. This will require you reference blocks by id in a page. The page can be hidden from your users by annotating it with #WhitelistAccessOnly.
http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/services/BeanBlockSource.html
http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/services/DisplayBlockContribution.html
http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/services/EditBlockContribution.html
http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/annotations/WhitelistAccessOnly.html
Here's an example of using an interface and a proxy to hide the implementation details from your model. Note how the proxy takes care of updating the modified flag and is able to map URI's from the Literal array to properties in the HumanBeing interface.
package com.github.uklance.triplestore;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
public class TripleStoreOrmTest {
public static class Literal<T> {
public String uri;
public boolean modified;
public Class<T> type;
public T value;
public Literal(String uri, Class<T> type, T value) {
super();
this.uri = uri;
this.type = type;
this.value = value;
}
#Override
public String toString() {
return "Literal [uri=" + uri + ", type=" + type + ", value=" + value + ", modified=" + modified + "]";
}
}
public interface HumanBeing {
public String getName();
public void setName(String name);
public int getAge();
public void setAge();
}
public interface TripleStoreProxy {
public Map<String, Literal<?>> getLiteralMap();
}
#Test
public void testMockTripleStore() {
Literal<?>[] literals = {
new Literal<String>("http://humanBeing/1/Name", String.class, "Henry"),
new Literal<Integer>("http://humanBeing/1/Age", Integer.class, 21)
};
System.out.println("Before " + Arrays.asList(literals));
HumanBeing humanBeingProxy = createProxy(literals, HumanBeing.class);
System.out.println("Before Name: " + humanBeingProxy.getName());
System.out.println("Before Age: " + humanBeingProxy.getAge());
humanBeingProxy.setName("Adam");
System.out.println("After Name: " + humanBeingProxy.getName());
System.out.println("After Age: " + humanBeingProxy.getAge());
Map<String, Literal<?>> literalMap = ((TripleStoreProxy) humanBeingProxy).getLiteralMap();
System.out.println("After " + literalMap);
}
protected <T> T createProxy(Literal<?>[] literals, Class<T> type) {
Class<?>[] proxyInterfaces = { type, TripleStoreProxy.class };
final Map<String, Literal> literalMap = new HashMap<String, Literal>();
for (Literal<?> literal : literals) {
String name = literal.uri.substring(literal.uri.lastIndexOf("/") + 1);
literalMap.put(name, literal);
}
InvocationHandler handler = new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getDeclaringClass().equals(TripleStoreProxy.class)) {
return literalMap;
}
if (method.getName().startsWith("get")) {
String name = method.getName().substring(3);
return literalMap.get(name).value;
} else if (method.getName().startsWith("set")) {
String name = method.getName().substring(3);
Literal<Object> literal = literalMap.get(name);
literal.value = args[0];
literal.modified = true;
}
return null;
}
};
return type.cast(Proxy.newProxyInstance(getClass().getClassLoader(), proxyInterfaces, handler));
}
}

Integer type reference String type value

two class:
public class BaseDo<K> {
protected K id;
public K getId() {
return id;
}
public void setId(K id) {
this.id = id;
}
}
public class BeanDo extends BaseDo<Integer> {
private String beanName;
public String getBeanName() {
return beanName;
}
public void setBeanName(String beanName) {
this.beanName = beanName;
}
}
I want use reflect to implment like this:
BeanDo beanDo = new BeanDo();
beanDo.setId("string here");
Integer type reference String type value.
Generics in Java are not used at runtime, so as far as the java runtime is concerned you're ID field is of type Object and so can be set to any value regardless of the generics. That said, doing so is a bad idea since anything assuming the generic contract will fail.
You can set the field by reflection as follows:
BeanDo beanDo = new BeanDo();
Method method = BeanDo.getClass().getMethod("setId", Object.class);
method.invoke(beanDo, "SomeRandomString");
That said, doing this is an extreamly bad idea because any code compile against BeanDo will assume that the id is an integer not a String. So any code like beanDo.getId() will fail with a class cast exception because it's not actually an integer.
Like the other posters, I'm somewhat in the dark about what you're trying to achieve.
Something like this?
public class BaseDo<K> {
protected K id;
public K getId() {
return id;
}
public void setId(K id) {
this.id = id;
}
}
public class BeanDo extends BaseDo<Integer> {
private String beanName;
public void setId(String id) {
setId(Integer.parseInt(id));
}
public String getBeanName() {
return beanName;
}
public void setBeanName(String beanName) {
this.beanName = beanName;
}
}
Now you can use something like this:
BeanDo beanDo = new BeanDo();
beanDo.setId("2");
What about this:
BeanDo beando = new BeanDo();
beando.setId("string there".hashCode());
I don't quite get what you mean with "I want to use reflect to implement this" though.
I guess you want something like this:
BeanDo doer = ... // get it from somewhere
String id = ... // get it from somewhere else too.
// and you want to set id to the doer bean.
reflectionMagicSetId( doer, id );
And have the method like:
private void reflectionMagicSetId( BandDo doer, String id ) {
/// do some magic here?
}
If that's what you want, what I give you works perfectly.
private void reflectionMagicSetId( BandDo doer, String id ) {
doer.setId( id == null ? 0 : id.hashCode() );
}
If you wann use integer then parse the string to integer as it will contain the integer and use that integer in the calling function argument
It seems like a subclass about the only way to be able to set a string, but still guarantee that anyone who's already calling getId() gets the Integer they expect. Something like this:
public class StringBeanDo extends BeanDo {
private String stringId;
public String getStringId()
{
return stringId;
}
public void setId( Integer val )
{
super.setId( val );
stringId = Integer.toString( val );
}
public void setId( String str )
{
stringId = str;
super.setId( convertStringToInteger( str )); // Do this however you like.
}
}
The implementation of convertStringToInteger would be up to you (it'll depend on what this ID is being used for). The key here is that you're maintaining TWO IDs, and keeping them in sync, so that older code can still limp along to some extent.

Categories

Resources