How to have several templates for Java files in Eclipse? - eclipse

I'd like to have several different templates for new java files in Eclipse. Is this possible?
Basically when I am creating a new Java class file in Package Explorer I'd like to use a template X as a base for new file, when I am creating a new unit test class I'd like to use template Y, when I am creating a Wicket component class I'd like to use template Z, etc.
Also is it possible to configure Eclipse so that when I create a new Java class, Eclipse will generate corresponding new unit test class automatically?

I am not sure if this is what you want, but you can add new wizards to Eclipse using the org.eclipse.ui.newWizards extension point.
Here is the corresponding documentation in Eclipse help.
Another (less elegant) way of doing this is to add your own templates (e.g. DAO) under the Java editor (Window->Preferences->Java->Editor->Templates) so when you type DAO and Ctrl + Enter it, it expands to the full template you want. This way creating a new empty file, typing DAO and autocompleting would transform into a full file snippet.
Hope it helps!

Related

Find in Eclipse all classes that are under specific package

I have project in which I have a package "com.mysite.Entities". Under this package I have several entities: "Class1.java","Class2.java","Class3.java". Currently in the names we don't have the string "Entity". And now I want to find what entities I have in this package. I want to write somewhere the package and as a result to have the 3 classes defined above.
Is this possible in Eclipse?
I don't want to find them with java code. What I need is just in some way to search in Eclipse IDE and to see in the UI that there are 3 classes, which were written under this package and to be able to select one of them and to open it into the Editor Window.
In Search > Java..., enter the search string com.mysite.Entities.*, search for Type and limit to Declarations.

Rename references at the same time in Eclipse

I have the following situation:
Consider that I have a class nested in a folder structure like for example /fol/der/exam/ple.java . Of course, the class ple.java contains package or import instuctions referencing to some sources. Now, when I choose Use as a source folder for /fol/der/exam/ple.java then, of course, the references used in package, import or function parameters are not the same anymore. I need to adjust them. Is there an elegant how to do that automatically in eclipse?
So, let´s assume that I have the following line in ple.java:
package fol.der.exam;
and that I have this line also in other files like a.java, b.java, etc. and I want to change the line, let´s say to package der.exam;
How can I do that automatically for all java files in eclipse?
best regards,

How to use xtext and xpand togheter

I have a rather silly question. I am using xtext to define a simple dsl and Xpand to generate code.
Now I want to use xpand to read my dsl models and generate code. How can i do that?
I read lot of tutorials saying that I can simple do that by creating a new xtext project from the wizard. Then, among others, the following project will be created:
org.xtext.example.domainmodel.generator.
And inside, the generator looks like this:
src
model (place the dsl model)
templates (place the template for the code generator)
workflow (workflow file)
Then i simple run the workflow and generate the code from the dsl file. That s straightforwards and easy.
My problem is that when I create a new xtext project I dont get the org.xtext.example.domainmodel.generator. project. I only have the following four:
org.xtext.example.domainmodel
org.xtext.example.domainmodel.sdk
org.xtext.example.domainmodel.tests
org.xtext.example.domainmodel.ui
So how can i produce the org.xtext.example.domainmodel.generator file???
Which tutorials do you mean? (links please) And do they really talk about a generator project? Talking about the package org.xtext.example.domainmodel.generator inside the org.xtext.example.domainmodel project is the usual thing.
Probably not relevant to the original person who asked this :) but I had a similar problem and the reason was that in my grammar extended xbase.
When extending xbase building the mwe2 skips the generator stub generation and uses the xbase code generation which is generated instead (the antlr parser, serializer etc.)

How do I include files with no extension for my Xtext DSL?

