diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/AbstractJavaSourceMemberHolder.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/AbstractJavaSourceMemberHolder.java index c0c72e8a..e9cd771c 100644 --- a/impl/src/main/java/org/jboss/forge/roaster/model/impl/AbstractJavaSourceMemberHolder.java +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/AbstractJavaSourceMemberHolder.java @@ -222,7 +222,7 @@ public O removeMethod(final Method method) @SuppressWarnings("unchecked") public MethodSource addMethod() { - MethodSource m = new MethodImpl<>((O) this); + MethodSource m = new MethodImpl<>((O) this, document); getDeclaration().bodyDeclarations().add(m.getInternal()); return m; } @@ -231,7 +231,7 @@ public MethodSource addMethod() @SuppressWarnings("unchecked") public MethodSource addMethod(final String method) { - MethodSource m = new MethodImpl<>((O) this, method); + MethodSource m = new MethodImpl<>((O) this, method, document); getDeclaration().bodyDeclarations().add(m.getInternal()); return m; } @@ -240,7 +240,7 @@ public MethodSource addMethod(final String method) @SuppressWarnings("unchecked") public MethodSource addMethod(java.lang.reflect.Method method) { - MethodSource m = new MethodImpl<>((O) this, method); + MethodSource m = new MethodImpl<>((O) this, method, document); getDeclaration().bodyDeclarations().add(m.getInternal()); return m; } @@ -249,7 +249,7 @@ public MethodSource addMethod(java.lang.reflect.Method method) @SuppressWarnings("unchecked") public MethodSource addMethod(Method method) { - MethodSource m = new MethodImpl<>((O) this, method.toString()); + MethodSource m = new MethodImpl<>((O) this, method.toString(), document); getDeclaration().bodyDeclarations().add(m.getInternal()); return m; } @@ -266,7 +266,7 @@ public List> getMethods() List methods = methodFinderVisitor.getMethods(); for (MethodDeclaration methodDeclaration : methods) { - result.add(new MethodImpl<>((O) this, methodDeclaration)); + result.add(new MethodImpl<>((O) this, methodDeclaration, document)); } return Collections.unmodifiableList(result); } diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/EnumConstantBodyImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/EnumConstantBodyImpl.java index 37d15777..79cd72a8 100644 --- a/impl/src/main/java/org/jboss/forge/roaster/model/impl/EnumConstantBodyImpl.java +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/EnumConstantBodyImpl.java @@ -23,7 +23,6 @@ import org.jboss.forge.roaster.model.Field; import org.jboss.forge.roaster.model.JavaType; import org.jboss.forge.roaster.model.Method; -import org.jboss.forge.roaster.model.Parameter; import org.jboss.forge.roaster.model.SyntaxError; import org.jboss.forge.roaster.model.Type; import org.jboss.forge.roaster.model.Visibility; @@ -40,12 +39,9 @@ import org.jboss.forge.roaster.model.source.JavaSource; import org.jboss.forge.roaster.model.source.MemberSource; import org.jboss.forge.roaster.model.source.MethodSource; -import org.jboss.forge.roaster.model.source.ParameterSource; -import org.jboss.forge.roaster.model.util.Types; import org.jboss.forge.roaster.spi.JavaParserImpl; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -58,10 +54,13 @@ class EnumConstantBodyImpl implements EnumConstantSource.Body private final EnumConstantSource enumConstant; private final JavaEnumSource javaEnum; - EnumConstantBodyImpl(EnumConstantSource enumConstant) + private final Document document; + + EnumConstantBodyImpl(EnumConstantSource enumConstant, Document document) { this.enumConstant = enumConstant; this.javaEnum = enumConstant.getOrigin(); + this.document = document; getBody(); } @@ -622,7 +621,7 @@ public Body removeMethod(final Method method) @Override public MethodSource addMethod() { - final MethodSource m = new MethodImpl<>(this); + final MethodSource m = new MethodImpl<>(this, document); getBody().bodyDeclarations().add(m.getInternal()); return m; } @@ -630,7 +629,7 @@ public MethodSource addMethod() @Override public MethodSource addMethod(final String method) { - final MethodSource m = new MethodImpl<>(this, method); + final MethodSource m = new MethodImpl<>(this, method, document); getBody().bodyDeclarations().add(m.getInternal()); return m; } @@ -638,7 +637,7 @@ public MethodSource addMethod(final String method) @Override public MethodSource addMethod(java.lang.reflect.Method method) { - final MethodSource m = new MethodImpl<>(this, method); + final MethodSource m = new MethodImpl<>(this, method, document); getBody().bodyDeclarations().add(m.getInternal()); return m; } @@ -646,7 +645,7 @@ public MethodSource addMethod(java.lang.reflect.Method method) @Override public MethodSource addMethod(Method method) { - MethodSource m = new MethodImpl<>(this, method.toString()); + MethodSource m = new MethodImpl<>(this, method.toString(), document); getBody().bodyDeclarations().add(m.getInternal()); return m; } @@ -661,7 +660,7 @@ public List> getMethods() for (MethodDeclaration methodDeclaration : methodFinderVisitor.getMethods()) { - result.add(new MethodImpl<>(this, methodDeclaration)); + result.add(new MethodImpl<>(this, methodDeclaration, document)); } return Collections.unmodifiableList(result); } diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/EnumConstantImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/EnumConstantImpl.java index 2805261e..bb2414e5 100644 --- a/impl/src/main/java/org/jboss/forge/roaster/model/impl/EnumConstantImpl.java +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/EnumConstantImpl.java @@ -15,6 +15,7 @@ import org.eclipse.jdt.core.dom.EnumConstantDeclaration; import org.eclipse.jdt.core.dom.Expression; import org.eclipse.jdt.core.dom.Javadoc; +import org.eclipse.jface.text.Document; import org.jboss.forge.roaster.Roaster; import org.jboss.forge.roaster.model.Annotation; import org.jboss.forge.roaster.model.ast.AnnotationAccessor; @@ -27,25 +28,24 @@ public class EnumConstantImpl implements EnumConstantSource { private final AnnotationAccessor annotations = new AnnotationAccessor<>(); private JavaEnumSource parent; - private AST ast; + private final AST ast; private final EnumConstantDeclaration enumConstant; - private void init(final JavaEnumSource parent) + private final Document document; + + public EnumConstantImpl(final JavaEnumSource parent, Document document) { this.parent = parent; this.ast = ((ASTNode) parent.getInternal()).getAST(); - - } - - public EnumConstantImpl(final JavaEnumSource parent) - { - init(parent); this.enumConstant = ast.newEnumConstantDeclaration(); + this.document = document; } - public EnumConstantImpl(final JavaEnumSource parent, final String declaration) + public EnumConstantImpl(final JavaEnumSource parent, final String declaration, Document document) { - init(parent); + this.parent = parent; + this.ast = ((ASTNode) parent.getInternal()).getAST(); + this.document = document; String stub = "public enum Stub { " + declaration + " }"; JavaEnumSource temp = (JavaEnumSource) Roaster.parse(stub); @@ -55,10 +55,12 @@ public EnumConstantImpl(final JavaEnumSource parent, final String declaration) this.enumConstant = subtree; } - public EnumConstantImpl(final JavaEnumSource parent, final Object internal) + public EnumConstantImpl(final JavaEnumSource parent, final EnumConstantDeclaration internal, Document document) { - init(parent); - this.enumConstant = (EnumConstantDeclaration) internal; + this.parent = parent; + this.ast = ((ASTNode) parent.getInternal()).getAST(); + this.enumConstant = internal; + this.document = document; } @Override @@ -121,7 +123,7 @@ public EnumConstantSource setConstructorArguments(String... literalArguments) @Override public Body getBody() { - return new EnumConstantBodyImpl(this); + return new EnumConstantBodyImpl(this, document); } @Override diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/JavaEnumImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/JavaEnumImpl.java index e1254d73..468fc322 100644 --- a/impl/src/main/java/org/jboss/forge/roaster/model/impl/JavaEnumImpl.java +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/JavaEnumImpl.java @@ -40,7 +40,7 @@ public List getEnumConstants() for (Object o : ((EnumDeclaration) getDeclaration()).enumConstants()) { EnumConstantDeclaration constant = (EnumConstantDeclaration) o; - result.add(new EnumConstantImpl(this, constant)); + result.add(new EnumConstantImpl(this, constant, document)); } return Collections.unmodifiableList(result); @@ -50,7 +50,7 @@ public List getEnumConstants() @SuppressWarnings("unchecked") public EnumConstantSource addEnumConstant() { - EnumConstantImpl enumConst = new EnumConstantImpl(this); + EnumConstantImpl enumConst = new EnumConstantImpl(this, document); EnumDeclaration enumDeclaration = (EnumDeclaration) getDeclaration(); List constants = enumDeclaration.enumConstants(); constants.add((EnumConstantDeclaration) enumConst.getInternal()); @@ -62,7 +62,7 @@ public EnumConstantSource addEnumConstant() @SuppressWarnings("unchecked") public EnumConstantSource addEnumConstant(final String declaration) { - EnumConstantImpl enumConst = new EnumConstantImpl(this, declaration); + EnumConstantImpl enumConst = new EnumConstantImpl(this, declaration, document); EnumDeclaration enumDeclaration = (EnumDeclaration) getDeclaration(); List constants = enumDeclaration.enumConstants(); diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/JavaInterfaceImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/JavaInterfaceImpl.java index 43722221..5654ea7d 100644 --- a/impl/src/main/java/org/jboss/forge/roaster/model/impl/JavaInterfaceImpl.java +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/JavaInterfaceImpl.java @@ -6,10 +6,6 @@ */ package org.jboss.forge.roaster.model.impl; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - import org.eclipse.jdt.core.dom.BodyDeclaration; import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.MethodDeclaration; @@ -19,9 +15,12 @@ import org.jboss.forge.roaster.model.source.JavaSource; import org.jboss.forge.roaster.model.source.MethodSource; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + /** * @author Lincoln Baxter, III - * */ public class JavaInterfaceImpl extends AbstractGenericCapableJavaSource implements JavaInterfaceSource @@ -50,7 +49,7 @@ public List> getMethods() List methods = methodFinderVisitor.getMethods(); for (MethodDeclaration methodDeclaration : methods) { - result.add(new JavaInterfaceMethodImpl(this, methodDeclaration)); + result.add(new JavaInterfaceMethodImpl(this, methodDeclaration, document)); } return Collections.unmodifiableList(result); } diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/JavaInterfaceMethodImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/JavaInterfaceMethodImpl.java index 4526980d..7ef0ce96 100644 --- a/impl/src/main/java/org/jboss/forge/roaster/model/impl/JavaInterfaceMethodImpl.java +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/JavaInterfaceMethodImpl.java @@ -7,6 +7,8 @@ package org.jboss.forge.roaster.model.impl; +import org.eclipse.jdt.core.dom.MethodDeclaration; +import org.eclipse.jface.text.Document; import org.jboss.forge.roaster.model.Visibility; import org.jboss.forge.roaster.model.source.JavaInterfaceSource; @@ -18,9 +20,9 @@ public class JavaInterfaceMethodImpl extends MethodImpl { - public JavaInterfaceMethodImpl(JavaInterfaceSource parent, Object internal) + public JavaInterfaceMethodImpl(JavaInterfaceSource parent, MethodDeclaration internal, Document document) { - super(parent, internal); + super(parent, internal, document); } @Override diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/JavaRecordImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/JavaRecordImpl.java index 0d998951..095e650d 100644 --- a/impl/src/main/java/org/jboss/forge/roaster/model/impl/JavaRecordImpl.java +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/JavaRecordImpl.java @@ -71,35 +71,35 @@ protected RecordDeclaration getDeclaration() List methods = methodFinderVisitor.getMethods(); for (MethodDeclaration methodDeclaration : methods) { - result.add(new MethodImpl<>(this, methodDeclaration)); + result.add(new MethodImpl<>(this, methodDeclaration, document)); } return Collections.unmodifiableList(result); } @Override public MethodSource addMethod() { - var m = new MethodImpl<>(this); + var m = new MethodImpl<>(this, document); getDeclaration().bodyDeclarations().add(m.getInternal()); return m; } @Override public MethodSource addMethod(String method) { - var m = new MethodImpl<>(this, method); + var m = new MethodImpl<>(this, method, document); getDeclaration().bodyDeclarations().add(m.getInternal()); return m; } @Override public MethodSource addMethod(java.lang.reflect.Method method) { - var m = new MethodImpl<>(this, method); + var m = new MethodImpl<>(this, method, document); getDeclaration().bodyDeclarations().add(m.getInternal()); return m; } @Override public MethodSource addMethod(Method method) { - var m = new MethodImpl<>(this, method.toString()); + var m = new MethodImpl<>(this, method.toString(), document); getDeclaration().bodyDeclarations().add(m.getInternal()); return m; } diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/MethodImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/MethodImpl.java index 667f937e..0c48cebe 100644 --- a/impl/src/main/java/org/jboss/forge/roaster/model/impl/MethodImpl.java +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/MethodImpl.java @@ -6,17 +6,11 @@ */ package org.jboss.forge.roaster.model.impl; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; -import java.util.Objects; - import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.ASTParser; import org.eclipse.jdt.core.dom.Block; +import org.eclipse.jdt.core.dom.Comment; import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.Javadoc; import org.eclipse.jdt.core.dom.MethodDeclaration; @@ -26,6 +20,8 @@ import org.eclipse.jdt.core.dom.Statement; import org.eclipse.jdt.core.dom.TypeParameter; import org.eclipse.jdt.core.dom.VariableDeclaration; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.Document; import org.jboss.forge.roaster.ParserException; import org.jboss.forge.roaster.Problem; import org.jboss.forge.roaster.Roaster; @@ -44,9 +40,18 @@ import org.jboss.forge.roaster.model.source.MethodSource; import org.jboss.forge.roaster.model.source.ParameterSource; import org.jboss.forge.roaster.model.source.TypeVariableSource; +import org.jboss.forge.roaster.model.util.JDTOptions; import org.jboss.forge.roaster.model.util.Methods; import org.jboss.forge.roaster.model.util.Types; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Objects; + /** * @author Lincoln Baxter, III */ @@ -55,34 +60,34 @@ public class MethodImpl> implements MethodSource private final AnnotationAccessor> annotations = new AnnotationAccessor<>(); private final ModifierAccessor modifiers = new ModifierAccessor(); - private O parent = null; - private AST ast = null; - private CompilationUnit cu = null; + private final O parent; + private final AST ast; + private final CompilationUnit cu; private final MethodDeclaration method; + private final Document document; - private void init(final O parent) + public MethodImpl(final O parent, Document document) { this.parent = parent; - cu = (CompilationUnit) parent.getInternal(); - ast = cu.getAST(); + this.cu = (CompilationUnit) parent.getInternal(); + this.ast = cu.getAST(); + this.method = ast.newMethodDeclaration(); + this.method.setConstructor(false); + this.document = document; } - public MethodImpl(final O parent) + public MethodImpl(final O parent, final MethodDeclaration internal, Document document) { - init(parent); - method = ast.newMethodDeclaration(); - method.setConstructor(false); - } - - public MethodImpl(final O parent, final Object internal) - { - init(parent); - method = (MethodDeclaration) internal; + this.parent = parent; + this.cu = (CompilationUnit) parent.getInternal(); + this.ast = cu.getAST(); + this.document = document; + this.method = internal; } - public MethodImpl(final O parent, final Method reflectMethod) + public MethodImpl(final O parent, final Method reflectMethod, Document document) { - this(parent); + this(parent, document); // Set method visibility int mod = reflectMethod.getModifiers(); if (Modifier.isPublic(mod)) @@ -125,9 +130,12 @@ else if (Modifier.isPrivate(mod)) } } - public MethodImpl(final O parent, final String method) + public MethodImpl(final O parent, final String method, Document document) { - init(parent); + this.parent = parent; + this.cu = (CompilationUnit) parent.getInternal(); + this.ast = cu.getAST(); + this.document = document; String stub = "public class Stub { " + method + " }"; JavaClassSource temp = (JavaClassSource) Roaster.parse(stub); @@ -142,7 +150,7 @@ public String toSignature() StringBuilder signature = new StringBuilder(); signature.append(Visibility.PACKAGE_PRIVATE == this.getVisibility() ? "" : this.getVisibility() - .scope()); + .scope()); signature.append(" "); signature.append(this.getName()).append("("); List> parameters = this.getParameters(); @@ -255,6 +263,7 @@ public String getBody() } @Override + @SuppressWarnings("unchecked") public MethodSource setBody(final String body) { if (body == null) @@ -268,10 +277,12 @@ public MethodSource setBody(final String body) { throw new ParserException(problems); } - String stub = "public class Stub { public void method() {" + body + "} }"; - JavaClassSource temp = (JavaClassSource) Roaster.parse(stub); - List> methods = temp.getMethods(); - Block block = ((MethodDeclaration) methods.get(0).getInternal()).getBody(); + Document document = new Document(body); + ASTParser parser = ASTParser.newParser(AST.getJLSLatest()); + parser.setSource(document.get().toCharArray()); + parser.setCompilerOptions(JDTOptions.getJDTOptions()); + parser.setKind(ASTParser.K_STATEMENTS); + Block block = (Block) parser.createAST(null); block = (Block) ASTNode.copySubtree(method.getAST(), block); method.setBody(block); } @@ -791,7 +802,7 @@ public MethodSource removeTypeVariable(String name) { @SuppressWarnings("unchecked") List typeParameters = method.typeParameters(); - for (Iterator iter = typeParameters.iterator(); iter.hasNext();) + for (Iterator iter = typeParameters.iterator(); iter.hasNext(); ) { if (Objects.equals(name, iter.next().getName().getIdentifier())) { diff --git a/tests/src/test/java/org/jboss/forge/test/roaster/model/MethodBodyTest.java b/tests/src/test/java/org/jboss/forge/test/roaster/model/MethodBodyTest.java index 0aae4659..baf8c9be 100644 --- a/tests/src/test/java/org/jboss/forge/test/roaster/model/MethodBodyTest.java +++ b/tests/src/test/java/org/jboss/forge/test/roaster/model/MethodBodyTest.java @@ -43,16 +43,6 @@ public void testMethodBodyWithLambdas() assertEquals(body, method.getBody()); } - @Test - @Disabled("ROASTER-26") - public void testSetMethodBodyWithComments() - { - String data = "public class Foo { Object bar() {\n//TODO comments\n return null;}}"; - JavaClassSource source = Roaster.parse(JavaClassSource.class, data); - MethodSource method = source.getMethod("bar"); - assertEquals("//TODO comments\n return null;", method.getBody()); - } - @Test public void testBodyShouldBeSet() { @@ -66,4 +56,30 @@ public void testBodyShouldBeSet() assertNotNull(method.getBody()); assertThat(method.getBody()).isNotEmpty(); } + + @Test + @Disabled + public void testGetMethodBodyWithComments() + { + String data = "public class Foo { Object bar() {\n//TODO comments\n return null;}}"; + JavaClassSource source = Roaster.parse(JavaClassSource.class, data); + MethodSource method = source.getMethod("bar"); + assertThat(method.getBody()).isEqualTo("\n//TODO comments\n return null;"); + } + + @Test + @Disabled + public void testSetMethodBodyWithComments() { + JavaClassSource source = Roaster.create(JavaClassSource.class); + MethodSource method = source.addMethod(); + String body = "// this is a comment\n" + + "System.out.println(\"Success\");\n" + + "System.out.println(\"Success Again\");\n" + + "// this is another comment\n" + + "int a=21;\n" + + "System.out.println(a);"; + method.setName("myMethod").setReturnType(void.class).setBody(body); + assertThat(method.getBody()).isEqualTo(body); + } + } \ No newline at end of file