How to check if a field's implements an interface? - eclipse-jdt

If I have an org.eclipse.jdt.internal.compiler.ast.FieldDeclaration, how would I check if the type of the field implements an interface, let's say Serializable? I don't see any documentation for org.eclipse.jdt.internal.
I found extract interface that a class implementing using AST parser, but that one talks about org.eclipse.jdt.core.dom.FieldDeclaration.

EDIT: At first I didn't look at the package and thought you were asking about org.eclipse.jdt.core.dom.FieldDeclaration. That's what the answer is good for.
If you need to work with internal classes, we expect that you know exactly what you are doing. There won't be any documentation for those (but conceptually the same approach as shown for the public API can similarly be used for the internal AST and bindings, too).
Type compatibility is best tested using the corresponding bindings, so your quest would consist of theses steps:
From the FieldDeclaration obtain the binding using resolveBinding()
From the field binding retrieve the type binding
Once get a binding representing the expected super type Serializable
Ask the actual field type isSubTypeCompatible(serializableType)
You'll find some more details - in particular on (3) - in a recent discussion in the JDT forum.

Related

How to automatically bind template types in Enterprise Architect when a class is realizing a generic interface

I have defined a generic interface using Enterprise Architect (see figure below).
I would now like to specify the following realization:
class AircraftsTypesRepository implements Repository<AircraftTypes, Integer>
Is there a way for EA to automatically bind types and method signatures to the generic types I specified in the base interface. In other words, I would like to show in the diagram that for the AircraftTypesRepository class, T and K and bound to T=AircraftTypes, and K=Integer. I would also like to see this reflected in the interface methods
I thought about this and (as there's no native support) would suggest to script that. There are plenty of ways, so I'd take a KISS one. The Realize relation could be adorned with tagged values named Bind<val> or so where <val> is the name of a template parameter (in your example T or K). These TVs should then be defined as RefGUID which allows them to link to an EA element. Creating these TVs should be one script which looks into the templated class. You find the template definition in the table t_xref with
SELECT description FROM t_xref
WHERE client = `<GUID of element>` AND type = `elment property`
This will contain something like
#ELEMENT;GUID={5EC3D8DF-BC37-4529-8F36-0D9BA363955D};Name=E;Type=ClassifierTemplateParameter;Pos=0;#ENDELEMENT;;
(I created an example with just T but you will decode it easily, I guess.)
Now that you have the tagged value(s) set in the Realize you can run a second script to synch the definition ("just" look for textually identical types). Later you could alter the TVs and re-synch again (AFAIK there's not hook for TVs being altered so that needs to be triggered manually).
This is not a complete solution but just a suggestion which leaves open quite some field for experimentation (and failure).

Add XML documentation for named elements in ValueTuple

With ValueTuple in C# 7, it is now possible to write methods and properties that return or consume composite objects without explicitly declaring a type. These named tuples can however be potentially confusing when no documentation is provided.
As the primary and probably most convenient way of documenting libraries is by using XML documentation, is there any way using XML documentation to provide a description of the variables in a named tuple?
I know the obvious solution is to declare a type and document it accordingly. However, granted that due to some 'reasons' that cannot be done, is it possible to XML document the data members in a ValueTuple?
NB: A similar question was asked before the advent of the ValueTuple.
C# has no special syntax for documenting named elements of a tuple, you will have to use the normal <returns> and <param> elements and explain in words what each named element contains.
Though there is a proposal for adding this capability to the language.

JAXB how to remove anything from JDefinedClass

i am using jaxb to generate code from an xsd.
The generated code contains a lot of annotations; for classes and fields.
I am trying to use com.sun.tools.internal.xjc.Plugin to modify the generated code.
In the plugin run() method we are given an Outline class from which we can get ClassOutline. ClassOutline has an JDefinedClass final member which has the info about actual class which will be generated.
If i want to add anything, there are apis in JDefinedClass which can be used. But if i want to remove something, there is no way.
e.g. i cannot clear annotations, because the JDefinedClass.annotations() method returns an UnmodifiableCollection. so i cannot clear it or remove anything from it.
i tried to create another JDefinedClass by invoking the _class method but the ClassOutline.implClass variable is final, so i cannot set it.
how to get a JDefinedClass which does not have any annotations?
is there another phase of code generation which i can trap into to really control the generation of JDefinedClass?
The code model is, indeed mostly "write only". But, speaking of annotations, you have probably missed the methods like com.sun.codemodel.JDefinedClass.removeAnnotation(JAnnotationUse) and com.sun.codemodel.JMethod.removeAnnotation(JAnnotationUse) (implemented from com.sun.codemodel.JAnnotatable.removeAnnotation(JAnnotationUse)).
So they're there. You can remove annotations with the normal CodeModel API.
As I can see, you can also remove fields and methods from classes. So what exactly are you missing?
JDefinedClass.annotations() It return an unmodifiable collection object and you cannot modify them.
So work around for this, you can restrict annotation addition/deletion at class and field level before building JCodeModel.
You need to create a custom Jackson2Annotator class which extends Jackson2Annotator and override their methods according to your requirement.
Following are few methods which are being used for specific type of annotation property:
propertyOrder(OTB JsonPropertyOrder)
propertyInclusion(OTB JsonInclude)
propertyField(can be used for custom defined annotation at field level)
More you can discover by looking Jackson2Annotator class what fit into your need.

Get attribute name from proxy type without using reflections

We use early-bound class for development. But occasionally we use attribute names.
There is a way to do it using reflections, but reflections are not allowed in sandbox plugins.
What are the approaches to getting an attribute from proxy types without relying on reflections?
Opportunity.OpportunityId.AttributeName
You have a couple options:
You can use a RetrieveEntityMetadata to the list of attributes that the entity contains. You won't be able to use any early binding here, but you can inspect the results at run time to see what are valid attributes for the entity.
You could also create a simple utility that uses reflection to auto-generate a class or enum that contains the list of attributes before you actually deploy. Just add the class to your plugin dll and you'd have the benefits of early binding of entity attributes when developing your plugin, without having to do reflection at runtime.

Can we get declared properties of a Groovy class by its order?

I created a plain Groovy class (i.e Person class)with some properties. Now I want to get those declared attributes (which I've defined in my class) with their order, but I don't know how to do it.
I've tried to use Person.metaClass.getProperties() but it retrieves not only declared properties but also built-in Groovy ones.
Could you please help me on this: just get declared properties by its order when declaring.
Thank you so much!
I can't see a use case, but the compiler could reorder all fields declaration while creating bytecode. I'm pretty sure ordering is not a constraint on fields though it should mostly be the case for not modified/enhanced class
As per the JVM spec, generated fields should be marked SYNTHETIC (like generated methods) in the bytecode, so you can test with :
Person.getDeclaredFields().grep { !it.synthetic }
and filter the base Groovy fields like ClassInfo,metaClass and others beginning by __timestamp
But I'm not a specialist, there could be another way I don't think of
There was a question about this on the mailing list back in February of this year
The answer is, no. There is no way to get properties in the order they are declared in the class without doing some extra work.
You could parse the source file for the class, and generate an ordered list of property names from that
You could write a custom annotation, and annotate the fields with this annotation ie: #Order(1) String prop
You could make all of the classes where this matters implement an interface which forces them to have a method that returns the names of the properties in order.
Other than that, you probably want to have a re-think :-(

Resources