-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJavaTranslationAspect.aj
More file actions
201 lines (178 loc) · 8.14 KB
/
Copy pathJavaTranslationAspect.aj
File metadata and controls
201 lines (178 loc) · 8.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package aspects;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import main.Dictionary;
import parser.PlSql_Parser;
@Aspect
public aspect JavaTranslationAspect {
List<String> code = new ArrayList<String>();
List<Integer> forIndexes = new ArrayList<Integer>();
boolean isInsideAssignment = false;
// Antes de la ejecución de CompilationUnit, escribimos todo
// el código anterior al bloque de la clase autogenerada.
@Before("(execution(* parser.PlSql_Parser.CompilationUnit()))")
public void beforeCompilationUnit(JoinPoint jp) {
String newCode = "public class " + PlSql_Parser.getClassName() + " {" + getIntro(1)
+ "public static void main(String args[]){"+ getIntro(1);
code.add(newCode);
consolePrint("@Before", "CompilationUnit", print2String(code), newCode);
}
// Después de la ejecución de ProcedureReference, tomamos las referencias,
// las traducimos usando el diccionario y las insertamos en el código.
@AfterReturning(pointcut="(execution(* PlSql_Parser.ProcedureReference(..)))",returning="name")
public void afterProcedureReference(String name) {
if(!name.equals("null") && name != null ) {
String newCode = Dictionary.ProcedureReference(name);
code.add(newCode);
consolePrint("@AfterReturning", "ProcedureReference", print2String(code), newCode);
}
}
// Después de la ejecución de ProcedureReference, abrimos el paréntesis
// que contiene los parámetros.
@After("(execution(* PlSql_Parser.ProcedureReference(..)))")
public void afterProcedureReference(JoinPoint jp) {
String newCode = "(";
code.add(newCode);
consolePrint("@After", "ProcedureReference", print2String(code), newCode);
}
// Después de la ejecución de Arguments, se traducen los parámetros
// usando el diccionario y se insertan.
@AfterReturning(pointcut="(execution(* PlSql_Parser.Arguments(..)))",returning="arguments")
public void afterArguments(String arguments) {
String newCode = Dictionary.Arguments(arguments);
code.add(newCode);
consolePrint("@AfterReturning", "Arguments", print2String(code), newCode);
}
// Después de la ejecución de ProcedureCall, se cierra el paréntesis
// que contiene los argumentos.
@After("(execution(* PlSql_Parser.ProcedureCall(..)))")
public void afterProcedureCall(JoinPoint jp) {
String newCode = ");" + getIntro(1);
code.add(newCode);
consolePrint("@After", "ProcedureCall", print2String(code), newCode);
}
// Después de la ejecución de CompilationUnit, cerramos la
// llave del bloque de la clase autogenerada.
@After("(execution(* PlSql_Parser.CompilationUnit(..)))")
public void afterCompilationUnit(JoinPoint jp) {
String newCode = getIntro(1) + "}" + getIntro(1) + " }";
code.add(newCode);
consolePrint("@After", "CompilationUnit", print2String(code), newCode);
PlSql_Parser.setCode(print2String(code));
}
// Antes de la ejecución de NumericForLoop, escribimos el principio del
// código del bucle for y almacenamos en la pila forIndexes el índice
// en el que lo hemos insertado en la lista code.
@Before("(execution(* parser.PlSql_Parser.NumericForLoop()))")
public void beforeNumericForLoop(JoinPoint jp) {
String newCode = "IntStream.range(";
code.add(newCode);
consolePrint("@Before", "NumericForLoop", print2String(code), newCode);
forIndexes.add(code.size()-1);
}
// Después de la ejecución de NumericForLoop, retiramos del código todo
// lo que está por debajo del índice del fragmento insertado en el @Before.
// Después, completamos el código del for y reinsertamos todo lo eliminado
// en el paso anterior. Por último, insertamos la llave de cierre del bucle.
@AfterReturning(pointcut="(execution(* PlSql_Parser.NumericForLoop(..)))", returning="arguments")
public void afterNumericForLoop(String[] arguments) {
int codeIndex = forIndexes.get(forIndexes.size()-1);
List<String> auxCode = new ArrayList<String>();
List<String> innerCode = new ArrayList<String>(code.subList(codeIndex+1, code.size()));
auxCode = code.subList(0, codeIndex+1);
String newCode = arguments[1] + ", " + arguments[2] + ").forEach("
+ getIntro(1) + arguments[0] + " -> {" + getIntro(1);
code = auxCode;
code.add(newCode);
System.out.println(code.toString());
addAll(code, innerCode);
newCode = getIntro(1) + "}";
code.add(newCode);
consolePrint("@AfterReturning", "NumericForLoop", print2String(code), newCode);
forIndexes.remove(forIndexes.size()-1);
}
// Después de VariableDeclaration, recogemos los valores necesarios e insertamos la línea correspondiente.
@AfterReturning(pointcut="(execution(* PlSql_Parser.VariableDeclaration(..)))", returning="arguments")
public void afterVariableDeclaration(String[] arguments) {
String dataType = Dictionary.dataType(arguments[1]);
String newCode = dataType + " " + arguments[0] + " = " + arguments[2] + ";" + getIntro(1);
code.add(newCode);
consolePrint("@AfterReturning", "VariableDeclaration", print2String(code), newCode);
}
// Antes de AssingmentStatement, se habilitan los advisors de
// PlSqlSimpleExpression y PlSqlMultiplicativeExpression.
@Before("(execution(* parser.PlSql_Parser.AssignmentStatement()))")
public void beforeAssignmentStatement(JoinPoint jp) {
isInsideAssignment = true;
}
// Después de Assingmentstatement, se captura el nombre de la variable que recibe
// el resultado y se coloca, seguida de "=", delante de los dos últimos token.
// Por último, se deshabilitan los advisors de PlSqlSimpleExpression y PlSqlMultiplicativeExpression.
@AfterReturning(pointcut="(execution(* parser.PlSql_Parser.AssignmentStatement()))", returning="variable")
public void afterAssignmentStatement(String variable) {
String temp = code.get(code.size()-1);
code = new ArrayList<String>(code.subList(0, code.size()-1));
String newCode = variable + " " + "=" + temp + ";" + getIntro(1);
code.add(newCode);
consolePrint("@AfterReturning", "AssignmentStatement", print2String(code), newCode);
isInsideAssignment = false;
}
// Después de PlSqlSimpleExpression se captura el operador de la expresión y se coloca entre los operandos.
@AfterReturning(pointcut="(execution(* PlSql_Parser.PlSqlSimpleExpression(..)))", returning="operation")
public void afterPlSqlSimpleExpression(String operation) {
if(isInsideAssignment) {
List<String> temp = new ArrayList<String>(code.subList(code.size()-2, code.size()));
code = new ArrayList<String>(code.subList(0, code.size()-2));
String newCode = temp.get(0) + " " + operation + " " + temp.get(1);
code.add(newCode);
consolePrint("@AfterReturning", "PlSqlSimpleExpression", print2String(code), newCode);
}
}
//Después de PlSqlMultiplicativeExpression se captura el operando de la expresión.
@AfterReturning(pointcut="(execution(* PlSql_Parser.PlSqlMultiplicativeExpression(..)))", returning="operator")
public void afterPlSQLMultiplicativeExpression(String operator) {
if(isInsideAssignment) {
String newCode = " " + operator + " ";
code.add(newCode);
consolePrint("@AfterReturning", "PlSqlMultiplicativeExpression", print2String(code), newCode);
}
}
///////////////////////// Auxiliary methods /////////////////////////////
private void consolePrint(String moment, String construct, String oldCode, String newCode) {
System.out.println(moment + " " + construct + ", inserting: " + getIntro(1) + newCode + getIntro(1));
System.out.println(oldCode + getIntro(1));
}
private String getIntro(int n) {
String tabs = "";
for(int i = 0; i<n; i++) {
tabs += "\r\n";
}
return tabs;
}
private void addAll(List<String> to, List<String> from) {
Iterator<String> iterator = from.iterator();
while(iterator.hasNext()) {
String item = iterator.next();
to.add(item);
}
}
private String print2String(List<String> code) {
String output = "";
Iterator<String> iterator = code.iterator();
while(iterator.hasNext()) {
String item = iterator.next();
output += item;
}
return output;
}
}