Skip to content

Commit

Permalink
add createVisit to PPC
Browse files Browse the repository at this point in the history
  • Loading branch information
xiewenya committed Dec 21, 2016
1 parent fe63c28 commit 7dfb348
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 26 deletions.
Expand Up @@ -23,8 +23,6 @@

import com.github.javaparser.ast.Node;

import java.lang.reflect.Constructor;

/**
* Pretty printer for AST nodes.
*/
Expand All @@ -40,26 +38,8 @@ public PrettyPrinter(PrettyPrinterConfiguration configuration) {
}

public String print(Node node) {

final PrettyPrintVisitor visitor = getNewInstance(configuration);
final PrettyPrintVisitor visitor = configuration.createVisitor();
node.accept(visitor, null);
return visitor.getSource();
}

private PrettyPrintVisitor getNewInstance(PrettyPrinterConfiguration configuration){
try {
Class<? extends PrettyPrintVisitor> clazz = configuration.getVisitorClass();
if (clazz == null){
return new PrettyPrintVisitor(configuration);
}else{
Constructor<? extends PrettyPrintVisitor> constructor =
clazz.getConstructor(configuration.getClass());
return constructor.newInstance(configuration);
}
} catch (Exception e) {
assert false;
return null;
}
}

}
Expand Up @@ -21,10 +21,12 @@

package com.github.javaparser.printer;

import java.lang.reflect.Constructor;

public class PrettyPrinterConfiguration {
private boolean printComments = true;
private String indent = " ";
private Class<? extends PrettyPrintVisitor> visitorClass;
private Class<? extends PrettyPrintVisitor> visitorClass = PrettyPrintVisitor.class;

public String getIndent() {
return indent;
Expand All @@ -44,12 +46,19 @@ public PrettyPrinterConfiguration setPrintComments(boolean printComments) {
return this;
}

public Class<? extends PrettyPrintVisitor> getVisitorClass() {
return visitorClass;
}

public PrettyPrinterConfiguration setVisitorClass(Class<? extends PrettyPrintVisitor> visitorClass) {
this.visitorClass = visitorClass;
return this;
}

public PrettyPrintVisitor createVisitor() {
try {
Constructor<? extends PrettyPrintVisitor> constructor =
visitorClass.getConstructor(this.getClass());
return constructor.newInstance(this);
} catch (Exception e) {
assert false;
return null;
}
}
}

0 comments on commit 7dfb348

Please sign in to comment.