List of usage examples for org.eclipse.jdt.core.dom Expression delete
public final void delete()
From source file:org.decojer.cavaj.transformers.TrExpressions.java
License:Open Source License
/** * Rewrite PUT-access to a class (static) or instance field. * * @param bb//from www . ja v a 2s . c om * current BB * @param f * lass (static) or instance field * @param rightOperand * right operand expression * @return {@code true} - success */ private boolean rewriteFieldInit(@Nonnull final BB bb, @Nonnull final F f, @Nonnull final Expression rightOperand) { if (!isFieldInit()) { return false; } if (!f.isDeclaration()) { return false; } final T ownerT = getM().getT(); if (ownerT == null) { return false; } if (!ownerT.is(f.getT())) { return false; } // set local field, could be initializer if (f.isStatic()) { if (!getM().isInitializer()) { return false; } if (getCfg().getStartBb() != bb || bb.getStmts() > 0) { return false; } if (f.getAstNode() == null) { // synthetic has been recognized before in TrJvmStruct2JavaAst, ignore assignment return true; } if (f.isEnum() && f.getT().isEnum() && !getCfg().getCu().check(DFlag.IGNORE_ENUM)) { // assignment to enum constant declaration if (!(rightOperand instanceof ClassInstanceCreation)) { log.warn(getM() + ": Enum field initialization for '" + f + "' has no class instance creation as operand!"); return false; } final ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation) rightOperand; // first two arguments must be String (== field name) and int (ordinal) final List<Expression> arguments = classInstanceCreation.arguments(); if (arguments.size() < 2) { log.warn(getM() + ": Enum field initialization for '" + f + "' has less than 2 arguments!"); return false; } if (!(arguments.get(0) instanceof StringLiteral)) { log.warn(getM() + ": Enum field initialization for '" + f + "' has no string literal as first parameter!"); return false; } final String literalValue = ((StringLiteral) arguments.get(0)).getLiteralValue(); if (!literalValue.equals(f.getName())) { log.warn(getM() + ": Enum field initialization for '" + f + "' has no string literal equal to field name as first parameter!"); return false; } if (!(arguments.get(1) instanceof NumberLiteral)) { log.warn(getM() + ": Enum field initialization for '" + f + "' has no number literal as first parameter!"); return false; } final Object astNode = f.getAstNode(); if (!(astNode instanceof EnumConstantDeclaration)) { log.warn(getM() + ": Enum field initialization for '" + f + "' has no EnumConstantDeclaration as AST node!"); return false; } final EnumConstantDeclaration enumConstantDeclaration = (EnumConstantDeclaration) astNode; for (int i = arguments.size(); i-- > 2;) { final Expression e = arguments.get(i); e.delete(); enumConstantDeclaration.arguments().add(0, e); } final AnonymousClassDeclaration anonymousClassDeclaration = classInstanceCreation .getAnonymousClassDeclaration(); if (anonymousClassDeclaration != null) { final Element declarationForNode = f.getT().getCu() .getDeclarationForNode(anonymousClassDeclaration); if (declarationForNode == null) { log.warn(getM() + ": Enum field initialization for '" + f + "' with anonymous declaration has no declaration node!"); } else { anonymousClassDeclaration.delete(); enumConstantDeclaration.setAnonymousClassDeclaration(anonymousClassDeclaration); // normally contains one constructor, that calls a synthetic super // constructor with the enum class as additional last parameter, // this may contain field initializers, that we must keep, // so we can only remove the constructor in final merge (because // anonymous inner classes cannot have visible Java constructor) declarationForNode.setDeclarationOwner(f); } } return true; } } else { if (!getM().isConstructor()) { return false; } if (!(bb.peek() instanceof ThisExpression)) { return false; } if (getCfg().getStartBb() != bb || bb.getStmts() > 1 || bb.getStmts() == 1 && !(bb.getStmt(0) instanceof SuperConstructorInvocation)) { // initial super(<arguments>) is allowed for constructors return false; } if (f.getAstNode() == null) { // synthetic has been recognized before in TrJvmStruct2JavaAst, ignore assignment bb.pop(); return true; } // multiple constructors with different signatures possible, all of them // contain the same field initializer code after super() - simply overwrite } final Object astNode = f.getAstNode(); if (!(astNode instanceof FieldDeclaration)) { log.warn(getM() + ": Field initialization for '" + f + "' has no FieldDeclaration as AST node!"); return false; } ((VariableDeclarationFragment) ((FieldDeclaration) astNode).fragments().get(0)) .setInitializer(wrap(rightOperand, Priority.ASSIGNMENT)); // TODO move anonymous TD to FD as child!!! important for ClassEditor // select, if fixed change ClassEditor#findDeclarationForJavaElement too if (!f.isStatic()) { bb.pop(); } return true; }
From source file:org.jboss.forge.roaster.model.impl.expressions.OperatorImpl.java
License:Open Source License
protected void wireArg(Argument<O, OperatorExpression<O, P>, ?> arg, AST ast) { Expression child = wireAndGetExpression(arg, this, ast); if (child.getNodeType() == ASTNode.INFIX_EXPRESSION) { if (!expr.getOperator().equals(((InfixExpression) child).getOperator())) { ParenthesizedExpression par = ast.newParenthesizedExpression(); par.setExpression(child);/* w w w .j a v a2 s .c om*/ child = par; } } InfixExpression infix = expr; if ("MISSING".equals(infix.getLeftOperand().toString())) { infix.setLeftOperand(child); } else if ("MISSING".equals(infix.getRightOperand().toString())) { infix.setRightOperand(child); } else { org.eclipse.jdt.core.dom.Expression prev = infix.getRightOperand(); InfixExpression more = ast.newInfixExpression(); more.setOperator(infix.getOperator()); infix.setRightOperand(more); prev.delete(); more.setLeftOperand(prev); more.setRightOperand(child); } }