I watched the question about Eclipse -> Java -> Templates and how to create template to decrease code time development but I couldn't find how to:
generate template on Java class created
generate the template for some special Java classes which extend javax.swing.JPanel
for example how to generate code like :
public class AClass extends JPanel {
public AClass(){this.aMethod();}
private void aMethod(){}
}
...on Java class created?
EDIT :
I try to invoke template code generation on new java source file created (see image)
I tried to edit the preferences -> Java -> code templates constructor body (see image) but I am not sure how to insert method invoke into constructor body :(
I tried to input this.aMethod(); into the edit text area but this doesn't generate constructor body as : public AClass(){this.aMethod();}
for question A :
You can edit template through eclipse preferences :
Java code style > Code template
For question B :
You would have to create a custom new file wizard (a little overkill maybe...)
you can find a simple tutorial on vogella :
http://www.vogella.com/tutorials/EclipseWizards/article.html
Related
I have a Java application that is required to do a bunch of stuff to a PowerPoint file, one of which involves invoking an existing PowerPoint macro and passing parameters to it. Here are some sample code:
public static void main (String[] args) {
createPptFile("test");
/* Do stuff */
/* Call macro here */
/* Do more stuff */
}
Sub SaySomething(something)
MsgBox(something)
End Sub
The Java application first creates a new test.pptm file, then at some point later the application is supposed to call SaySomething(something) while passing a value to it. How can I do this? What libraries, if any, would I need? Please provide some sample code to show how this can be done.
The application is to be implemented using Java 1.8, and will run on Windows 10 and using Microsoft Office 2013. Apache POI is being used to edit PowerPoint files at present, though using it is not mandatory.
I want to add a sentence to all new classes that I create in java. By default Java entry point comes like this:
public static void main(String[] args) {
}
and I want to achive something like this:
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
}
Is this possible? Thank you!
Eclipse Java EE IDE for Web Developers.
Version: Mars Release (4.5.0)
Build id: 20150621-1200
You can create/edit/ any code template you want
For Eclipse go to
Window
Preferences
Java
Editor
Templates
More here:
http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fpreferences%2Fjava%2Feditor%2Fref-preferences-templates.htm
another here :
http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2FgettingStarted%2Fqs-EditorTemplates.htm
In Eclipse it is possible to edit built-in code templates (the one used by Eclipse e.g. for new class files) and create your own named snippets (for fast access to often-reused templates).
Code Templates
Go to Window > Preferences > Java > Code Style > Code Templates, then to Code > New Java files. Adapt this or some other template to suit your needs.
Templates
Usually it's better though to add a smaller template via Window > Preferences > Java > Editor > Templates and use it wherever you need. Smaller bricks are more reusable then hardcoded class templates. Those are applied with <type-beginning-of-template-name + ctrl + space.
I am working with Eclipse IDE. I want to develop a plug-in first of all, this plug-in allows me to get the whole source code as a simple string.
I made a simple "hello the world" plug-in using the template "hello world command". Now I am searching to get the source code from the editor of Eclipse and display it with System.out.println(); statement instead of showing HELLO THE WORLD.
I tried this but it shows me only the hierarchical sequence of my project, packageName/src/nameOfClass.
System.out.println(
Workbench.getInstance().getActiveWorkbenchWindow().getActivePage().getActiveEditor().getEditorInput()
);
My goal is to get the source code of the class itself (public class nameOfClass{ *** }).
First do not use Workbench this is an internal class and must not be used. Use PlatformUI to get the workbench.
IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
if (editor instanceof ITextEditor)
{
ITextEditor textEditor = (ITextEditor)editor;
IDocumentProvider provider = textEditor.getDocumentProvider();
IEditorInput input = editor.getEditorInput();
IDocument document = provider.getDocument(input);
String text = document.get();
...
}
Note: Not all editors are text editors so this needs to be checked (the ITextEditor instance check above).
I have a .java file which contains a class. I want to add a method to that class but I can't find a real useful "HOWTO" or examples around.
I'm using Eclipse and its JDT plugin for AST.
I tried a code that creates an ICompilationUnit from a project
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject("ProjName");
IJavaProject javaProject = JavaCore.create(project);
IPackageFragment package1 = javaProject.getPackageFragments()[0];
ICompilationUnit unit = package1.getCompilationUnits()[0];
then add a method with astrewrite.
But it seems to work only if I run all as a Plugin Project and not a simple Java Application.
I need to write an application in java that "simply" parse a java file and adds method to its class.
What I supposed to do is:
1) Create an ICompilationUnit directly form the .java file I want to parse (eventually located in my own project's directory)
2) Using another way
Both case I can't go further. Anyone can help me?
When you need to make a change by adding something to the compilation unit, you will have to use the functions provided by CompilationUnit to create new nodes.
To add a method to "unit" you will have to :
Create a MethodDeclaration node using your compilation unit :
MethodDeclaration md = unit.getAST().newMethodDeclaration();
Customize this method declaration to your requirements :
md.setName( unit.getAST().newSimpleName( "newMethod" ) );
md.setBody( unit.getAST().newBlock() );
this will produce : void newMethod() {}
Obtain the TypeBinding from "unit" :
TypeDeclaration typeDeclaration = ( TypeDeclaration )unit.types().get( 0 );
Add your newly created MethodDeclaration to the body declarations :
typeDeclaration.bodyDeclarations().add( md );
There's a method called getMethods() on TypeDeclaration but it doesn't return a live list of MethodDeclarations, therefore you can't modify that directly.
It's really easy to read the source file as text and replace the last } with the method declaration plus }. Obviously this doesn't work if someone puts multiple top-level classes in one file (which is extremely rare and I doubt you'll have a problem with that).
Could someone please help me to create rule in pmd for eclipse? I am unable to start even i followed through PMD official site. I am planning to create rule in java instead of XPath rule. Any simple guidelines to start it?
Thanks
Try the 'Write a rule using Java' method as you can easily start this way and later you can try with XPath expressions. You can follow these steps alongside the official link.
Start with the src package that comes with PMD (e.g. pmd-4.2.x\src), create your java class inside an existing package (e.g. pmd-4.2.5\src\net\sourceforge\pmd\rules\basic). In this case it is the following sample code (WhileLoopsMustUseBracesRule) :
package net.sourceforge.pmd.rules.basic;
import net.sourceforge.pmd.*;
import net.sourceforge.pmd.ast.*;
public class WhileLoopsMustUseBracesRule extends AbstractRule {
public Object visit(ASTWhileStatement node, Object data) {
SimpleNode firstStmt = (SimpleNode)node.jjtGetChild(1);
if (!hasBlockAsFirstChild(firstStmt)) {
addViolation(data, node);
}
return super.visit(node,data);
}
private boolean hasBlockAsFirstChild(SimpleNode node) {
return (node.jjtGetNumChildren() != 0 && (node.jjtGetChild(0) instanceof ASTBlock));
}
}
Append the following rule inside basic.xml(pmd-4.2.5\rulesets\basic.xml) :
Copy paste the xml content from "Put the WhileLoopsMustUseBracesRule rule in a ruleset file" section to basic.xml.
Replace the line
class="WhileLoopsMustUseBracesRule">
with
class="net.sourceforge.pmd.rules.basic.WhileLoopsMustUseBracesRule">
as you must have created the java file (WhileLoopsMustUseBracesRule.java) inside package "net.sourceforge.pmd.rules.basic"
Run this command from cmd prompt if in windows.
pmd.bat C:\JAVAFILE_ON_WHICH_YOU_WANT_TO_RUN_THIS_RULE xml C:\PMD\pmd-4.2.5\pmd-4.2.5\rulesets\basic.xml
For the nix os (linux/MacOS), use pmd.sh instead of pmd.bat
Once you are able to get it running, you can create your own rule inside your own package.
Hope this helps.!
I wrote a tutorial a while back with some sample code that could be helpful.
http://www.techtraits.ca/writting-pretty-code-with-pmd/