@Override public <R> void visitCtBlock(CtBlock<R> block) { int i = 0; while (i < block.getStatements().size()) { int size = block.getStatements().size(); CtStatement s = block.getStatement(i); s.accept(this); if (block.getStatements().size() >= size) i++; } }
private static <T> void processConstructor(CtConstructor<T> c, CtClass<T> toMerge) { CtStatement firstStmt = c.getBody().getStatements().get(0); if (firstStmt instanceof CtInvocation) { CtInvocation<?> superConstructorCall = (CtInvocation) firstStmt; if (!(superConstructorCall.getExecutable().getDeclaration() instanceof CtConstructor)) return; CtConstructor superConstructor = (CtConstructor) superConstructorCall.getExecutable().getDeclaration(); if (superConstructor.getDeclaringType() == toMerge) { CtBlock superConstructorBody = c.getFactory().Core().clone(superConstructor.getBody()); superConstructorBody.accept( new CtScanner() { @Override public <T> void visitCtParameterReference(CtParameterReference<T> ref) { int parameterOrder = superConstructor.getParameters().indexOf(ref.getDeclaration()); ref.setDeclaringExecutable(c.getReference()); CtExpression<?> arg = superConstructorCall.getArguments().get(parameterOrder); if (!(arg instanceof CtVariableAccess)) throw sgce("super() should be directly called in " + c); CtVariable param = ((CtVariableAccess) arg).getVariable().getDeclaration(); if (!(param instanceof CtParameter)) throw sgce("super() should be directly called in " + c); ref.setSimpleName(param.getSimpleName()); super.visitCtParameterReference(ref); } }); c.getBody().removeStatement(firstStmt); List<CtStatement> superConstructorBodyStatements = superConstructorBody.getStatements(); for (int i = superConstructorBodyStatements.size() - 1; i >= 0; i--) { c.getBody().insertBegin(superConstructorBodyStatements.get(i)); } } } }
/** * Pretty print for the resulting degraded block * * @param clonedBody * @return */ public String prettyPrintBody(CtStatement clonedBody) { String result = ""; if (clonedBody instanceof CtBlock) { CtBlock block = (CtBlock) clonedBody; try { for (int i = 0; i < block.getStatements().size(); i++) { if (block.getStatement(i) instanceof CtBlock) result += prettyPrintBody(block.getStatement(i)); else if (block.getStatement(i) != null) result += block.getStatement(i).toString() + ";\n"; } } catch (NullPointerException ex) { log.error("Unable to print the degraded loop!"); } } else result = clonedBody.toString(); return result; }
protected List<CtLocalVariable> getLocalVarInScope(CtStatement stmt) { List<CtLocalVariable> vars = new ArrayList<>(); try { CtBlock parentBlock = stmt.getParent(CtBlock.class); if (parentBlock != null) { boolean beforeCurrentStmt = true; int i = 0; List<CtStatement> stmts = parentBlock.getStatements(); while (beforeCurrentStmt && i < stmts.size()) { CtStatement currentStatement = stmts.get(i); i++; beforeCurrentStmt = beforeCurrentStmt && currentStatement != stmt; if (currentStatement instanceof CtLocalVariable) { vars.add((CtLocalVariable) currentStatement); } } vars.addAll(getLocalVarInScope(parentBlock)); } } catch (Exception e) { throw new RuntimeException(e); } return vars; }