How to find references to a Spring converter in Eclipse? - eclipse

I'm working on a Java web app which utilises Spring's ConversionService API.
Converters look like this:
public class MyCustomConverter implements Converter<MySourceClass, MyTargetClass> {
#Override
public MyTargetClass convert(final MySourceClass source) {
// ...conversion code...
return myTargetClass;
}
}
and are registered in the application config, e.g:
#PostConstruct
public void addConverters() {
genericConversionService.addConverter(myCustomConverter);
// ...others...
}
A conversion can then be applied like this:
MyTargetClass result = conversionService.convert(mySource, MyTarget.class);
The problem I'm having is finding usage within the code of a particular converter (such as the example directly above). Am using Eclipse IDE - could anyone suggest a way to do this?

If you want to see all the references made to the method ConversionService.convert, you can highlight the convert method and use the Eclipse short-cut Ctrl + Shift + G. This will search the method inside your entire workspace. To search only in the project, you can right-click on the method and select References > Project.
To restrict the references search with a specific method parameter, see this answer : https://stackoverflow.com/a/11836545/1743880.

Related

Setting a eclipse plug-in to singleton when creating a plug-in project via IPluginContentWizard interface

I'm developing a wizard that implements the org.eclipse.pde.ui.IPluginContentWizard interface. Thus it gets added as plug-in project template in the end of the plug-in project wizard. All files will be created just fine, but there is one error in the project. The plug-in is not declared to be a singleton which it must be when extending extension points.
How do I do that within the wizard? I figured it needs to be done in performFinish(IProject project, IPluginModelBase model, IProgressMonitor monitor) but neither the project nor the model gives me a possibility to do so.
Edit: For future readers: My mistake was, that I didn't add the extension via the API but rather via generating the plugin.xml "by hand". This caused no mechanism in the background to do their job and thus the singleton directive wasn't set.
This way will be too long, let's use more PDE API:
First, define the template section
import org.eclipse.pde.ui.templates.OptionTemplateSection;
public class YourTemplateSection extends OptionTemplateSection {
//implement abstract methods according your needs
#Override
protected void updateModel(IProgressMonitor monitor) throws CoreException {
IPluginBase plugin = model.getPluginBase();
//do what is needed
plugin.add(extension);//here the "singleton" directive will be set
}
}
then use the section with wizard
import org.eclipse.pde.ui.templates.ITemplateSection;
import org.eclipse.pde.ui.templates.NewPluginTemplateWizard;
public class YourContentWizard extends NewPluginTemplateWizard {
#Override
public ITemplateSection[] createTemplateSections() {
return new ITemplateSection[] { new YourTemplateSection() };
}
}
In case one does the same rookie mistake then me, I wanted to post my solution I came up after revisiting the project later:
Don't create the plugin.xml manually, use the PDE API of the plugin model to add extensions.
In the org.eclipse.pde.ui.IPluginContentWizard implementions's performFinish(...) method do this:
try {
IPluginExtension extension = model.getExtensions().getModel().getFactory().createExtension();
extension.setPoint("org.eclipse.elk.core.layoutProviders");
IPluginElement provider = model.getPluginFactory().createElement(extension);
provider.setName("provider");
provider.setAttribute("class", id + "." + algorithmName + "MetadataProvider");
extension.add(provider);
model.getExtensions().add(extension);
} catch (CoreException e) {
e.printStackTrace();
}

How to use classes like RegistryRoot correctly in custom actions?

I have to implement a custom action to search the windows registry for the installed version of the dotnet framework. Therefore I thought to extend the ReadRegistryValueAction to integrate my individual search algorithm. But the custom action will not be found at the IDE. So I extends the action from the AbstractInstallAction and included the RegistryRoot class to configure the action inside the IDE the same way as with provided registry actions of install4j framework.
public class CheckDotNetInstallationAction extends AbstractInstallAction {
private RegistryRoot registryRoot;
public RegistryRoot getRegistryRoot() {
return registryRoot;
}
public void setRegistryRoot(RegistryRoot registryRoot) {
this.registryRoot = registryRoot;
}
#Override
public boolean install(InstallerContext paramInstallerContext)
throws UserCanceledException {
// do custom search
return false;
}
}
But instead to get a dropdown list, there is only a blank field. I expected also a dropdown list the same way as in the present registry action. Now there are two questions:
Is it possible to extends existing actions/screens/forms and to use and configure it in the IDE or is it necessary to extends from the AbstractInstallAction?
How can I use classes like RegistryRoot for my custom components the same way as they are used in the actions provided by the install4j framework? Specifically the way to configure these components inside the IDE.
You have to add add a BeanInfo class and set an enumeration mapper. See the source file
samples/customCode/SampleActionBeanInfo.java
in your install4j install4j Installation and and look for the the call to setEnumerationMappers.

SpringApplicationContextLoader ignores Application class

