is there some type of reflection in Java that is simply an enum of the fields in a class?
All my fields are sitting right there at compile time, so I’m scratching my head over the need for a dynamic reflection approach to simply being able to reference the fields of a class.
-
Looking to get a set, define a subset of, and filter out some fields from set-of-all
-
…and probably call some getters/setters generated? by way of an enumeration to reference the field name/type/modifiers).
-
Anyone know of any compiler flag or, at this point,
-
an alternative javac? (note, musing the the Java 8 -parameters flag).
-
also note, not too interested in adding the full Lombok headspace, but I may swoon for this one. Although unlikely, I might even consider a language switch (to a JVM language)
I’m not a ‘language being’, and I know there’s “reasons” (albeit clue me into non hierarchy-composition/dynamic stuff reasons)… but as someone just working on some high up ORM/http mapping stuff… shouldn’t this be (added like a decade ago) ?
public class NiceClass {
//* NOTE this is not real java code today - don't copy and paste expecting it to work! */
static {
Set<NiceClass.class.field> allMyFields = NiceClass.class.fieldz.public();
Set<NiceClass.class.field> allMyLocalPublicFields = NiceClass.class.fieldz.local.public();
Set<NiceClass.class.field> allMyLocalPrivateAndLocalProtectedFields = { Sets.union(
NiceClass.class.fieldz.local.private(),
NiceClass.class.fieldz.local.protected())}
Set<NiceClass.class.field> specificFields = { Set.of(NiceClass.class.fields.ONE,
NiceClass.class.fields.THREE,
NiceClass.class.fields.FIVE )}
Set<NiceClass.class.field> allFieldsWithoutLoggerByFieldName = {
NiceClass.class.fieldz.all().stream().filter(s-> s.name != "logger" ).collect(Collectors.toSet()) }
Set<NiceClass.class.field> allFieldsWithoutLoggerByType = {
NiceClass.class.fieldz.all().stream().filter(s-> s.type != Logger.class ).collect(Collectors.toSet())
}
Set<NiceClass.class.field> allNonTransientFields = {
NiceClass.class.fieldz.all().stream().filter(s-> s.modifier != Modifier.transient ).collect(Collectors.toSet())
}
Set<NiceClass.class.field> holyGrailMySerialization = {
allFieldsWithoutLoggerByType.stream().filter(s-> s.access != private &&
! Modifiers.contains(Modifier.transient) &&
! Modifiers.contains(Modifier.volatile) &&
s.type != MyCredentials.class )
.collect(Collectors.toSet())
}
Set<NiceClass.class.field> holyGrailMySerialization = {
holdGrailMySerialization.stream().filter(s-> ! s.annotations.contains(PrivateStuff.class) )
.collect(Collectors.toSet())
}
//and if you give the mouse a cookie...why shouldn't these be easy to "enable"?
public void set(Object.FieldInterface fieldEnum, Object value); //(casting in impl)
//and typesafe...
NiceClass.class.fieldz.stream().filter(
s->s.setterEnabled)
.map(s->s.type)
.collect(Collectors.toSet()).foreach(s->{
<modifiers> void set<s.type>(fieldEnum, value);
}
})
//bonus to figure out how to reduce that param to what's *statically enabled*...
//I know this is wonky looking syntax ( a Set of types allowed in a generic? )
<modifiers> void set<s.type>(Object.FieldInterface<!!Set.contains(NiceClass.class.fieldz.enabled)>) fieldEnum, value);
//Anyways, the possibilties seem endless having this lightweight reflection...
}