Commit 65e199d1 authored by Jay Jay Billings's avatar Jay Jay Billings
Browse files

Merge branch 'next' of https://github.com/eclipse/ice into next

parents 3373a439 a6c82006
......@@ -18,22 +18,16 @@ public class DefaultFields {
* This field is left out of the <code>DataElement.matches</code> checks.
*/
private static Field privateId = Field.builder()
.name("privateId")
.name("UUID")
.varName("privateId")
.type(UUID.class)
.docString("The private UUID of this element. This field is left out of matches().")
.defaultValue(UUID.class.getCanonicalName() + ".randomUUID()")
.match(false)
.getter(false)
.getter(true)
.setter(false)
.alias(
Field.builder()
.name("UUID")
.unique(true)
.getter(true)
.setter(false)
.defaultField(true)
.build()
)
.defaultField(true)
.unique(true)
.build();
/**
......
......@@ -31,11 +31,24 @@ import lombok.Singular;
@Builder
@JsonDeserialize(builder = Field.FieldBuilder.class)
public class Field {
/**
* Name of the field.
*/
String name;
/**
* Name of the variable representing this field. If the same as
* {@link Field#name}, leave null.
*
* This handles cases like the default field {@link DefaultFields#privateId}
* being accessible through the method
* {@link org.eclipse.ice.dev.annotations.IDataElement#getUUID()} rather than
* {@code getPrivateId()}. In that example, the variable name would be
* {@code privateId} and the field name would be {@code UUID}.
*/
String varName;
/**
* String representation of the field's type.
*/
......@@ -97,11 +110,6 @@ public class Field {
*/
boolean unique;
/**
* A list of alternate names for this field.
*/
@Singular("alias") List<Field> aliases;
/**
* A list of annotations to apply to this field.
*/
......@@ -112,6 +120,29 @@ public class Field {
*/
@Builder.Default Set<String> modifiers = Set.of("protected");
/**
* Get the name of the variable representing this field.
*
* If no variable name has been specifically set, var == name.
* @return the name of the variable
*/
@JsonIgnore
public String getVarName() {
if (this.varName == null) {
return this.name;
}
return this.varName;
}
/**
* Get whether this field has a variable name that differs from the field name.
* @return whether the variable name differs from the field name
*/
@JsonIgnore
public boolean isVarNameDifferent() {
return this.varName != null;
}
/**
* Get a class by name or return null if not found
* @param cls
......@@ -153,39 +184,6 @@ public class Field {
return prefix + getNameForMethod();
}
/**
* Return whether this field has a getter, directly or via one of its aliases.
*
* This method is separate from {@code getAnyGetter()} despite being very
* similar for ease of use in velocity templates.
* @return true if getter present, false otherwise
*/
@JsonIgnore
public boolean hasGetter() {
return getAnyGetter() != null;
}
/**
* Return the name of any valid getter for this field, either a direct getter
* for the field or one of its aliases.
* @return getter name, null if none present
*/
@JsonIgnore
public String getAnyGetter() {
String retval = null;
if (getter) {
retval = getGetterName();
} else {
for (Field alias : aliases) {
if (alias.isGetter()) {
retval = alias.getGetterName();
break;
}
}
}
return retval;
}
/**
* Return if this field has a final modifier and is therefore a constant value.
* @return field is constant
......@@ -199,7 +197,6 @@ public class Field {
* Instruct Jackson how to deserialize fields.
*/
private interface FieldBuilderMeta {
@JsonDeserialize(contentAs = Field.class) FieldBuilder aliases(Collection<? extends Field> aliases);
@JsonDeserialize(contentAs = String.class) FieldBuilder annotations(Collection<? extends String> annotations);
@JsonDeserialize(contentAs = String.class) FieldBuilder modifiers(Set<String> modifiers);
@JsonAlias("fieldName") FieldBuilder name(String name);
......
......@@ -73,6 +73,19 @@ public class Fields implements Iterable<Field> {
.iterator();
}
/**
* Return iterator over fields where the variable name differs from the
* Field name.
*
* @return iterator over the fields
* @see Field#isVarDifferent()
*/
public Iterator<Field> getVarNamesDiffer() {
return fields.stream()
.filter(field -> field.isVarNameDifferent())
.iterator();
}
/**
* Returns an iterator over the mutable fields found in this collection. The
* mutable fields are the default set iterated over because they are the most
......
......@@ -44,10 +44,10 @@ public class ${class} implements ${interface}, Serializable {
#fielddoc
#nonnull($tab, $newline)
#if(!${field.Getter})
#if(!${field.Getter} || ${field.VarNameDifferent})
@Getter(AccessLevel.NONE)
#end
#if(!${field.Setter})
#if(!${field.Setter} || ${field.VarNameDifferent})
@Setter(AccessLevel.NONE)
#end
#fielddecl
......@@ -57,27 +57,25 @@ public class ${class} implements ${interface}, Serializable {
#fielddoc
#fielddecl
#end
#foreach($field in $fields)
#foreach($alias in ${field.aliases})
#if(${alias.Getter})
#foreach($field in ${fields.VarNamesDiffer})
#if(${field.Getter})
/**
* Get ${field.Name} by alias ${alias.Name}.
* Get ${field.VarName}.
* @return ${field.Name}
*/
public #fieldtype get${alias.NameForMethod}() {
return ${field.Name};
public #fieldtype ${field.GetterName}() {
return ${field.VarName};
}
#end
#if(${alias.Setter})
#end
#if(${field.Setter})
/**
* Set ${field.Name} by alias ${alias.Name}.
* Set ${field.VarName}.
* @param ${field.VarName} new value for ${field.Name}.
*/
public void set${alias.NameForMethod}(#nonnull("", " ")#fieldtype ${field.Name}) {
this.${field.Name} = ${field.Name};
public void set${field.NameForMethod}(#fieldparametertype ${field.VarName}) {
this.${field.VarName} = ${field.VarName};
}
#end
#end
#end
......@@ -86,17 +84,17 @@ public class ${class} implements ${interface}, Serializable {
*
* Used in JSON Deserialization.
#foreach($field in $fields)
* @param ${field.Name} {@code #fieldtype} for field ${field.Name}
* @param ${field.VarName} {@code #fieldtype} for field ${field.VarName}
#end
*/
@JsonCreator
public $class(
#foreach($field in $fields)
@JsonProperty("${field.Name}") #nonnull("", " ")#fieldtype ${field.Name}#if($foreach.hasNext),#end
@JsonProperty("${field.VarName}") #fieldparametertype ${field.VarName}#if($foreach.hasNext),#end
#end
) {
#foreach($field in $fields)
this.${field.Name} = ${field.Name};
this.${field.VarName} = ${field.VarName};
#end
}
......@@ -131,8 +129,8 @@ public class ${class} implements ${interface}, Serializable {
@Override
public void copy(@NonNull $interface element) {
#foreach($field in $fields)
#if($field.hasGetter())
this.${field.Name} = element.${field.AnyGetter}();
#if($field.Getter)
this.${field.VarName} = element.${field.GetterName}();
#end
#end
}
......@@ -162,23 +160,23 @@ public class ${class} implements ${interface}, Serializable {
// by debuggers.
#foreach($field in ${fields.Match})
// Matching ${field.Name}
boolean ${field.Name}Match =
// Matching ${field.VarName}
boolean ${field.VarName}Match =
#if(${field.Nullable})
this.${field.Name} == null ?
this.${field.Name} == other.${field.Name} :
this.${field.Name}.equals(other.${field.Name});
this.${field.VarName} == null ?
this.${field.VarName} == other.${field.VarName} :
this.${field.VarName}.equals(other.${field.VarName});
#elseif(${field.Primitive})
this.${field.Name} == other.${field.Name};
this.${field.VarName} == other.${field.VarName};
#else
this.${field.Name}.equals(other.${field.Name});
this.${field.VarName}.equals(other.${field.VarName});
#end## if nullable
#end## foreach
retval =
#@settab(6)
#foreach($field in ${fields.Match})#@nonewline
${field.Name}Match#if($foreach.hasNext) &&#if($foreach.count % 3 == 0)$newline#else #end#end
${field.VarName}Match#if($foreach.hasNext) &&#if($foreach.count % 3 == 0)$newline#else #end#end
#end#end;
#end
}
......
......@@ -25,7 +25,7 @@ public interface $interface extends IDataElement<${interface}> {
/**
* Set ${field.Name}.
*/
public void set${field.NameForMethod}(#fieldtype ${field.Name});
public void set${field.NameForMethod}(#fieldtype ${field.VarName});
#end## if Setter
#end## if DefaultField
#end## foreach field
......
......@@ -85,15 +85,15 @@ public class $class implements $interface<$elementInterface> {
/**
* Find $elementInterface by ${var.Name}.
* @param ${var.Name}
* @param ${var.VarName}
* @return found element or null
*/
#if(${var.DefaultField})
@Override
#end
public $elementInterface findBy${var.NameForMethod}($type ${var.Name}) throws Exception {
public $elementInterface findBy${var.NameForMethod}($type ${var.VarName}) throws Exception {
Document doc = this.collection
.find(PersistenceFilters.eq("${docName}", ${var.Name}))
.find(PersistenceFilters.eq("${docName}", ${var.VarName}))
.first();
if (doc == null) {
return null;
......@@ -105,23 +105,20 @@ public class $class implements $interface<$elementInterface> {
/**
* Find $elementInterface by ${var.Name}.
* @param ${var.Name}
* @param ${var.VarName}
* @return Iterator of results
*/
#if(${var.DefaultField})
@Override
#end
public Iterable<$elementInterface> findBy${var.NameForMethod}(${var.Type} ${var.Name}) throws Exception {
return this.collection.find(PersistenceFilters.eq("${var.Name}", ${var.Name}))
public Iterable<$elementInterface> findBy${var.NameForMethod}(${var.Type} ${var.VarName}) throws Exception {
return this.collection.find(PersistenceFilters.eq("${var.VarName}", ${var.VarName}))
.map(doc -> mapper.convertValue(doc, ${implementation}.class));
}
#end## if unique
#end## if getter and search
#end## macro findmethod
#foreach($field in $fields)
#findmethod($field ${field.Type} ${field.Name})
#foreach($alias in $field.Aliases)
#findmethod($alias ${field.Type} ${field.Name})
#end
#findmethod($field ${field.Type} ${field.VarName})
#end
}
\ No newline at end of file
......@@ -46,10 +46,13 @@
## Get evaluated field type
#macro(fieldtype)#evaluate(${field.Type})#end
## Get the type of this field for use in parameter list (includes nonnull)
#macro(fieldparametertype)#nonnull("", " ")#fieldtype#end
## Get field declaration
#macro(fielddecl)
#@settab(1)
#join(" ", ${field.Annotations})#join(" ", ${field.Modifiers})#fieldtype() ${field.Name}#if(${field.DefaultValue}) = #evaluate(${field.DefaultValue})#end;
#join(" ", ${field.Annotations})#join(" ", ${field.Modifiers})#fieldtype() ${field.VarName}#if(${field.DefaultValue}) = #evaluate(${field.DefaultValue})#end;
#end
#end
......
......@@ -5,6 +5,8 @@ package org.eclipse.ice.tests.dev.annotations.processors;
import static org.junit.jupiter.api.Assertions.*;
import java.util.UUID;
import org.eclipse.ice.dev.annotations.processors.Field;
import org.junit.jupiter.api.Test;
......@@ -35,7 +37,6 @@ class FieldTest {
.nullable(true)
.getter(false)
.setter(false)
.alias(Field.builder().name("another").getter(true).build())
.build();
String fJson = mapper.writeValueAsString(f);
......@@ -59,55 +60,30 @@ class FieldTest {
.type(boolean.class)
.build();
assertEquals("isTest", f.getGetterName());
}
/**
* Test hasGetter.
*/
@Test
void testHasGetter() {
Field f = Field.builder()
.name("test")
.type(String.class)
.getter(true)
.build();
assertTrue(f.hasGetter());
f = Field.builder()
.name("test")
.name("TEST")
.type(String.class)
.getter(false)
.alias(Field.builder().name("another").getter(true).build())
.build();
assertTrue(f.hasGetter());
f = Field.builder()
.name("test")
.getter(false)
.build();
assertFalse(f.hasGetter());
assertEquals("getTEST", f.getGetterName());
}
/**
* Test getAnyGetter.
* Test our handling of differences between the field name and the var name.
*/
@Test
void testAnyGetter() {
void testNameAndVarDifferences() {
Field f = Field.builder()
.name("test")
.type(String.class)
.getter(true)
.build();
assertEquals("getTest", f.getAnyGetter());
f = Field.builder()
.name("test")
.type(String.class)
.getter(false)
.alias(Field.builder().name("another").getter(true).build())
.name("UUID")
.type(UUID.class)
.varName("privateId")
.build();
assertEquals("getAnother", f.getAnyGetter());
assertTrue(f.isVarNameDifferent());
assertEquals("privateId", f.getVarName());
f = Field.builder()
.name("test")
.getter(false)
.build();
assertNull(f.getAnyGetter());
assertEquals("test", f.getVarName());
assertFalse(f.isVarNameDifferent());
}
}
......@@ -35,7 +35,6 @@ public class PojoOutlineTest {
.match(false)
.primitive(true)
.nullable(true)
.alias(Field.builder().name("another").getter(true).build())
.build()
).build();
......@@ -61,7 +60,6 @@ public class PojoOutlineTest {
.nullable(true)
.getter(false)
.setter(false)
.alias(Field.builder().name("another").getter(true).build())
.build()
).build();
......@@ -81,7 +79,6 @@ public class PojoOutlineTest {
.nullable(true)
.getter(false)
.setter(false)
.alias(Field.builder().name("another").getter(true).build())
.build()
).build();
assertEquals("TestElementImpl", pojo.getImplementation());
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment