Skip to content

Instantly share code, notes, and snippets.

@ctoestreich
Created July 1, 2016 16:26
Show Gist options
  • Save ctoestreich/7d61e00e1ed9204dd1ae766526a7b099 to your computer and use it in GitHub Desktop.
Save ctoestreich/7d61e00e1ed9204dd1ae766526a7b099 to your computer and use it in GitHub Desktop.
Get Properties In Dot Notation
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.ParameterizedType;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ConditionField {
}
class Credit {
String productName
@ConditionField
Card card
@ConditionField
List<Product> products
}
class Product {
String name
}
class Card {
String cardType
String cardNumber
EnumType enumType
}
enum EnumType {
MPP('MPP')
EnumType(String value){
this.value = value
}
String value
}
class Customer {
String firstName
String lastName
@ConditionField
Credit credit
}
void getProperties(String prefix, Class object, List<String> names){
def filtered = ['class', 'active']
object.declaredFields.grep { !it.synthetic }.collect{it}.findAll{!filtered.contains(it.name)}.each {
if(it.getAnnotation(ConditionField)){
// println it.name + ' ' + it.type
if (it.genericType instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) it.genericType;
}
String name = getProperties("$prefix.${it.name}", it.type, names)
if(name){
names << name
}
}
names << "$prefix.${it.name}"
}
}
List<String> names = []
getProperties('customer', Customer, names)
println names.sort()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment