3

I want to create Java source code with code snippets that are possibly syntactically invalid. That's possible with com.sun.codemodel.JBlock#directStatement() method.

My problem is, that I want to modify existing (syntactically valid) source files and I can't find a way to create a JCodeModel from a file (or a String or a Stream etc.).

Is there a way to do this with JCodeModel?

(I could use Eclipse JDT/AST framework instead, but it apparently doesn't offer the option to add code snippets directly...)

Community
  • 1
  • 1
Morrandir
  • 595
  • 1
  • 4
  • 19
  • It is posiible to get a JClass object from a source file. Unfortunately I can't find a way to get a JDefinedClass from it. You can indeed get a JCodeModel from it, but this doesn't contain any JDefinedClass objects in its packages. – Morrandir Dec 13 '12 at 14:51
  • Have you tried this? http://stackoverflow.com/questions/2333866/editing-modifying-a-java-file-programmatically-not-the-class-file – pratikch Dec 25 '12 at 09:06
  • Can you please post the exact code into your question? – HackerMonkey Dec 25 '12 at 14:55
  • @pratikch As far as I can see, your link doesn't provide a solution. What exactly should I try? – Morrandir Dec 25 '12 at 15:46
  • @Hasanein Khafaji What code? Part of my problem is, that there doesn't seem to be any code for this. :) – Morrandir Dec 25 '12 at 15:47

3 Answers3

1

You can use JavaParser for an effective parsing of your code and tben with the results of the parsing, you can make the manipulations with JCodemodel.

0

IMHO an approach could be like this.

  • Try to see what you are trying to achieve with the

    existing (syntactically valid) source files

  • Then figure out if you can do it without modifying them by either inheritance or containment of the classes in generated classes. This will provide a clear separation in generated and already created code.

  • If not possible try using design patterns like visitor in your existing (syntactically valid) classes and inject your generated code class instance into it to accomplish the job.

  • Worst case - last option will be to really modify the existing classes.

-1

I would recommend you to not to use JAVA code manipulation (it is quite complicated to manipulate with JAVA code from program), but JAVA Bytecode manipulation. Three frequently used bytecode manipulators are:

In case you really need to manipulate with source code you can check Eclipse JDT and ASTRewrite class. Bellow is an example how to create method and

final ASTRewrite rewrite= new ASTRewrite(root);
root.accept(new ASTVisitor() {
public boolean visit(Assignment assignment) {
    // check if affected
    AST ast= assignment.getAST();
    MethodInvocation setter= ast.newMethodInvocation();
    setter.setName(ast.newSimpleName(setterName));
    setter.arguments().add(
    rewrite.createMoveTarget(assignment.getRightHandSide()));
    rewrite.replace(assignment, setter);
}
});

Tutorials:

Radim Burget
  • 1,456
  • 2
  • 20
  • 39
  • Unfortunately that's not an option. I creating educational software, where UML activity diagrams are translated into Java source code, which is a direct output/feedback for the students. – Morrandir Dec 25 '12 at 22:02
  • In this case Eclipse JDT's ASTRewrite can be an option. Please see the ectended answer. – Radim Burget Dec 26 '12 at 05:26
  • With JDT I have the problem with inserting syntactically invalid code: http://stackoverflow.com/questions/13801305/how-to-add-a-code-snippet-to-method-body-with-jdt-ast – Morrandir Dec 26 '12 at 08:48