Hi,
My question is
How can (If possilbe) to add annotation for property in Oxygene for Java and how to read value.
It is only possible using method (setter and getter for property) ?
In annotation I can define target as
ElementType.TYPE,
ElementType.METHOD,
ElementType.CONSTRUCTOR,
ElementType.ANNOTATION_TYPE,
ElementType.PACKAGE,
ElementType.FIELD,
ElementType.LOCAL_VARIABLE
My simple example
[FldCustAttribute(keyField := true, RealName := ‘ID’)]
property ID : String read FID write FID;
I can read annotation for fields using :
for fld in myObject.class.Fields do
flatr := fld.GetAnnotation(typeof(myClass));
How to do it for property ? Should I add annoatation for setter and getter method only ?
You can’t have annotations on a property itself (logged that the compiler fails on this properly), however you can add them to the read or write part of a property:
type
IANN = public interface(Annotation)
method name: string;
end;
ConsoleApp = public class
public
class method Main(args: array of string);
[read: IANN(Name := 'test'), write:IANN(Name := 'test')]
property Name: String;
end;
Hello,
Thank you for your answer.
I tried it but i have problem when in my method I want to read annotation for property .
I use : for fld in myObject.class.Fields do
flatr := fld.GetAnnotation(typeof(myClass));
In this loop i have no access to property but when I changed property to public fileld (as below) all works fine.
Myclass = public class
public
[FldCustAttribute(keyField := true, RealName := ‘ID’)]
var Test : String;
end;
What should i changed to read annotation for property ?
Best regards
Ah sorry I should have explained it better. The java runtime has no concept of properties, essentially they’re a pair of get/set methods (property MyProperty: String becomes method getValue: String; method setValue(val: String)) the compiler creates these for you. The build you have lets you set annotations on a property but it shouldn’t have, those are lost. You can only apply annotations to the read, the write or the underlying field (read: write: or var: prefix for the annotation type) and use the .Methods or .Fields property on the class to access them.