Skip to content
Sarah Abdelkhalek edited this page Aug 28, 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);

Methods

  • Add Import Statements
    Method: addImportStatment
    • Takes as an input the import statements to be added.
    • Returns String containing the code after the addition.
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 Class Body
    Method: getClassBody
    • Take as an input order(one-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(one-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{}" ;
parser p = new parser(x);
p.getMethodDeclerations();

/*
returns:
[public static void main
(
String[] args
) 

{, public static int m1
(
String s
) 

{, public static void m2
(
ArrayList
<
Integer
> 
a
) 

{, public static int m3
(
int n1
, 
int n2
) 

{]

*/
  • Get a Method Body
    Method: getMethodBody
    • Take as an input the order(one-indexed) of the Method.
    • Return the Methods's Body as a String.
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{}" ;
parser p = new parser(x);
p.getMethodBody(2);
/*
returns:
return 0
;

*/
  • Refactor/Transform Method Body
    Method: refactorMethodBody
    • Take as an input order(one-indexed) of the Method and the new body code.
    • Returns String containing the code after the transformation.
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{}" ;
parser p = new parser(x);
p.refactorMethodBody(2, "String x = \"NEW CODE\";"+p.getMethodBody(2));
/*
returns:
public class BubbleSort 
{	
public static void main
(
String[] args
) 

{	

}	
public static int m1
(
String s
) 

{

String x 
= 
"NEW CODE"
;
return 0
;
}	
public static void m2
(
ArrayList
<
Integer
> 
a
) 

{
return
}	
public static int m3
(
int n1
, 
int n2
) 

{
return 0
;

}

}
class InsertionSort
{
}

*/
  • Get the main Method Body
    Method: getMainMethodBody
    • Takes no input.
    • Return the Methods's Body as a String.
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{}" ;
parser p = new parser(x);
p.getMainMethodBody();
/*
returns:
String s 
= 
"HELLO WORLD"
;	

*/
  • Refactor/Transform the main Method Body
    Method: refactorMethodBody
    • Takes as an input the new body code.
    • Returns String containing the code after the transformation.
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{}" ;
parser p = new parser(x);
p.refactorMainMethodBody("String x = \"HELLO WORLD\";" + p.getMainMethodBody());
/*
returns:

public class BubbleSort 
{	
public static void main
(
String[] args
) 

{

String x 
= 
"HELLO WORLD"
;		

String s 

= 

"HELLO WORLD"

;	



}	
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
{
}


*/
  • Get all Array Assignments (i.e. arr[index] = value;)
    Method: getArrayAssignment
    • Takes no input.
    • Returns ArrayList of all Array assignments as Strings.
String x = "public class BubbleSort {" +
"	public static void main(String[] args) {"+
"	  	int arr [] = new int [3];"+
"		arr[0] = 0;"+
"		arr[1] = 1;"+
"		arr[2] = 2;"+
"	}" + 
"}" ;
parser p = new parser(x);
p.getArrayAssignment();
/*
returns:
[arr[0] 
= 
0
;, arr[1] 
= 
1
;, arr[2] 
= 
2
;]
*/
  • Refactor/Transform an Array Assignment
    Method: refactorArrayAssignment
    • Takes as an input the order(one-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.
String x = "public class BubbleSort {" +
"	public static void main(String[] args) {"+
"	}" + 
"}" ;
parser p = new parser(x);
p.refactorMainMethodBody("String x = \"HELLO WORLD\";");
/*
returns:
public class BubbleSort 
{	
public static void main
(
String[] args
) 

{

String x 
= 
"HELLO WORLD"
;

}
}
*/
Clone this wiki locally