Using Eclipse's JDT, how does one get an IType from a class name? - eclipse

Is there a simple, straightforward way to get an IType from a class name? I think there must be some static method somewhere. Basically, I'd like to do something like:
IType objectType = Somewhere.getType("java.lang.Object")
Does anybody know of something like this? I have been searching in vain.

Given an IProject, one can use the IJavaProject#findType methods, e.g.
IType objectType = project.findType("java.lang.Object");

Look at org.eclipse.jdt.core.search.SearchEngine. I haven't tried it myself, I'm usually using the ASTParser with the Resolve option on (that's when you parse a source), but it should do the trick.

Related

Can I create a custom JUnit 5 annotation #Defect("Ticket-ID") and map that to #Tag("Ticket-ID")

I want to know how to add a parameter to an annotation, and then use that parameter to help compose an annotation.
As a simple example, suppose I want to define #Defect(<Ticket-ID>). Among other things, that annotation will add #Tag(<Ticket-Id>).
Can I do this? If so, what would the code look like?
I think you can't map #Defect() to #Tag(), don't see the point on it also. You would be only changing Tag name.
What you can do is, create an interface to map #Tag("Defect") and then add a value as a parameter to it:
#Target({TYPE, METHOD})
#Retention(RetentionPolicy.RUNTIME)
#Test
#Tag("Defect")
#interface Defect {
int value();
}
Then you could use it like:
#Defect("JiraIssue-1234");
You can also get the value you pass as parameter like:
AnnotationSupport.findAnnotation(_extensionContext.getElement(), Defect.class).get().value()

Eclipse fails to auto-suggest Jackson class

What is causing Eclipse to NOT recognize and consequently not offer any suggestion on an import of JsonParser.Feature as shown in the picture below:
Manually adding the static import of com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_SINGLE_QUOTES (commented in the picture above), however, works fine.
Would it be the case that something is eclipsing the file on the classpath, and if so - what is Eclipse's strategy on resolving those conflicts? Or is it something else?
Thank you in advance.
You cannot use JsonParser class to access Feature since it is not a static member of the class. Instead you can directly use the Feature class :
mapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);
I can suggest 2 workarounds:
Use AutoComplete (Ctrl + Space) to suggest classes:
Add . to class Name (JsonParser.) and then remove it (JsonParser) it will suggest all JsonParser classes:

A simple way to register a fake for a concrete class using AutofacContrib.NSubstitute

A class that is resolved as
builder.Resolve<IMyInterface>
Can be faked like this (for testing)
builder.RegisterType<MyFakeClass>().As<IMyInterface>();
But what if my class is resolved as
builder.Resolve<MyRealClass>
How to I fake this in autofac registeration? If I am using AutofacContrib.NSubstitute.
How can I achieve something like this
builder.RegisterType<MyFakeClass>().As<MyRealClass>();
As long as MyFakeClass inherits MyRealClass, that's all you have to do.

Xml Serialize Object (HttpBrowserCapabilities)

There is a native framework object called HttpBrowserCapabilities. I'd like to Serialize this to XML. Any ideas of the best way to go about it?
My first thought was to create my own class that inherits it then decorate all the properties with XML and then serialize it. I was wondering if there was a simpler (magical) way to do this. :)
TIA
Unless the class is marked as being serializeable, no. However to achive the above, using a decorator, you can use IDataContractSurrogates. See http://msdn.microsoft.com/en-us/library/system.runtime.serialization.idatacontractsurrogate.aspx

Wicket: how to use the BodyTagAttributeModifier class?

i'm trying to dynamically add the class attribute to the body tag, and i came across this class. but i can't seem to understand how to use this class. i have something like this in my page class (or panel class, as i tried with that too):
add(new BodyTagAttributeModifier("class", "homepage", this));
this doesn't even compile, saying there's something wrong with the 2nd parameter. but i think String is automatically considered a Model in wicket, like the Label class. am i missing something here?
What if you just add an wicket:id to the body attribute and use the AttributeAppender class? Or, if the body attribute already has an id, can't you just use this class?
http://wicket.sourceforge.net/apidocs/wicket/behavior/AttributeAppender.html
Some Wicket Components have this String-to-model-shortcut (like Label), but it's not a general feature. You have to convert your String into a Model manually:
add(new BodyTagAttributeModifier("class", Model.of("homepage"), this));