Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6150037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:33:15+00:00 2026-05-23T19:33:15+00:00

I’m writing a simple eclipse plugin, which is a code generator. User can choose

  • 0

I’m writing a simple eclipse plugin, which is a code generator. User can choose an existing method, then generate a new test method in corresponding test file, with JDT.

Assume the test files is already existed, and it’s content is:

public class UserTest extends TestCase {
    public void setUp(){}
    public void tearDown(){}
    public void testCtor(){}
}

Now I have generate some test code:

/** complex javadoc */
public void testSetName() {
    ....
    // complex logic
}

What I want to do is to append it to the existing UserTest. I have to code:

String sourceContent = FileUtils.readFileToString("UserTest.java", "UTF-8");
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(content.toCharArray());
CompilationUnit testUnit = (CompilationUnit) parser.createAST(null);

String newTestCode = "public void testSetName() { .... }";

// get the first type
final List<TypeDeclaration> list = new ArrayList<TypeDeclaration>();
testUnit .accept(new ASTVisitor() {
    @Override
    public boolean visit(TypeDeclaration node) {
        list.add(node);
        return false;
    }
});
TypeDeclaration type = list.get(0);

type.insertMethod(newTestCode); // ! no this method: insertMethod 

But there is no such a method insertMethod.

I know two options now:

  1. Do not use jdt, just to insert the new code to the test file, before last }
  2. use testUnit.getAST().newMethodDeclaration() to create a method, then update it.

But I don’t like these two options, I hope there is something like insertMethod, which can let me append some text to the test compilation unit, or convert the test code to a MethodDeclaration, then append to the test compilation unit.


UPDATE

I see nonty’s answer, and found there are two CompilationUnit in jdt. One is org.eclipse.jdt.internal.core.CompilationUnit, another is org.eclipse.jdt.core.dom.CompilationUnit. I used the second one, and nonty used the first one.

I need to supplement my question: At first I want to create a eclipse-plugin, but later I found it hard to create a complex UI by swt, so I decided to create a web app to generate the code. I copied those jdt jars from eclipse, so I can just use org.eclipse.jdt.core.dom.CompilationUnit.

Is there a way to use org.eclipse.jdt.internal.core.CompilationUnit outside eclipse?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:33:15+00:00Added an answer on May 23, 2026 at 7:33 pm

    What is wrong with

    ...
    String newTestCode = "public void testSetName() { .... }";
    IProgressMonitor monitor = new NullProgressMonitor();
    
    IMethod method = testUnit.getTypes[0].createMethod(newTestCode, null, false, monitor);
    

    You can even specify where to add the new method and whether to “overwrite” any exiting method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm making a simple page using Google Maps API 3. My first. One marker
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.