Before
public class test {
public void deneme() {
System.out.println("莽ok g眉zel 艧eyler olacak");
}
private int euhe() {
System.out.println("莽枚艧陌眉臒");
return -1;
}
}
After
public class test {
public void DENEME(int value) {
System.out.println("莽ok g眉zel 艧eyler olacak");
}
/**/**nice comment there*/
*/
/**/**nice comment there*/
*/
private int changedMethodName(int value) {
System.out.println("莽枚艧陌眉臒");
return -1;
}
}
Code that does that
public static void main(String[] args) throws FileNotFoundException, IOException {
files_list = FilePicker.chooseAndGetJavaFiles();
if (files_list == null || files_list.isEmpty()) {
Errors.showError(Errors.COMMENT_GENERATOR_FILELIST_NULL_OR_EMPTY);
} else {
CompilationUnit cu = null;
files_list.get(0).setWritable(true);
FileInputStream in = new FileInputStream(files_list.get(0));
try {
//cu = JavaParser.parse(in);
cu = JavaParser.parse(files_list.get(0), "UTF-8");
} catch (ParseException ex) {
Logger.getLogger(CommentGenerator.class.getName()).log(Level.SEVERE, null, ex);
} finally {
in.close();
}
new MethodChangerVisitor().visit(cu, null);
TypeDeclaration asd;
System.out.println(cu.toString());
File modified = files_list.get(0);
Files.write(files_list.get(0).toPath(), Arrays.asList(cu.toString()), StandardCharsets.UTF_8);
}
}
private static class MethodChangerVisitor extends VoidVisitorAdapter {
@Override
public void visit(MethodDeclaration n, Object arg) {
// change the name of the method to upper case
if (n.getName().equals("deneme")) {
n.setName(n.getName().toUpperCase());
} else {
JavadocComment doc = n.getJavaDoc();
doc = new JavadocComment("nice comment there");
System.out.println("doccc: " + doc);
n.setName("changedMethodName");
n.setJavaDocComment(doc.toString());
}
System.out.println("javadoc: " + n.getJavaDoc());
// create the new parameter
Parameter newArg = ASTHelper.createParameter(ASTHelper.INT_TYPE, "value");
// add the parameter to the method
ASTHelper.addParameter(n, newArg);
}
}
Problem is , as you can see, it adds javadoc twice. Moreover, there is a gap between javadoc and the method decleration.
Also, Is there any options to javadoc properly ? I mean In Javadoc there are attributes like @param , @author and so on ?
The problem is not caused by the visitor.
Parsing and pretty printing this code:
public class Foo {
/** This line gets duplicated */
public void foo() {
}
}
using this program:
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.StringReader;
public class Test {
public static void main(String[] args) throws IOException, ParseException {
String content = new String(Files.readAllBytes(Paths.get("Foo.java")));
System.out.println(JavaParser.parse(new StringReader(content)));
}
}
yields:
public class Foo {
/** This line gets duplicated */
/** This line gets duplicated */
public void foo() {
}
}
Version used is 2.5.1.
Hello I have the same issue.
I think the duplicate comment issue come from the DumpVisitor or the class TypeDeclaration
I think the bug come from one the two file below :
DumpVisitor.java @353-354
printJavaComment(n.getComment(), arg);
printJavadoc(n.getJavaDoc(), arg);
or from the TypeDeclaration.java @129-134
@Override
public JavadocComment getJavaDoc() {
if(getComment() instanceof JavadocComment){
return (JavadocComment) getComment();
}
return null;
}
Yes, Javadoc uses the same field as comment, so we shouldn't print it twice. Thanks for figuring this out :)
Here's the explanation: before 2.5.0, the javadoc getter/setter was only half implemented, meaning that the getter would always return null. The code in DumpVisitor is much older and enthusiastically assumes it needs to print JavaDoc if it is there. But in 2.5.0 we finished JavaDoc support by making it an alias for the already existing comment field, and of course DumpVisitor starts to output that comment twice since getJavaDoc and getComment now return the same thing. Sadly there was no test in place.
Please look at the pull request above and +1 it if it is okay.
Most helpful comment
Here's the explanation: before 2.5.0, the javadoc getter/setter was only half implemented, meaning that the getter would always return null. The code in DumpVisitor is much older and enthusiastically assumes it needs to print JavaDoc if it is there. But in 2.5.0 we finished JavaDoc support by making it an alias for the already existing comment field, and of course DumpVisitor starts to output that comment twice since getJavaDoc and getComment now return the same thing. Sadly there was no test in place.
Please look at the pull request above and +1 it if it is okay.