Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support comments in method bodies #269

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public O removeMethod(final Method<O, ?> method)
@SuppressWarnings("unchecked")
public MethodSource<O> addMethod()
{
MethodSource<O> m = new MethodImpl<>((O) this);
MethodSource<O> m = new MethodImpl<>((O) this, document);
getDeclaration().bodyDeclarations().add(m.getInternal());
return m;
}
Expand All @@ -231,7 +231,7 @@ public MethodSource<O> addMethod()
@SuppressWarnings("unchecked")
public MethodSource<O> addMethod(final String method)
{
MethodSource<O> m = new MethodImpl<>((O) this, method);
MethodSource<O> m = new MethodImpl<>((O) this, method, document);
getDeclaration().bodyDeclarations().add(m.getInternal());
return m;
}
Expand All @@ -240,7 +240,7 @@ public MethodSource<O> addMethod(final String method)
@SuppressWarnings("unchecked")
public MethodSource<O> addMethod(java.lang.reflect.Method method)
{
MethodSource<O> m = new MethodImpl<>((O) this, method);
MethodSource<O> m = new MethodImpl<>((O) this, method, document);
getDeclaration().bodyDeclarations().add(m.getInternal());
return m;
}
Expand All @@ -249,7 +249,7 @@ public MethodSource<O> addMethod(java.lang.reflect.Method method)
@SuppressWarnings("unchecked")
public MethodSource<O> addMethod(Method<?, ?> method)
{
MethodSource<O> m = new MethodImpl<>((O) this, method.toString());
MethodSource<O> m = new MethodImpl<>((O) this, method.toString(), document);
getDeclaration().bodyDeclarations().add(m.getInternal());
return m;
}
Expand All @@ -266,7 +266,7 @@ public List<MethodSource<O>> getMethods()
List<MethodDeclaration> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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();
}

Expand Down Expand Up @@ -622,31 +621,31 @@ public Body removeMethod(final Method<Body, ?> method)
@Override
public MethodSource<Body> addMethod()
{
final MethodSource<Body> m = new MethodImpl<>(this);
final MethodSource<Body> m = new MethodImpl<>(this, document);
getBody().bodyDeclarations().add(m.getInternal());
return m;
}

@Override
public MethodSource<Body> addMethod(final String method)
{
final MethodSource<Body> m = new MethodImpl<>(this, method);
final MethodSource<Body> m = new MethodImpl<>(this, method, document);
getBody().bodyDeclarations().add(m.getInternal());
return m;
}

@Override
public MethodSource<Body> addMethod(java.lang.reflect.Method method)
{
final MethodSource<Body> m = new MethodImpl<>(this, method);
final MethodSource<Body> m = new MethodImpl<>(this, method, document);
getBody().bodyDeclarations().add(m.getInternal());
return m;
}

@Override
public MethodSource<Body> addMethod(Method<?, ?> method)
{
MethodSource<Body> m = new MethodImpl<>(this, method.toString());
MethodSource<Body> m = new MethodImpl<>(this, method.toString(), document);
getBody().bodyDeclarations().add(m.getInternal());
return m;
}
Expand All @@ -661,7 +660,7 @@ public List<MethodSource<Body>> getMethods()

for (MethodDeclaration methodDeclaration : methodFinderVisitor.getMethods())
{
result.add(new MethodImpl<>(this, methodDeclaration));
result.add(new MethodImpl<>(this, methodDeclaration, document));
}
return Collections.unmodifiableList(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,25 +28,24 @@ public class EnumConstantImpl implements EnumConstantSource
{
private final AnnotationAccessor<JavaEnumSource, EnumConstantSource> 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);
Expand All @@ -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
Expand Down Expand Up @@ -121,7 +123,7 @@ public EnumConstantSource setConstructorArguments(String... literalArguments)
@Override
public Body getBody()
{
return new EnumConstantBodyImpl(this);
return new EnumConstantBodyImpl(this, document);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public List<EnumConstantSource> 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);
Expand All @@ -50,7 +50,7 @@ public List<EnumConstantSource> getEnumConstants()
@SuppressWarnings("unchecked")
public EnumConstantSource addEnumConstant()
{
EnumConstantImpl enumConst = new EnumConstantImpl(this);
EnumConstantImpl enumConst = new EnumConstantImpl(this, document);
EnumDeclaration enumDeclaration = (EnumDeclaration) getDeclaration();
List<EnumConstantDeclaration> constants = enumDeclaration.enumConstants();
constants.add((EnumConstantDeclaration) enumConst.getInternal());
Expand All @@ -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<EnumConstantDeclaration> constants = enumDeclaration.enumConstants();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*
*/
public class JavaInterfaceImpl extends AbstractGenericCapableJavaSource<JavaInterfaceSource> implements
JavaInterfaceSource
Expand Down Expand Up @@ -50,7 +49,7 @@ public List<MethodSource<JavaInterfaceSource>> getMethods()
List<MethodDeclaration> methods = methodFinderVisitor.getMethods();
for (MethodDeclaration methodDeclaration : methods)
{
result.add(new JavaInterfaceMethodImpl(this, methodDeclaration));
result.add(new JavaInterfaceMethodImpl(this, methodDeclaration, document));
}
return Collections.unmodifiableList(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -18,9 +20,9 @@
public class JavaInterfaceMethodImpl extends MethodImpl<JavaInterfaceSource>
{

public JavaInterfaceMethodImpl(JavaInterfaceSource parent, Object internal)
public JavaInterfaceMethodImpl(JavaInterfaceSource parent, MethodDeclaration internal, Document document)
{
super(parent, internal);
super(parent, internal, document);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,35 +71,35 @@ protected RecordDeclaration getDeclaration()
List<MethodDeclaration> 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<JavaRecordSource> addMethod()
{
var m = new MethodImpl<>(this);
var m = new MethodImpl<>(this, document);
getDeclaration().bodyDeclarations().add(m.getInternal());
return m;
}

@Override public MethodSource<JavaRecordSource> 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<JavaRecordSource> 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<JavaRecordSource> 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;
}
Expand Down
Loading