new
This commit is contained in:
Binary file not shown.
@@ -69,7 +69,7 @@ public class ExprTool {
|
||||
if(ctx.normalClassDeclaration() != null){
|
||||
class_name = ctx.normalClassDeclaration().Identifier().getText();
|
||||
}else if(ctx.enumDeclaration() != null){
|
||||
class_name = ctx.normalClassDeclaration().Identifier().getText();
|
||||
class_name = ctx.enumDeclaration().Identifier().getText();
|
||||
}
|
||||
|
||||
//String className = ctx.identifier().getText();
|
||||
|
||||
@@ -11,7 +11,10 @@
|
||||
## JavaParser1.0.0
|
||||
```bash
|
||||
javac -cp .:javaparser-1.0.0.jar LCA_JP1_0_0.java
|
||||
java -cp .:javaparser-1.0.0.jar test.java
|
||||
java -cp .:javaparser-1.0.0.jar LCA_JP1_0_0 test.java
|
||||
```
|
||||
|
||||
## JavaParser3.27.0
|
||||
```bash
|
||||
javax
|
||||
```
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -4,56 +4,38 @@ import japa.parser.ast.body.*;
|
||||
import japa.parser.ast.visitor.VoidVisitorAdapter;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.File;
|
||||
|
||||
public class LCA_JP1_0_0 {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
// Checks if file is provided as command line argument
|
||||
if (args.length == 0) {
|
||||
System.out.println("Usage: java LCA_JP1_0_0 <JavaSourceFile>");
|
||||
return;
|
||||
}
|
||||
|
||||
// Makes sure file exits and can be read
|
||||
File sourFile = new File(args[0]);
|
||||
if (!sourFile.exists() || !sourFile.isFile() || !sourFile.canRead()) {
|
||||
System.err.println("Error: File can't be read or it doesn't exist: " + args[0]);
|
||||
return;
|
||||
FileInputStream in = new FileInputStream(new File(args[0]));
|
||||
CompilationUnit cu = JavaParser.parse(in);
|
||||
in.close();
|
||||
|
||||
LocalClassVisitor visitor = new LocalClassVisitor();
|
||||
visitor.visit(cu, null);
|
||||
}
|
||||
|
||||
static class LocalClassVisitor extends VoidVisitorAdapter<Void> {
|
||||
private boolean insideMethod = false;
|
||||
|
||||
@Override
|
||||
public void visit(MethodDeclaration n, Void arg) {
|
||||
insideMethod = true;
|
||||
super.visit(n, arg);
|
||||
insideMethod = false;
|
||||
}
|
||||
|
||||
try (FileInputStream in = new FileInputStream(sourFile))
|
||||
// Parse the given Java file
|
||||
{
|
||||
CompilationUnit cu;
|
||||
try {
|
||||
cu = JavaParser.parse(in);
|
||||
} catch (Exception e) {
|
||||
System.err.println("Error: Failed to parse file");
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
// Traverse the AST and print local classes
|
||||
new VoidVisitorAdapter<Void>() {
|
||||
|
||||
@Override
|
||||
public void visit(MethodDeclaration n, Void arg) {
|
||||
super.visit(n, arg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(ClassOrInterfaceDeclaration n, Void arg) {
|
||||
System.out.println("Found class: " + n.getName());
|
||||
super.visit(n, arg);
|
||||
}
|
||||
}.visit(cu, null);
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error: Unable to read the file.");
|
||||
e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
System.err.println("Uneaxpected error occured.");
|
||||
e.printStackTrace();
|
||||
@Override
|
||||
public void visit(ClassOrInterfaceDeclaration n, Void arg) {
|
||||
System.out.println("Local class: " + n.getName());
|
||||
super.visit(n, arg);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user