Created
July 1, 2016 16:26
-
-
Save ctoestreich/7d61e00e1ed9204dd1ae766526a7b099 to your computer and use it in GitHub Desktop.
Get Properties In Dot Notation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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