I am using a Kotlin class from Java code. My Kotlin class looks like:
class Something {
var a = 0
}
I want to be able to access a from Java code like
s = new Something();
s.a = 5;
however, I only have s.getA() and s.setA(5). Is there any way to make this property directly settable and gettable from Java? Obviously we can't have custom getter and setter in this case.
You can annotate a property with the #JvmField annotation to expose it as a Java field.
If you need to expose a Kotlin property as a field in Java, you need to annotate it with the #JvmField annotation. The field will have the same visibility as the underlying property. You can annotate a property with #JvmField if it has a backing field, is not private, does not have open, override or const modifiers, and is not a delegated property.
Related
I have created an interface on kotlin.
interface IDataManager{
val dataType: String?
}
Now I am trying to get its variable in my java class, like following.
public static DataWrapper getInstance(IDataManager iDataManager) {
dataType= iDataManager.dataType;
return instance;
}
But I am getting error: cannot find symbol iDataManager.dataType
Please call getter function to get a value of the variable:
dataType = iDataManager.getDataType();
If we use properties on Kotlin side we should use getters and setters to access those properties on Java side.
edit - as Alexey points out in the comments, this doesn't work for interfaces, since the property needs a backing field and properties in interfaces can't have those. It's still useful to know, but it doesn't apply to the OP's question
As well as what Sergey said, you can add the #JvmField annotation on things if you want to expose them as a field instead of generating the getters and setters
interface IDataManager{
#JvmField val dataType: String?
}
#JvmStatic is another useful one for Java interop, you can put it on properties and functions in companion objects, so instead of this
Utils.Companion.coolUtility()
you can do this (like you're used to)
Utils.coolUtility()
I have a Groovy class:
class SomeGroovyThing {
String name
}
And in Java I receive a reference to an instance of this class as an Object:
Object o = project.getExtensions().getByName("groovyThing")
I can't cast the Object because I don't have access to the Groovy class in Java (for unrelated reasons). How can I retrieve the value of the name property (i.e. I want to do o.name but obviously that doesn't work).
I am wanting to do some sort of Reflection, but the fact that my Object is a Groovy class is throwing a wrench in things.
You can do this using Reflection.
You can get it:
String name=(String)o.getClass().getField("name").get(o);
Or set it:
o.getClass().getField("name").set(o,name);
If name is private, you can set or get it using the setter/getter methods(getMethod() in Class) or use setAccessible(true) on the Field.
I want to build a validator between two fields in POJO inside my Spring-Boot app.
I searched an example how to make it and I found this code:
Object checkedValue = BeanUtils.getProperty(object, selected);
My problem is that I can't use BeanUtils.getProperty(object, selected),
How can I get the property from my bean ?
If you are referring to pull specific values, you can simply use the getDeclaredFiled from the Class. A typical code block for this implementation as per the code you have will look as specified below:
Class<?> tempClass = object.getClass();
Field field = tempClass.getDeclaredField(selected);
field.setAccessible(true);
Object checkedValue = field.get(object);
If you are referring to pull environment variables, You can use Environment from the Spring's core package package org.springframework.core.env
If you are using annotations, simple #Autowire the Environment and you can retrieve the property like you do using BeanUtils. Typical code block would like as specified below
#Autowired
private Environment environment;
String value = environment.getProperty("property_name");
I want get annotation value form a specific annotation, such as #Callcount, which has a field named key.
MethodList<MethodDescription.InDefinedShape> methods = typeDefinition.getDeclaredMethods();
for (MethodDescription.InDefinedShape method : methods) {
AnnotationDescription.Loadable<CalledCount> callCountAnno;
if ((callCountAnno = method.getDeclaredAnnotations().ofType(CalledCount.class)) != null) {
callCountAnno.getValue(?);//how can i do here?
}
}
i don't know how to build the parameter of method callCountAnno.getValue(), what i shoud do?
The easiest solution would be to load the annotation which allows you to access the value in a type-safe manner. You can do so via the load or loadSilent methods. Alternatively, you need to provide the propery you want to resolve. You can provide a loaded method reference via: MethodDescription.ForLoadedMethod( ... ).
I have 2 java annotation types, let's say XA and YA. Both have some method(). I parse the source code and retrieve Annotation object. Now I'd like to dynamicaly cast the annotation to the real type of it to be able to call the method(). How can I do it without the instanceof statement? I really want to avoid switch-like source. I need something like this:
Annotation annotation = getAnnotation(); // I recieve the Annotation object here
String annotationType = annotation.annotationType().getName();
?_? myAnnotation = (Class.forName(annotationType)) annotation;
annotation.method(); // this is what I need, get the method() called
?_? means I have no idea what would be myAnnotation type. I cannot use the base class for my XA and YA annotations since the inheritance in annotations is not allowed. Or is it possible to do somehow?
Thanks for any suggestion or help.
Why don't you use the typesafe way to retrieve your annotation ?
final YourAnnotationType annotation = classType.getAnnotation(YourAnnotationType.class);
annotation.yourMethod();
If your annotation can't be found, null is returned.
Please note that this also works with fields and methods.
One way is to invoke the method dynamically using the name of it:
Annotation annotation = getAnnotation();
Class<? extends Annotation> annotationType = annotation.annotationType();
Object result = annotationType.getMethod("method").invoke(annotation);
This approach is quite risky and totally compromise the code refactoring if needed.