-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Sarah Abdelkhalek edited this page Aug 26, 2019
·
11 revisions
Welcome to the Java-Parser wiki!
first of all instantiate the parser object
- using input File
File file = new File("BubbleSort.java");
parser p=new parser(file);
- using input String
String s = "public class BubbleSort {}";
parser p=new parser(p);
- Add Import Statements
Method: addImportStatment
- Takes as an input the import statements to be added.
- Returns String containing the code after the addition.
- Takes as an input the import statements to be added.
String imports = "import java.io.BufferedReader;\r\n" +
"import java.io.File;\r\n"+
"import java.io.FileNotFoundException;\r\n" +
"import java.io.FileReader;\r\n";
p.addImportStatment(imports);
- Get All Classes Declarations
Method: getClasses- Takes no input.
- Returns ArrayList of all classes declerations as Strings.
String x = "public class BubbleSort{" +
" public static void main(String[] args) {"+
" }" +
"}" +
"class InsertionSort{}" ;
parser p=new parser(x);
//returns --> [public class BubbleSort{, class InsertionSort{]*/
- Get a Class Body
Method: getClassBody=- Take as an input order(zero-indexed) of the Class.
- Return the Class's Body as a String.
String x = "public class BubbleSort{" +
" public static void main(String[] args) {"+
" }" +
"}" +
"class InsertionSort{}" ;
parser p=new parser(x);
p.getClassBody(0);
/*
returns:
public static void main
(
String[] args
)
{
}
*/
- Refactor/Transform Class Body
Method: refactorClassBody- Take as an input the order(zero-indexed) of the Class and the new body code.
- Returns String containing the code after the transformation.
String x = "public class BubbleSort{" +
" public static void main(String[] args) {"+
" }" +
"}" +
"class InsertionSort{}" ;
parser p=new parser(x);
p.refactorClassBody(1, "String x=\"NEW CODE\";"+p.getClassBody(1));
/*
returns:
public class BubbleSort
{
String x
=
"NEW CODE"
;
public static void main
(
String[] args
)
{
}
}
class InsertionSort
{
}
*/
- Get All Constructor Declarations
Method: getClasses- Takes no input.
- Returns ArrayList of all constructors declerations as Strings.
p.getConstructorDecleration();
- Get All Methods Declarations
Method: getMethodDeclerations- Takes no input.
- Returns Array-list of all Methods declarations as Strings.
String x = "public class BubbleSort {" +
" public static void main(String[] args) {"+
" }" +
" public static int m1(String s) {return 0;}" +
" public static void m2(ArrayList<Integer> a) {return}" +
" public static int m3(int n1, int n2) {return 0;}"+
"}" +
"class InsertionSort{}" ;
p.getMethodDeclerations();
- Get a Method Body
Method: getMethodBody- Take as an input the order(zero-indexed) of the Method.
- Return the Methods's Body as a String.
p.getMethodBody(0);
- Refactor/Transform Method Body
Method: refactorMethodBody- Take as an input order(zero-indexed) of the Method and the new body code.
- Returns String containing the code after the transformation.
p.refactorMethodBody(0, "//the new code" + p.getMethodBody(0));
- Get the main Method Body
Method: getMainMethodBody- Take as an input the order(zero-indexed) of the Method.
- Return the Methods's Body as a String.
p.getMainMethodBody(0);
- Refactor/Transform the main Method Body
Method: refactorMethodBody- Takes as an input the new body code.
- Returns String containing the code after the transformation.
p.refactorMainMethodBody("//the new code" + p.getMainMethodBody(0));
- Get all Array Assignments (i.e. arr[index] = value;)
Method: getArrayAssignment- Takes no input.
- Returns ArrayList of all Array assignments as Strings.
p.getArrayAssignment();
- Refactor/Transform an Array Assignment
Method: refactorArrayAssignment- Takes as an input the order(zero-indexed) of the array Assignment instruction and the new code to be add instead of the chosen array assignment.
- Returns String containing the code after the transformation.
p.refactorArrayAssignment(0,p.getArrayAssignment().get(0)+"//the new code");