diff options
author | nickl- <github@jigsoft.co.za> | 2017-10-30 20:37:23 +0200 |
---|---|---|
committer | nickl- <github@jigsoft.co.za> | 2017-10-30 20:37:23 +0200 |
commit | 62851d0af30cdb0c54046627585451a468aacd2a (patch) | |
tree | fd7632ac39246a9598cb5bf6176c4bf0bcb21744 /src/main/javassist/compiler/CodeGen.java | |
parent | 45b4c55361eef93542db8014e3ef8941169c97bd (diff) | |
download | javassist-62851d0af30cdb0c54046627585451a468aacd2a.tar.gz javassist-62851d0af30cdb0c54046627585451a468aacd2a.zip |
Source walk/Spring clean/Parameterize/Enhance et.al.
The following were applied during multiple itterations through the source.
* Parameterize raw types.
* Mark unused members.
* Annotate override and deprecated methods.
* Convert loops to enhance for loop.
* Remove redundant else statements.
* Widening collection type references.
* Optimize code for improved readability.
* Squash compiler warnings.
* Identify smells.
Diffstat (limited to 'src/main/javassist/compiler/CodeGen.java')
-rw-r--r-- | src/main/javassist/compiler/CodeGen.java | 98 |
1 files changed, 57 insertions, 41 deletions
diff --git a/src/main/javassist/compiler/CodeGen.java b/src/main/javassist/compiler/CodeGen.java index ab02d8cf..b77ac2dc 100644 --- a/src/main/javassist/compiler/CodeGen.java +++ b/src/main/javassist/compiler/CodeGen.java @@ -18,10 +18,12 @@ package javassist.compiler; import java.util.ArrayList; import java.util.Arrays; +import java.util.List; + import javassist.compiler.ast.*; import javassist.bytecode.*; -/* The code generator is implemeted by three files: +/* The code generator is implemented by three files: * CodeGen.java, MemberCodeGen.java, and JvstCodeGen. * I just wanted to split a big file into three smaller ones. */ @@ -47,7 +49,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { */ public boolean inStaticMethod; - protected ArrayList breakList, continueList; + protected List<Integer> breakList, continueList; /** * doit() in ReturnHook is called from atReturn(). @@ -171,18 +173,16 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { if (dim == 0) return name; - else { - StringBuffer sbuf = new StringBuffer(); - int d = dim; - while (d-- > 0) - sbuf.append('['); + StringBuffer sbuf = new StringBuffer(); + int d = dim; + while (d-- > 0) + sbuf.append('['); - sbuf.append('L'); - sbuf.append(name); - sbuf.append(';'); + sbuf.append('L'); + sbuf.append(name); + sbuf.append(';'); - return sbuf.toString(); - } + return sbuf.toString(); } protected static String toJvmTypeName(int type, int dim) { @@ -242,16 +242,21 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { expr.accept(typeChecker); } + @Override public void atASTList(ASTList n) throws CompileError { fatal(); } - + + @Override public void atPair(Pair n) throws CompileError { fatal(); } + @Override public void atSymbol(Symbol n) throws CompileError { fatal(); } + @Override public void atFieldDecl(FieldDecl field) throws CompileError { field.getInit().accept(this); } + @Override public void atMethodDecl(MethodDecl method) throws CompileError { ASTList mods = method.getModifiers(); setMaxLocals(1); @@ -263,7 +268,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { inStaticMethod = true; } } - + ASTList params = method.getParams(); while (params != null) { atDeclarator((Declarator)params.head()); @@ -320,6 +325,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { protected abstract void insertDefaultSuperCall() throws CompileError; + @Override public void atStmnt(Stmnt st) throws CompileError { if (st == null) return; // empty @@ -417,10 +423,10 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { } private void atWhileStmnt(Stmnt st, boolean notDo) throws CompileError { - ArrayList prevBreakList = breakList; - ArrayList prevContList = continueList; - breakList = new ArrayList(); - continueList = new ArrayList(); + List<Integer> prevBreakList = breakList; + List<Integer> prevContList = continueList; + breakList = new ArrayList<Integer>(); + continueList = new ArrayList<Integer>(); ASTree expr = st.head(); Stmnt body = (Stmnt)st.tail(); @@ -454,19 +460,16 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { hasReturned = alwaysBranch; } - protected void patchGoto(ArrayList list, int targetPc) { - int n = list.size(); - for (int i = 0; i < n; ++i) { - int pc = ((Integer)list.get(i)).intValue(); + protected void patchGoto(List<Integer> list, int targetPc) { + for (int pc:list) bytecode.write16bit(pc, targetPc - pc + 1); - } } private void atForStmnt(Stmnt st) throws CompileError { - ArrayList prevBreakList = breakList; - ArrayList prevContList = continueList; - breakList = new ArrayList(); - continueList = new ArrayList(); + List<Integer> prevBreakList = breakList; + List<Integer> prevContList = continueList; + breakList = new ArrayList<Integer>(); + continueList = new ArrayList<Integer>(); Stmnt init = (Stmnt)st.head(); ASTList p = st.tail(); @@ -517,8 +520,8 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { private void atSwitchStmnt(Stmnt st) throws CompileError { compileExpr(st.head()); - ArrayList prevBreakList = breakList; - breakList = new ArrayList(); + List<Integer> prevBreakList = breakList; + breakList = new ArrayList<Integer>(); int opcodePc = bytecode.currentPc(); bytecode.addOpcode(LOOKUPSWITCH); int npads = 3 - (opcodePc & 3); @@ -583,8 +586,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { expr = TypeChecker.stripPlusExpr(expr); if (expr instanceof IntConst) return (int)((IntConst)expr).get(); - else - throw new CompileError("bad case label"); + throw new CompileError("bad case label"); } private void atBreakStmnt(Stmnt st, boolean notCont) @@ -672,6 +674,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { bc.addOpcode(MONITORENTER); ReturnHook rh = new ReturnHook(this) { + @Override protected boolean doit(Bytecode b, int opcode) { b.addAload(var); b.addOpcode(MONITOREXIT); @@ -711,7 +714,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { "sorry, cannot break/continue in synchronized block"); } - private static int getListSize(ArrayList list) { + private static int getListSize(List<Integer> list) { return list == null ? 0 : list.size(); } @@ -724,6 +727,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { return false; } + @Override public void atDeclarator(Declarator d) throws CompileError { d.setLocalVar(getMaxLocals()); d.setClassName(resolveClassName(d.getClassName())); @@ -745,10 +749,13 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { } } + @Override public abstract void atNewExpr(NewExpr n) throws CompileError; + @Override public abstract void atArrayInit(ArrayInit init) throws CompileError; + @Override public void atAssignExpr(AssignExpr expr) throws CompileError { atAssignExpr(expr, true); } @@ -931,6 +938,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { return false; } + @Override public void atCondExpr(CondExpr expr) throws CompileError { if (booleanExpr(false, expr.condExpr())) expr.elseExpr().accept(this); @@ -974,6 +982,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { return -1; } + @Override public void atBinExpr(BinExpr expr) throws CompileError { int token = expr.getOperator(); @@ -1136,7 +1145,6 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { arrayDim = 0; return true; } - else { int pc = bytecode.currentPc(); bytecode.addIndex(0); // correct later if (booleanExpr(isAndAnd, bexpr.oprand2())) @@ -1147,7 +1155,6 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { bytecode.addIndex(6); // skip GOTO instruction bytecode.addOpcode(Opcode.GOTO); } - } } else if (isAlwaysBranch(expr, branchIf)) { // Opcode.GOTO is not added here. The caller must add it. @@ -1207,8 +1214,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { if (type1 == NULL) return exprType; - else - return type1; + return type1; } private static final int ifOp[] = { EQ, IF_ICMPEQ, IF_ICMPNE, @@ -1390,6 +1396,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { bytecode.addOpcode(op); } + @Override public void atCastExpr(CastExpr expr) throws CompileError { String cname = resolveClassName(expr.getClassName()); String toClass = checkCastExpr(expr, cname); @@ -1403,6 +1410,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { bytecode.addCheckcast(toClass); } + @Override public void atInstanceOfExpr(InstanceOfExpr expr) throws CompileError { String cname = resolveClassName(expr.getClassName()); String toClass = checkCastExpr(expr, cname); @@ -1443,7 +1451,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { { if (srcType == destType) return; - + int op, op2; int stype = typePrecedence(srcType); int dtype = typePrecedence(destType); @@ -1475,6 +1483,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { bytecode.addOpcode(op2); } + @Override public void atExpr(Expr expr) throws CompileError { // array access, member access, // (unary) +, (unary) -, ++, --, !, ~ @@ -1483,7 +1492,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { ASTree oprand = expr.oprand1(); if (token == '.') { String member = ((Symbol)expr.oprand2()).get(); - if (member.equals("class")) + if (member.equals("class")) atClassObject(expr); // .class else atFieldRead(expr); @@ -1560,6 +1569,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { throw new CompileError("invalid type for " + expr.getName()); } + @Override public abstract void atCallExpr(CallExpr expr) throws CompileError; protected abstract void atFieldRead(ASTree expr) throws CompileError; @@ -1749,7 +1759,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { if (doDup && isPost) bytecode.addOpcode(DUP2); - bytecode.addLconst((long)1); + bytecode.addLconst(1); bytecode.addOpcode(token == PLUSPLUS ? LADD : LSUB); if (doDup && !isPost) bytecode.addOpcode(DUP2); @@ -1835,7 +1845,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { exprType = INT; } else if (t == LONG) { - bytecode.addLconst((long)1); + bytecode.addLconst(1); bytecode.addOpcode(token == PLUSPLUS ? LADD : LSUB); } else if (t == FLOAT) { @@ -1856,8 +1866,10 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { protected abstract void atFieldPlusPlus(int token, boolean isPost, ASTree oprand, Expr expr, boolean doDup) throws CompileError; + @Override public abstract void atMember(Member n) throws CompileError; + @Override public void atVariable(Variable v) throws CompileError { Declarator d = v.getDeclarator(); exprType = d.getType(); @@ -1887,6 +1899,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { } } + @Override public void atKeyword(Keyword k) throws CompileError { arrayDim = 0; int token = k.get(); @@ -1914,13 +1927,14 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { if (token == THIS) className = getThisName(); else - className = getSuperName(); + className = getSuperName(); break; default : fatal(); } } + @Override public void atStringL(StringL s) throws CompileError { exprType = CLASS; arrayDim = 0; @@ -1928,6 +1942,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { bytecode.addLdc(s.get()); } + @Override public void atIntConst(IntConst i) throws CompileError { arrayDim = 0; long value = i.get(); @@ -1942,6 +1957,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { } } + @Override public void atDoubleConst(DoubleConst d) throws CompileError { arrayDim = 0; if (d.getType() == DoubleConstant) { |