List of usage examples for org.eclipse.jdt.core.dom ExpressionStatement setExpression
public void setExpression(Expression expression)
From source file:com.google.devtools.j2cpp.translate.Rewriter.java
License:Open Source License
/** * Rewrites System.out and System.err println calls as NSLog calls. * * @return true if the node was rewritten *///from w w w .j av a2 s. c o m // TODO(user): remove when there is iOS console support. @SuppressWarnings("unchecked") private boolean rewriteSystemOut(MethodInvocation node) { Expression expression = node.getExpression(); if (expression instanceof Name) { Name expr = (Name) node.getExpression(); IBinding binding = expr.resolveBinding(); if (binding instanceof IVariableBinding) { IVariableBinding varBinding = (IVariableBinding) binding; ITypeBinding type = varBinding.getDeclaringClass(); if (type == null) { return false; } String clsName = type.getQualifiedName(); String varName = varBinding.getName(); if (clsName.equals("java.lang.System") && (varName.equals("out") || varName.equals("err"))) { // Change System.out.* or System.err.* to NSLog AST ast = node.getAST(); MethodInvocation newInvocation = ast.newMethodInvocation(); IMethodBinding methodBinding = new IOSMethodBinding("NSLog", Types.getMethodBinding(node), null); Types.addBinding(newInvocation, methodBinding); Types.addFunction(methodBinding); newInvocation.setName(ast.newSimpleName("NSLog")); Types.addBinding(newInvocation.getName(), methodBinding); newInvocation.setExpression(null); // Insert NSLog format argument List<Expression> args = node.arguments(); if (args.size() == 1) { Expression arg = args.get(0); arg.accept(this); String format = getFormatArgument(arg); StringLiteral literal = ast.newStringLiteral(); literal.setLiteralValue(format); Types.addBinding(literal, ast.resolveWellKnownType("java.lang.String")); newInvocation.arguments().add(literal); // JDT won't let nodes be re-parented, so copy and map. ASTNode newArg = NodeCopier.copySubtree(ast, arg); if (arg instanceof MethodInvocation) { IMethodBinding argBinding = ((MethodInvocation) arg).resolveMethodBinding(); if (!argBinding.getReturnType().isPrimitive()) { IOSMethodBinding newBinding = new IOSMethodBinding("format", argBinding, Types.getNSString()); Types.addMappedInvocation((MethodInvocation) newArg, newBinding); } } newInvocation.arguments().add(newArg); } else if (args.size() > 1 && node.getName().getIdentifier().equals("printf")) { newInvocation.arguments().addAll(NodeCopier.copySubtrees(ast, args)); } else if (args.size() == 0) { // NSLog requires a format string. StringLiteral literal = ast.newStringLiteral(); literal.setLiteralValue(""); Types.addBinding(literal, ast.resolveWellKnownType("java.lang.String")); newInvocation.arguments().add(literal); } // Replace old invocation with new. ASTNode parent = node.getParent(); if (parent instanceof ExpressionStatement) { ExpressionStatement stmt = (ExpressionStatement) parent; stmt.setExpression(newInvocation); } else { throw new AssertionError("unknown parent type: " + parent.getClass().getSimpleName()); } return true; } } } return false; }
From source file:java5totext.input.JDTVisitor.java
License:Open Source License
@Override public void endVisit(org.eclipse.jdt.core.dom.ExpressionStatement node) { ExpressionStatement element = (ExpressionStatement) this.binding.get(node); this.initializeNode(element, node); if (this.binding.get(node.getExpression()) != null) element.setExpression((Expression) this.binding.get(node.getExpression())); }
From source file:org.eclipse.jdt.core.dom.ASTConverter.java
License:Open Source License
public Statement convert(org.eclipse.jdt.internal.compiler.ast.Statement statement) { if (statement instanceof ForeachStatement) { return convert((ForeachStatement) statement); }//from w ww .j a va 2s .c om if (statement instanceof org.eclipse.jdt.internal.compiler.ast.LocalDeclaration) { org.eclipse.jdt.internal.compiler.ast.LocalDeclaration localDeclaration = (org.eclipse.jdt.internal.compiler.ast.LocalDeclaration) statement; return convertToVariableDeclarationStatement(localDeclaration); } if (statement instanceof org.eclipse.jdt.internal.compiler.ast.AssertStatement) { return convert((org.eclipse.jdt.internal.compiler.ast.AssertStatement) statement); } if (statement instanceof org.eclipse.jdt.internal.compiler.ast.Block) { return convert((org.eclipse.jdt.internal.compiler.ast.Block) statement); } if (statement instanceof org.eclipse.jdt.internal.compiler.ast.BreakStatement) { return convert((org.eclipse.jdt.internal.compiler.ast.BreakStatement) statement); } if (statement instanceof org.eclipse.jdt.internal.compiler.ast.ContinueStatement) { return convert((org.eclipse.jdt.internal.compiler.ast.ContinueStatement) statement); } if (statement instanceof org.eclipse.jdt.internal.compiler.ast.CaseStatement) { return convert((org.eclipse.jdt.internal.compiler.ast.CaseStatement) statement); } if (statement instanceof org.eclipse.jdt.internal.compiler.ast.DoStatement) { return convert((org.eclipse.jdt.internal.compiler.ast.DoStatement) statement); } if (statement instanceof org.eclipse.jdt.internal.compiler.ast.EmptyStatement) { return convert((org.eclipse.jdt.internal.compiler.ast.EmptyStatement) statement); } if (statement instanceof org.eclipse.jdt.internal.compiler.ast.ExplicitConstructorCall) { return convert((org.eclipse.jdt.internal.compiler.ast.ExplicitConstructorCall) statement); } if (statement instanceof org.eclipse.jdt.internal.compiler.ast.ForStatement) { return convert((org.eclipse.jdt.internal.compiler.ast.ForStatement) statement); } if (statement instanceof org.eclipse.jdt.internal.compiler.ast.IfStatement) { return convert((org.eclipse.jdt.internal.compiler.ast.IfStatement) statement); } if (statement instanceof org.eclipse.jdt.internal.compiler.ast.LabeledStatement) { return convert((org.eclipse.jdt.internal.compiler.ast.LabeledStatement) statement); } if (statement instanceof org.eclipse.jdt.internal.compiler.ast.ReturnStatement) { return convert((org.eclipse.jdt.internal.compiler.ast.ReturnStatement) statement); } if (statement instanceof org.eclipse.jdt.internal.compiler.ast.SwitchStatement) { return convert((org.eclipse.jdt.internal.compiler.ast.SwitchStatement) statement); } if (statement instanceof org.eclipse.jdt.internal.compiler.ast.SynchronizedStatement) { return convert((org.eclipse.jdt.internal.compiler.ast.SynchronizedStatement) statement); } if (statement instanceof org.eclipse.jdt.internal.compiler.ast.ThrowStatement) { return convert((org.eclipse.jdt.internal.compiler.ast.ThrowStatement) statement); } if (statement instanceof org.eclipse.jdt.internal.compiler.ast.TryStatement) { return convert((org.eclipse.jdt.internal.compiler.ast.TryStatement) statement); } if (statement instanceof org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) { ASTNode result = convert((org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) statement); if (result == null) { return createFakeEmptyStatement(statement); } // annotation and enum type declarations are not returned by the parser inside method bodies TypeDeclaration typeDeclaration = (TypeDeclaration) result; TypeDeclarationStatement typeDeclarationStatement = new TypeDeclarationStatement(this.ast); typeDeclarationStatement.setDeclaration(typeDeclaration); switch (this.ast.apiLevel) { case AST.JLS2_INTERNAL: TypeDeclaration typeDecl = typeDeclarationStatement.internalGetTypeDeclaration(); typeDeclarationStatement.setSourceRange(typeDecl.getStartPosition(), typeDecl.getLength()); break; default: AbstractTypeDeclaration typeDeclAST3 = typeDeclarationStatement.getDeclaration(); typeDeclarationStatement.setSourceRange(typeDeclAST3.getStartPosition(), typeDeclAST3.getLength()); break; } return typeDeclarationStatement; } if (statement instanceof org.eclipse.jdt.internal.compiler.ast.WhileStatement) { return convert((org.eclipse.jdt.internal.compiler.ast.WhileStatement) statement); } if (statement instanceof org.eclipse.jdt.internal.compiler.ast.Expression) { org.eclipse.jdt.internal.compiler.ast.Expression statement2 = (org.eclipse.jdt.internal.compiler.ast.Expression) statement; final Expression expr = convert(statement2); final ExpressionStatement stmt = new ExpressionStatement(this.ast); stmt.setExpression(expr); int sourceStart = expr.getStartPosition(); int sourceEnd = statement2.statementEnd; stmt.setSourceRange(sourceStart, sourceEnd - sourceStart + 1); return stmt; } return createFakeEmptyStatement(statement); }
From source file:org.eclipse.modisco.java.discoverer.internal.io.java.JDTVisitor.java
License:Open Source License
@Override public void endVisit(final org.eclipse.jdt.core.dom.ExpressionStatement node) { ExpressionStatement element = (ExpressionStatement) this.binding.get(node); initializeNode(element, node);/*from w w w . ja v a2 s . c o m*/ if (this.binding.get(node.getExpression()) != null) { element.setExpression(JDTVisitorUtils.completeExpression(this.binding.get(node.getExpression()), this)); } }
From source file:org.eclipse.wb.internal.core.model.variable.EmptyVariableSupport.java
License:Open Source License
/** * Converts this "empty" variable into existing local variable.<br> * Case when initializer {@link Expression} is direct child of {@link ExpressionStatement}. *//* www.ja va 2 s . c om*/ private void materialize_sameStatement() throws Exception { AstEditor editor = m_javaInfo.getEditor(); ExpressionStatement oldStatement = (ExpressionStatement) m_initializer.getParent(); // prepare type ITypeBinding typeBinding; String typeName; { typeBinding = AstNodeUtils.getTypeBinding(m_initializer); typeName = editor.getTypeBindingSource(typeBinding); } // initialize position and source int position = oldStatement.getStartPosition(); String source = ""; // add type Type newType; { newType = editor.getParser().parseQualifiedType(position, typeName); source += typeName + " "; } // add variable String variableName; SimpleName newDeclarationVariable; { variableName = editor.getUniqueVariableName(m_initializer.getStartPosition(), NamesManager.getName(m_javaInfo), null); newDeclarationVariable = editor.getParser().parseVariable(position + source.length(), variableName, null, typeBinding, false, Modifier.NONE); source += variableName + " = "; } // move initializer { String initializerSource = editor.getSource(m_initializer); // replace initializer, so "free" it to use with different parent oldStatement.setExpression(editor.getParser().parseSimpleName(0, "__wbp_Tmp")); // use initializer for declaration AstNodeUtils.moveNode(m_initializer, position + source.length()); source += initializerSource; } // add fragment VariableDeclarationFragment newFragment; { newFragment = m_initializer.getAST().newVariableDeclarationFragment(); newFragment.setName(newDeclarationVariable); newFragment.setInitializer(m_initializer); AstNodeUtils.setSourceRange(newFragment, newDeclarationVariable, m_initializer); } // add statement VariableDeclarationStatement newStatement; { newStatement = m_initializer.getAST().newVariableDeclarationStatement(newFragment); newStatement.setType(newType); AstNodeUtils.setSourceRange(newStatement, newType, newFragment, 1); source += ";"; } // update source editor.replaceSubstring(oldStatement.getStartPosition(), oldStatement.getLength(), source); // set new statement into AST { List<Statement> statements = DomGenerics.statements((Block) oldStatement.getParent()); int index = statements.indexOf(oldStatement); statements.set(index, newStatement); editor.resolveImports(newStatement); } // use local variable support m_javaInfo.setVariableSupport(new LocalUniqueVariableSupport(m_javaInfo, newDeclarationVariable)); }