The SpringApplicationContextLoader assumes that the application is either using 100% XML or 100% Java config. This is because #ContextConfiguration allows either a list of classes or locations/value, not both. If any is specified, SpringApplicationContextLoader ignores the Application class that creates and starts the SpringApplication.
Trying to make Boot work with a 100% Groovy/no-XML pet project, I ran across the above issue. My Application class has #EnableAutoConfiguration and #ComponentScan annotations on it, the former required by Boot to set up a Web server. The later I had to keep because of SPR-11627. On the other hand, if I omitted the locations/value on #ContextConfiguration, dependencies weren't set up (duh!).
I give the code below along with a patch that I locally made to SpringApplicationContextLoader. If there's a better way, please let me know.
MovieDatabaseRESTClientIntegrationTest.groovy
RunWith(SpringJUnit4ClassRunner)
#ContextConfiguration(value = ['classpath:client-config.groovy', 'classpath:integ-test-config.groovy'],
loader = PatchedSpringApplicationContextLoader)
#SpringApplicationConfiguration(classes = MovieDatabaseApplication)
#WebAppConfiguration
#IntegrationTest
class MovieDatabaseRESTClientIntegrationTest {
MovieDatabaseApplication.groovy
#EnableAutoConfiguration
#ComponentScan
class MovieDatabaseApplication {
SpringApplicationContextLoader.java fix
private Set<Object> getSources(MergedContextConfiguration mergedConfig) {
Set<Object> sources = new LinkedHashSet<Object>();
sources.addAll(Arrays.asList(mergedConfig.getClasses()));
sources.addAll(Arrays.asList(mergedConfig.getLocations()));
/* The Spring application class may have annotations on it too. If such a class is declared on the test class,
* add it as a source too. */
SpringApplicationConfiguration springAppConfig = AnnotationUtils.findAnnotation(mergedConfig.getTestClass(),
SpringApplicationConfiguration.class);
if (springAppConfig != null) {
sources.addAll(Arrays.asList(springAppConfig.classes()));
}
if (sources.isEmpty()) {
throw new IllegalStateException(
"No configuration classes or locations found in #SpringApplicationConfiguration. "
+ "For default configuration detection to work you need Spring 4.0.3 or better (found "
+ SpringVersion.getVersion() + ").");
}
return sources;
}
Also posted on Spring forum.
I could be wrong but I don't think there is any support for beans{} configuration in #ContextConfiguration and #SpringContextConfiguration is just an extension of that. A feature request in JIRA would be appropriate. Also there has never been any support for mixed configuration format (as the entry point at least) - you always have to choose either XML or #Configuration, or else supply your own ContextLoader. You also shouldn't have both #ContextConfiguration and #SpringContextConfiguration on the same class (the behaviour is undefined).

Using Java.util.scanner with GWT

for some reason when I try to use scanner with gwt, i get the following error:
No source code is available for type java.util.Scanner; did you forget to inherit a required module?
I looked around and it seems the "No source code is available for type xxxx" errors are due to not having a Javascript equivalent type for the Java type.
Is scanner not able to be used with GWT?
Here is a snippet of my code:
import java.util.Scanner;
...
public void submit(){
String text = editor.getEditor().getText();
Scanner input = new Scanner(text);
while(input.hasNextLine()){
String line = input.nextLine();
if(line.contains("//")){
cInfo.setDone(false);
cInfo.setCode(text);
return;
}
cInfo.setDone(true);
cInfo.setCode(text);
}
}
}
java.util.Scanner is not part of the GWT JRE Emulation. If you need a detail overview of what is inside the emulation here is the link to the docs:
https://developers.google.com/web-toolkit/doc/latest/RefJreEmulation#Package_java_util
Your code (at least the one in the current version of your question) is probably[*] equivalent to
public void submit() {
String text = editor.getEditor().getText();
if ("".equals(text))
return;
cInfo.setDone(!text.contains("//"));
cInfo.setCode(text);
}
However, I have a feeling that this may not actually be what want to do (or is it?)
If you need to split strings on the client side, I usually recommend the Splitter class in Guava. Most of its methods are GwtCompatible, and (together with CharMatcher, Joiner, ...) it's great to use both on the client and server side of your Java code.
[*] assuming, that setDone and setCode are simple setters without side effects

Eclipse RCP: How to access internal classes of plugins?

I want to use the default XML editor (org.eclipse.wst.xml.ui) of Eclipse in an RCP application. I need to read the DOM of the xml file currently open. The plugin doesn't offer any extension point, so I'm trying to access the internal classes. I am aware that the I should not access the internal classes, but I don't have another option.
My approach is to create a fragment and an extension point to be able to read data from the plugin. I'm trying not to recompile the plugin, that's why I thought that a fragment was necessary. I just want to load it and extract the data at runtime.
So, my question is: is there another way to access the classes of a plugin? if yes, how?
Any tutorial, doc page or useful link for any of the methods is welcome.
Since nobody answered my question and I found the answer after long searches, I will post the answer for others to use if they bump into this problem.
To access a plugin at runtime you must create and extension point and an extension attached to it into the plugin that you are trying to access.
Adding classes to a plugin using a fragment is not recommended if you want to access those classes from outside of the plugin.
So, the best solution for this is to get the plugin source from the CVS Repository and make the modifications directly into the source of the plugin. Add extension points, extensions and the code for functionality.
Tutorials:
Getting the plugin from the CVS Repository:
http://www.eclipse.org/webtools/community/tutorials/DevelopingWTP/DevelopingWTP.html
Creating extensions and extension points and accessing them:
http://www.vogella.de/articles/EclipseExtensionPoint/article.html
http://www.eclipsezone.com/eclipse/forums/t97608.rhtml
I ended up extending XMLMultiPageEditorPart like this:
public class MultiPageEditor extends XMLMultiPageEditorPart implements
IResourceChangeListener {
#Override
public void resourceChanged(IResourceChangeEvent event) {
// TODO Auto-generated method stub
setActivePage(3);
}
public Document getDOM() {
int activePageIndex = getActivePage();
setActivePage(1);
StructuredTextEditor fTextEditor = (StructuredTextEditor) getSelectedPage();
IDocument document = fTextEditor.getDocumentProvider().getDocument(
fTextEditor.getEditorInput());
IStructuredModel model = StructuredModelManager.getModelManager()
.getExistingModelForRead(document);
Document modelDocument = null;
try {
if (model instanceof IDOMModel) {
// cast the structured model to a DOM Model
modelDocument = (Document) (((IDOMModel) model).getDocument());
}
} finally {
if (model != null) {
model.releaseFromRead();
}
}
setActivePage(activePageIndex);
return modelDocument;
}
}
This is not a clean implementation, but it gets the job done.