Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » Use MethodDeclaration manipulate code
Use MethodDeclaration manipulate code [message #179974] Wed, 29 September 2004 01:24 Go to next message
Eclipse UserFriend
Originally posted by: elwin.ho.nospam.hp.com

Hi,
I need to generate the following code in runtime:

/* (non-Javadoc)
* @see org.foo.Prim#sfDeploy()
*/
public synchronized void sfDeploy() throws FooException,
RemoteException {
// TODO Auto-generated method stub
super.sfDeploy();
}


I can use MethodDeclaration class to generate part of code already by
following the example in the online help. I couldn’t find the way to add
the “throws FooExcetpion” in the method declaration and the call the
“super.sfDeploy()”.

Thanks
Elwin

======= ================
MethodDeclaration methodDeclaration = ast.newMethodDeclaration();
methodDeclaration.setConstructor(false);
methodDeclaration.setModifiers(Modifier.PUBLIC|Modifier.SYNC HRONIZED);
methodDeclaration.setName(ast.newSimpleName("sfDeploy"));
methodDeclaration.setReturnType(ast.newPrimitiveType(Primiti veType.VOID));
org.eclipse.jdt.core.dom.Block block = ast.newBlock();
MethodInvocation methodInvocation = ast.newMethodInvocation();
methodDeclaration.setBody(block);
typeDeclaration.bodyDeclarations().add(methodDeclaration);
Re: Use MethodDeclaration manipulate code [message #180051 is a reply to message #179974] Wed, 29 September 2004 11:38 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: olivier_thomannNOSPAM.ca.ibm.com

Elwin a écrit :
> Hi,
> I need to generate the following code in runtime:
>
> /* (non-Javadoc)
> * @see org.foo.Prim#sfDeploy()
> */
> public synchronized void sfDeploy() throws FooException,
> RemoteException {
> // TODO Auto-generated method stub
> super.sfDeploy();
> }
>
>
> I can use MethodDeclaration class to generate part of code already by
> following the example in the online help. I couldn’t find the way to add
> the “throws FooExcetpion” in the method declaration and the call the
> “super.sfDeploy()”.
>
> Thanks
> Elwin
>
> ======= ================
> MethodDeclaration methodDeclaration = ast.newMethodDeclaration();
> methodDeclaration.setConstructor(false);
> methodDeclaration.setModifiers(Modifier.PUBLIC|Modifier.SYNC HRONIZED);
> methodDeclaration.setName(ast.newSimpleName("sfDeploy"));
> methodDeclaration.setReturnType(ast.newPrimitiveType(Primiti veType.VOID));
> org.eclipse.jdt.core.dom.Block block = ast.newBlock();
> MethodInvocation methodInvocation = ast.newMethodInvocation();
> methodDeclaration.setBody(block);
> typeDeclaration.bodyDeclarations().add(methodDeclaration);
You seem to use JLS2 API. You should move to JLS3.
Here is what I would do:
unit.recordModifications();
AST localAst = typeDeclaration.getAST();
MethodDeclaration methodDeclaration = localAst.newMethodDeclaration();
methodDeclaration.setConstructor(false);
methodDeclaration.setModifiers(Modifier.PUBLIC|Modifier.SYNC HRONIZED);
methodDeclaration.setName(localAst.newSimpleName("sfDeploy"));
methodDeclaration.setReturnType(localAst.newPrimitiveType(Pr imitiveType.VOID));
Block block = localAst.newBlock();
methodDeclaration.setBody(block);
SuperMethodInvocation superMethodInvocation =
localAst.newSuperMethodInvocation();
superMethodInvocation.setName(localAst.newSimpleName("sfDeploy "));
ExpressionStatement expressionStatement =
localAst.newExpressionStatement(superMethodInvocation);
block.statements().add(expressionStatement);
List throwsExceptions = methodDeclaration.thrownExceptions();
throwsExceptions.add(localAst.newSimpleName("FooException"));
throwsExceptions.add(localAst.newSimpleName("RemoteException "));
typeDeclaration.bodyDeclarations().add(methodDeclaration);
TextEdit edit = unit.rewrite(document, null);
try {
edit.apply(document);
} catch (MalformedTreeException e) {
e.printStackTrace();
} catch (BadLocationException e) {
e.printStackTrace();
}
// here the document contains the new method declaration
System.out.println(document.get());

You should use the JLS3 API as they are the one that handles 1.5 constructs.

Hope this help,

Olivier
Re: Use MethodDeclaration manipulate code [message #180399 is a reply to message #180051] Thu, 30 September 2004 12:25 Go to previous messageGo to next message
Eclipse UserFriend
What is 1.5ish about the code he wants to generate? Also note that JLS3
doesn't work in 3.0 so that will only help him in the future. He didn't say
where this was to be deployed....


"Olivier Thomann" <olivier_thomannNOSPAM@ca.ibm.com> wrote in message
news:cjekl7$gep$1@eclipse.org...
> Elwin a
Re: Use MethodDeclaration manipulate code [message #180538 is a reply to message #180399] Thu, 30 September 2004 16:52 Go to previous message
Eclipse UserFriend
Originally posted by: elwin_ho.nospam.hp.com

Hi,
Thanks both of you. The sample code did work for me. But now I have
another problem is that I cannot delete the file from the package
explorer.

The project I am working on need to manipulate the newly created skeleton
from java wizard. I extend the NewClassWizardPage and implemented the
createType() method, then use this.getCreatedType().getCompilationUnit()
to get the new created class to modify. Everything is working fine, until
the user try to delete this new Java file from package Explorer, after
they click “delete”, the file is removed from the editor area, but remain
in the package Explorer. If I click the file in Explorer, I will get
“File Deleted error: The file has been deleted from the file system. Do
you want to save your changes or close the editor without saving?”.
User doesn’t have any way to remove the file from package explorer. Look
like, I make some mistake that cause eclipse create a “ghost copy” of file
in memory. Below is the simplify code I use for this? Any suggestions?

Btw, I am using Eclipse 3.0, Windows XP and RedHat Linux

Thanks in advance.
Elwin



public class foo
extends NewClassWizardPage
{
….
public void createType(IProgressMonitor monitor)
throws CoreException, InterruptedException
{
super.createType(monitor);
ICompilationUnit cu = ( this.getCreatedType().getCompilationUnit()
);
cu.becomeWorkingCopy(null, null);


ASTParser parser = ASTParser.newParser(AST.JLS2);
parser.setSource(cu);

CompilationUnit astRoot = (CompilationUnit)parser.createAST(null);
AST ast = astRoot.getAST();
astRoot.recordModifications();

TypeDeclaration typeDeclaration =
(TypeDeclaration)astRoot.types().get(
0);
….
cu.commitWorkingCopy(false, new SubProgressMonitor(monitor, 1));
}







Jim Adams wrote:

> What is 1.5ish about the code he wants to generate? Also note that JLS3
> doesn't work in 3.0 so that will only help him in the future. He didn't say
> where this was to be deployed....


> "Olivier Thomann" <olivier_thomannNOSPAM@ca.ibm.com> wrote in message
> news:cjekl7$gep$1@eclipse.org...
> > Elwin a écrit :
> > > Hi,
> > > I need to generate the following code in runtime:
> > >
> > > /* (non-Javadoc)
> > > * @see org.foo.Prim#sfDeploy()
> > > */
> > > public synchronized void sfDeploy() throws FooException,
> > > RemoteException {
> > > // TODO Auto-generated method stub
> > > super.sfDeploy();
> > > }
> > >
> > >
> > > I can use MethodDeclaration class to generate part of code already by
> > > following the example in the online help. I couldn’t find the way to add
> > > the “throws FooExcetpion” in the method declaration and the call the
> > > “super.sfDeploy()”.
> > >
> > > Thanks
> > > Elwin
> > >
> > > ======= ================
> > > MethodDeclaration methodDeclaration = ast.newMethodDeclaration();
> > > methodDeclaration.setConstructor(false);
> > > methodDeclaration.setModifiers(Modifier.PUBLIC|Modifier.SYNC HRONIZED);
> > > methodDeclaration.setName(ast.newSimpleName("sfDeploy"));
> > >
> methodDeclaration.setReturnType(ast.newPrimitiveType(Primiti veType.VOID));
> > > org.eclipse.jdt.core.dom.Block block = ast.newBlock();
> > > MethodInvocation methodInvocation = ast.newMethodInvocation();
> > > methodDeclaration.setBody(block);
> > > typeDeclaration.bodyDeclarations().add(methodDeclaration);
> > You seem to use JLS2 API. You should move to JLS3.
> > Here is what I would do:
> > unit.recordModifications();
> > AST localAst = typeDeclaration.getAST();
> > MethodDeclaration methodDeclaration = localAst.newMethodDeclaration();
> > methodDeclaration.setConstructor(false);
> > methodDeclaration.setModifiers(Modifier.PUBLIC|Modifier.SYNC HRONIZED);
> > methodDeclaration.setName(localAst.newSimpleName("sfDeploy"));
> >
> methodDeclaration.setReturnType(localAst.newPrimitiveType(Pr imitiveType.VOID
> ));
> > Block block = localAst.newBlock();
> > methodDeclaration.setBody(block);
> > SuperMethodInvocation superMethodInvocation =
> > localAst.newSuperMethodInvocation();
> > superMethodInvocation.setName(localAst.newSimpleName("sfDeploy "));
> > ExpressionStatement expressionStatement =
> > localAst.newExpressionStatement(superMethodInvocation);
> > block.statements().add(expressionStatement);
> > List throwsExceptions = methodDeclaration.thrownExceptions();
> > throwsExceptions.add(localAst.newSimpleName("FooException"));
> > throwsExceptions.add(localAst.newSimpleName("RemoteException "));
> > typeDeclaration.bodyDeclarations().add(methodDeclaration);
> > TextEdit edit = unit.rewrite(document, null);
> > try {
> > edit.apply(document);
> > } catch (MalformedTreeException e) {
> > e.printStackTrace();
> > } catch (BadLocationException e) {
> > e.printStackTrace();
> > }
> > // here the document contains the new method declaration
> > System.out.println(document.get());
> >
> > You should use the JLS3 API as they are the one that handles 1.5
> constructs.
> >
> > Hope this help,
> >
> > Olivier
Previous Topic:Marker layers
Next Topic:What is the API to determine method location
Goto Forum:
  


Current Time: Sun Nov 09 14:03:18 EST 2025

Powered by FUDForum. Page generated in 0.03907 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top