Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java template for a project in Eclipse

Tags:

java

eclipse

Is it possible to make creation of (Java) file(s) in Eclipse easier/quicker.

I know there is that "Create new class wizard", but it is slow for my "special" needs...

I have a specific project in which I'm creating new classes often, but the structure for those classes is the same. Let say I want to create class A, so I want file A.java to be created as

class A {
    public static void main(String[] args) {
    }
    static int solve() {
    }
}

and it would be perfect that also ATest.java is created for this class, for example

class ATest {
    @Test
    int test1() {
        Assert.assertEquals(0, A.solve());
    }
}

or is there such plugin for Eclipse?

like image 858
Betlista Avatar asked Oct 21 '22 04:10

Betlista


2 Answers

Yes, you can try using FastCode Plugin, where you can create new templates as per your requirement. As FastCode plugin supports custom templates, here is an example how to create the above class and test class together:

<template name="CREATE_NEW_CLASS">
        <description>Used to Create class in the specified package.</description>
        <allowed-file-names>*.*</allowed-file-names>
        <first-template-item>package</first-template-item>
        <second-template-item>none</second-template-item>
        <additional-parameters>className</additional-parameters>
        <template-body>
            <![CDATA[
            <fc:class type="class" name="${className}" package="${package.name}" project="${package.javaProject}">
            public class ${className} {
            public static void main(String[] args) {
                }
                static int solve() {
                }
            }
            </fc:class>
            <fc:class type="test" name="${className}Test" package="${package.name}" project="${package.javaProject}">
            public class ${className}Test {
            @Test
                int test1(){
                    Assert.assertEquals(0, ${className}.solve());
                }
            }
            </fc:class>
        ]]>
    </template-body>
</template>

Once you add the template, you need to do import using import option in template preference page as explained in the document.

like image 195
LeenaLatesh Avatar answered Oct 22 '22 18:10

LeenaLatesh


Yes, this is relatively simple to set up. Open the Project Properties and navigate to the Java Code Style > Code Templates section. Once there, check the box to enable project-specific settings. The generation template you want is under the Code part of the tree; you want Class Body. It is probably empty, but click the Edit... button to modify it.

enter image description here

Whatever you enter in the Edit dialog will be inserted between the class' brackets when using the New Class wizard.

There's no way I know of to automatically create another class (the Test in your case). But Eclipse has a JUnit wizard that makes doing so very easy. Just right-click on a class and choose New > Other... and then find Junit Test Case in the list. That wizard will guide you through creating the test class, including selecting the method(s) you want to test.

Note: these instructions set up the template for just the project or projects you select. You could also set up the same thing for your entire workspace Preferences, but doing so provides no way to share that configuration so that the same project checked out into another workspace will use it. I usually recommend making these kinds of settings on a per-project basis.

like image 30
E-Riz Avatar answered Oct 22 '22 17:10

E-Riz