Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » Problem writting source code in a .java file
Problem writting source code in a .java file [message #210454] Thu, 21 July 2005 04:19 Go to next message
Eclipse UserFriend
Originally posted by: david.nitenberg.gmail.com

Hi,

I am realizing a plugin that needs to parse an existing .java file in the
workspace, but I am having trouble to do it. One of my goals is to be able
to automatically generate the getters and setters in this .java file (I
don't want to go through the window that proposes which getters and
setters to generate; I want it to be transparent for the user).

I tried to call directly the AddGetterSetterOperation but it won't work.
So I thought I could just parse the .java file and generate the getters
setters by myself, but I don't know how to create an ASTParser for an
ICompilationUnit.

In fact, my problem is that I don't know how to write in a .java file to
add source code.

I hope my problem is clear :-)
Could anyone guide/help me in this objective, please ?

thanx in advance
Re: Problem writting source code in a .java file [message #210462 is a reply to message #210454] Thu, 21 July 2005 06:14 Go to previous messageGo to next message
Eclipse UserFriend
May be you don't need to using the AST model at all.

String content = "public void setName(String name) {this.name=name;}";

ICompilationUnit icu;
IType type = icu.getType(YOUR_CLASS_NAME);
type.createMethod(content, null, true, null);

Cheers

David wrote:
> Hi,
> I am realizing a plugin that needs to parse an existing .java file in
> the workspace, but I am having trouble to do it. One of my goals is to
> be able to automatically generate the getters and setters in this .java
> file (I don't want to go through the window that proposes which getters
> and setters to generate; I want it to be transparent for the user).
>
> I tried to call directly the AddGetterSetterOperation but it won't work.
> So I thought I could just parse the .java file and generate the getters
> setters by myself, but I don't know how to create an ASTParser for an
> ICompilationUnit.
>
> In fact, my problem is that I don't know how to write in a .java file to
> add source code.
>
> I hope my problem is clear :-)
> Could anyone guide/help me in this objective, please ?
>
> thanx in advance
>
Re: Problem writting source code in a .java file [message #210474 is a reply to message #210462] Thu, 21 July 2005 08:17 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: david.nitenberg.gmail.com

Thanx Eddie for the help. I tried to use your code but I still have a
problem :

Toto (not open) [in Toto.java [in fake [in src [in test]]]]
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
[...]

This happens with the following source code:

IWorkspaceRoot workspace = ResourcesPlugin.getWorkspace().getRoot();
IJavaModel javaModel = JavaCore.create(workspace);
IJavaProject javaProject = javaModel.getJavaProject("test");
IPackageFragmentRoot pa = javaProject.getPackageFragmentRoot("src");
IPackageFragment fra = pa.getPackageFragment("fake");
ICompilationUnit compUnit = fra.getCompilationUnit("Toto.java");

String content = "public void setName(String name) {this.name=name;}";

IType type = compUnit.getType("Toto");
System.out.println(type.toString());
try {
type.createMethod(content, null, true, null);
} catch (JavaModelException e1) {
e1.printStackTrace();
}

The exception occurs with the line type.createMethod...
I tried to open the file with the following statement :
compUnit.open(null);
but it didn't change anything.
Could you help me please ?
Re: Problem writting source code in a .java file [message #210619 is a reply to message #210474] Thu, 21 July 2005 21:17 Go to previous messageGo to next message
Eclipse UserFriend
Please try to this statement to getting the IPackageFragment:

IPackageFragment packageFragment = null;

IPackageFragmentRoot[] roots = javaProject.getAllPackageFragmentRoots();
for (int i=0;i<roots.length;i++)
if (!roots[i].isArchive()) {
IPackageFragment fragment = roots[i].getPackageFragment("fake");
if (fragment.exists()) {
packageFragment = fragment;
break;
}
}

ICompliationUnit icu =packageFragment.getCompliationUnit("Toto.java");
icu.getType("Toto").createMethod(......);




David wrote:
> Thanx Eddie for the help. I tried to use your code but I still have a
> problem :
>
> Toto (not open) [in Toto.java [in fake [in src [in test]]]]
> Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
> [...]
>
> This happens with the following source code:
>
> IWorkspaceRoot workspace = ResourcesPlugin.getWorkspace().getRoot();
> IJavaModel javaModel = JavaCore.create(workspace);
> IJavaProject javaProject = javaModel.getJavaProject("test");
> IPackageFragmentRoot pa = javaProject.getPackageFragmentRoot("src");
> IPackageFragment fra = pa.getPackageFragment("fake");
> ICompilationUnit compUnit = fra.getCompilationUnit("Toto.java");
>
> String content = "public void setName(String name) {this.name=name;}";
>
> IType type = compUnit.getType("Toto");
> System.out.println(type.toString());
> try {
> type.createMethod(content, null, true, null);
> } catch (JavaModelException e1) {
> e1.printStackTrace();
> }
>
> The exception occurs with the line type.createMethod...
> I tried to open the file with the following statement :
> compUnit.open(null);
> but it didn't change anything.
> Could you help me please ?
>
Re: Problem writting source code in a .java file [message #210627 is a reply to message #210619] Fri, 22 July 2005 05:28 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: david.nitenberg.gmail.com

Thanx a lot eddie, it finally worked :-)
Could you tell me why my source code didn't work and yours does ?
Re: Problem writting source code in a .java file [message #210748 is a reply to message #210627] Sun, 24 July 2005 22:17 Go to previous message
Eclipse UserFriend
IJavaProject.getPackageFragmentRoot(String jarPath);

This method is getting the jar file which have the given name, so your
code is getting the "src" jar file, not "src" folder.

David wrote:
> Thanx a lot eddie, it finally worked :-)
> Could you tell me why my source code didn't work and yours does ?
>
Previous Topic:add new statements without a newline
Next Topic:Help with JUnit
Goto Forum:
  


Current Time: Thu Jun 26 01:30:03 EDT 2025

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

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

Back to the top