Kotlin reflection - getting all field names of a Class
Tag : java , By : Igor Carron
Date : March 29 2020, 07:55 AM
I wish this help you Probably what you want is to get properties of a class, not fields. This can be done as follows: MyClass::class.declaredMemberProperties
MyClass::class.java.declaredFields
val p = MyClass::class.declaredMemberProperties.first()
val modifiers = p.javaGetter?.modifiers
|
Kotlin, how to retrieve field value via reflection
Date : March 29 2020, 07:55 AM
Does that help Instead of using Java fields and Java reflection code, you can also use Kotlin properties and Kotlin reflection classes: class Reflector {
val Foo = 1;
fun printFields() {
this::class.memberProperties.forEach {
if (it.visibility == KVisibility.PUBLIC) {
println(it.name)
println(it.getter.call(this))
}
}
}
}
|
Getting member variables through reflection in Kotlin
Date : March 29 2020, 07:55 AM
seems to work fine I'm just going by the documentation, so the below may be a bit wrong, but you could try this: val prop : KCallable = Foo::class.members.firstOrNull { it.name == "x" }
if (prop != null) {
val xValue : Int? = prop.call(object)
//you have to declare the type of the xValue
}
|
Re-create (generic) member and populate/assign field member(s) via Java reflection
Tag : java , By : hsdfhksh
Date : March 29 2020, 07:55 AM
help you fix your problem I have a set of classes with any number of non-custom members — just primitives, wrappers, etc.; some of them can be annotated with @Encrypted in order to be processed differently. All types implement Envelope. , To expand on my comment, try something like this (untested): import io.shido.domain.Envelope;
import org.apache.commons.lang3.reflect.FieldUtils;
import java.util.Map;
public final class Factory<T extends Envelope> {
private final Class<T> clazz;
private final Map<String, Object> regularMembers;
private final Map<String, Object> secureMembers;
public Factory(final Class<T> clazz, final Map<String, Object> regularMembers, final Map<String, Object> secureMembers) {
this.clazz = clazz;
this.regularMembers = regularMembers;
this.secureMembers = secureMembers;
}
public T build() {
try {
final T result = clazz.newInstance();
regularMembers.forEach((fieldName, fieldValue) -> assign(result, fieldName, fieldValue));
secureMembers.forEach((fieldName, fieldValue) -> assign(result, fieldName, fieldValue));
return result;
} catch (final Exception e) {
logger.error("Cannot build type based on input parameters due to:", e);
throw new IllegalStateException(e.toString());
}
}
private void assign(final T type, final String fieldName, final Object fieldValue) {
try {
FieldUtils.getField(type.getClass(), fieldName).set(type, fieldValue);
} catch (final IllegalAccessException e) {
e.printStackTrace();
}
}
}
|
Kotlin reflection: get instance of a member property
Tag : kotlin , By : user119413
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , I'm trying to retrieve the instances of my properties, like the example below: , I was able to solve it in a not-very-beautiful way: instance::class.memberProperties.forEach {
instance.javaClass.getMethod("get${it.name.capitalize()}").invoke(instance)
}
|