By default Xtext allows to specify a single extension for DSL files when creating a new project. However, it is possible to add more extensions for a single DSL as described in Xtext FAQ. But I couldn't get it working with files with no extension at all.
A typical example is a makefile for Make build system. One can use Makefile, GNUmakefile and *.mk names, and Eclipse will open the same editor for such files.
I want to get Xtext to recognize both *.mydsl files and a file named Mydsl.
I tried to add
filenames="Mydsl"
attribute to editor node of org.eclipse.ui.editors extension point in plugin.xml of my UI project. This enables Eclipse to open Mydsl files in the proper editor. But Xtext does not index these files and reports linking errors when I try to refer an element defined in Mydsl from any other file.
Is there a way to enable Xtext to process source files with fixed name but with no extension as well as regular files?
UPD. 1
Accordingly to Sebastian's answer I tried to specify a custom content type in plugin.xml of the main project:
<extension
point="org.eclipse.core.contenttype.contentTypes">
<content-type
base-type="org.eclipse.core.runtime.text"
file-extensions="mydsl"
file-names="Mydsl"
id="org.xtext.example.mydsl.contentType"
name="My Language"
priority="normal">
</content-type>
</extension>
And binding it as follows:
<extension
point="org.eclipse.xtext.content_resourceServiceProvider">
<resourceServiceProvider
class="org.xtext.example.mydsl.MyDslResourceServiceProvider"
contentTypeIdentifier="org.xtext.example.mydsl.contentType">
</resourceServiceProvider>
</extension>
But I still get linking errors as described above. I also added breakpoints into all methods of MyDslResourceServiceProvider and it seems that it doesn't even get instantiated or somehow invoked.
I also tried to move these extensions to the UI project but with no effect too.
UPD. 2
Finally, I've done it.
Steps to get things work in a nutshell:
Define new content type using org.eclipse.core.contenttype.contentTypes extension point
Create a content handler by extending org.eclipse.emf.ecore.resource.impl.PlatformContentHandlerImpl class and override canHandle(URI) method to return true if and only if the argument is not null
Register it with org.eclipse.emf.ecore.content_handler
Create a new resource service provider with canHandle(URI) returning true always. One can extend org.eclipse.xtext.resource.impl.DefaultResourceServiceProvider and override the corresponding method
In the UI project bind it to org.eclipse.xtext.content_resourceServiceProvider, do not forget to specify an extension factory before the class name
In the UI project register org.eclipse.xtext.resource.IResourceFactory as org.eclipse.emf.ecore.content_parser, again with the extension factory
Add content type bindings to org.eclipse.ui.editors, org.eclipse.compare.contentViewers and org.eclipse.compare.contentMergeViewers
Depending on whether you need an old extension binding or not, remove org.eclipse.emf.ecore.extension_parser and org.eclipse.xtext.extension_resourceServiceProvider extensions
A change set (applied to a fresh project) for my case can be found here.
You could try to exploit the extension point for resource service providers and resource factories. It allows to register services / factory either by file extension or by content type. I think the latter should work if you provide a suitable content type for your files.

Generating JUnit stubs for new methods in existing class in Eclipse

This question is tangentially related to How can I create JUnit stub methods in eclipse? but it doesn't answer my specific question.
Assuming you have an existing JUnit test case class, if you add a method to the target class (or choose to test a previously untested method), is there a way to tell Eclipse to generate the stub for the "new" method(s) in the existing TestCase without creating a new test case class?
Install the MoreUnit extension from the MarketPlace.
Open the package hierarchy panel.
Navigate down to the class that
you've modified.
Right click on the new method.
Select "Generate Test". The generated stub will appear in your ClassTest file.
My solution.
I simply go through the standard 'create JUnit test case'
Select file to test. -> New Junit test case
Go through the normal process in creating the test case, but only select those that you want new stubs for.
The file is created with the stubs, which I now copy into the existing test case file.
Delete the newly created test file class.
It's not the most efficient, but quicker than 'thick fingering' when you create them manually.
David
The usual working cycle with unit tests is to create the test case first
public void test_new_method() {
ClassUnderTest x = new ClassUnderTest();
x.NewMethod();
}
At that point Eclipse will give you a warning that the method "NewMethod" does not exist. Than you simply select a "Quick Fix" (Ctrl-1 or Ctrl-2 i'm not sure) to create the missing method in the class.