Skip to content

Instantly share code, notes, and snippets.

@nerzid
Created August 17, 2016 10:25
Show Gist options
  • Save nerzid/5e815bb4cb0fb13fdff2e0a8d71c852e to your computer and use it in GitHub Desktop.
Save nerzid/5e815bb4cb0fb13fdff2e0a8d71c852e to your computer and use it in GitHub Desktop.
this is old. It uses github.com/javaparser/javaparser . Problem was javadocs added and twice. You are using INRIA/spoon library right now
package com.nerzid.autocomment.generator;
import com.github.javaparser.ASTHelper;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseException;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.Parameter;
import com.github.javaparser.ast.body.TypeDeclaration;
import com.github.javaparser.ast.comments.JavadocComment;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import com.nerzid.autocomment.errorhandler.Errors;
import com.nerzid.autocomment.filehandler.FilePicker;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.nio.charset.StandardCharsets;
/**
*
* @author nerzid
*/
public class CommentGenerator {
public static ArrayList<File> files_list;
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), StandardCharsets.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());
// n.setJavaDocComment("Pls ol");
// n.setBlockComment("asdad");
//n.setLineComment("asdadad");
System.out.println("Javadoc: " +n.getJavaDoc().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
n.addParameter(newArg);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment