public class ReferencePath extends Object
A Reference path consists of:
Reference paths can be described by the combination of (a) the starting Java object type, and (b) the path of reference
fields and final target field in a String form consisting of field names separated by period (".") characters.
All of the fields except the last must be reference fields.
For example, path "parent.age" starting from object type Person might refer to the age of
the parent of a Person.
When a complex field appears in a reference path, both the name of the complex field and the specific sub-field
being traversed should appear, e.g., "mymap.key.somefield". For set and list fields, the (only) sub-field is
named "element", while for map fields the sub-fields are named "key" and "value".
Fields of Sub-Types
In some cases, a field may not exist in a Java object type, but it does exist in a some sub-type of that type. For example:
@JSimpleClass(storageId = 10)
public class Person {
@JSimpleSetField(storageId = 11)
public abstract Set<Person> getFriends();
@OnChange("friends.element.name")
private void friendNameChanged(SimpleFieldChange<NamedPerson, String> change) {
// ... do whatever
}
}
@JSimpleClass(storageId = 20)
public class NamedPerson extends Person {
@JSimpleField(storageId = 21)
public abstract String getName();
public abstract void setName(String name);
}
Here the path "friends.element.name" is technically incorrect because "friends.element"
has type Person, and "name" is a field of NamedPerson, not Person. However, this will
still work as long as there is no ambiguity, i.e., in this example, there are no other sub-types of Person
with a field named "name". Note also in the example above the
SimpleFieldChange parameter to the method
friendNameChanged() necessarily has generic type NamedPerson, not Person.
In cases where multiple sub-types of a common super-type type have fields with the same name but different storage IDs,
the storage ID may be explicitly specified as a suffix, for example, "name#123".
Reference paths are created via JSimpleDB.parseReferencePath().
JSimpleDB.parseReferencePath()| Modifier and Type | Method and Description |
|---|---|
int[] |
getReferenceFields()
Get the storage IDs of the reference fields in this path.
|
Class<?> |
getStartType()
Get the Java type of the object at which this path starts.
|
int |
getTargetField()
Get the storage ID associated with the target field in the target object type.
|
Set<TypeToken<?>> |
getTargetFieldTypes()
Get the Java type(s) corresponding to the field at which this path ends.
|
int |
getTargetSuperField()
Get the storage ID associated with the complex field containing the target field
a sub-field, in the case that the target field is a sub-field of a complex field.
|
Class<?> |
getTargetType()
Get the Java type of the object at which this path ends.
|
Set<Class<?>> |
getTargetTypes()
Get the possible Java types of the object at which this path ends.
|
String |
toString()
Get the
String form of the path associated with this instance. |
public Class<?> getStartType()
If there are zero reference fields in this path, then this will equal the target object type, or possibly a super-type if the target field exists only in a sub-type.
public Class<?> getTargetType()
The returned type will be as narrow as possible while still including all possibilities, but note that it's
possible for there to be multiple candidates for the "target type", none of which is a sub-type of any other.
To retrieve all such target types, use getTargetTypes(); this method just invokes
Util.findLowestCommonAncestorOfClasses() on the result.
public Set<Class<?>> getTargetTypes()
If there are zero reference fields in this path, then this method will return only the Java type of the starting object type, or possibly a sub-type if the target field exists only in a sub-type.
The returned type(s) will be maximally narrow. The set will contain only one element if a unique such type exists, otherwise it will contain multiple mutually incompatible supertypes of the object types at which this path ends.
public Set<TypeToken<?>> getTargetFieldTypes()
The returned type(s) will be maximally narrow. The set will contain only one element if a unique such type exists, otherwise it will contain multiple mutually incompatible supertypes of the object types at which this path ends. The latter case can only occur when the field is a reference field, and there are multiple Java model classes compatible with the field's type.
public int getTargetField()
This is just the storage ID of the last field in the path.
public int getTargetSuperField()
public int[] getReferenceFields()
The path may be empty, i.e., zero references are traversed in the path.
Otherwise, the first field is a field in the starting object type and the last field is field in some object type that refers to the target object type.
Copyright © 2017. All rights reserved.