List of usage examples for org.eclipse.jdt.core.dom Block statements
ASTNode.NodeList statements
To view the source code for org.eclipse.jdt.core.dom Block statements.
Click Source Link
From source file:org.ebayopensource.dsf.javatojs.translate.StatementTranslator.java
License:Open Source License
private IStmt toForEachStmt(final EnhancedForStatement astStmt, final BaseJstNode jstBlock) { JavaTranslationConvention convention = TranslateCtx.ctx().getConfig().getJavaTranslationConvention(); ExpressionTranslator exprTranslator = getExprTranslator(); IExpr jstExpr = exprTranslator.processExpression(astStmt.getExpression(), jstBlock); JstVar jstVar = getOtherTranslator().toJstVar(astStmt.getParameter(), jstBlock.getOwnerType()); IJstType collectionType = jstExpr.getResultType(); if (isJsForEachSupportedType(collectionType)) { return toForInStmt(astStmt, jstBlock, jstVar, jstExpr); }//ww w .ja v a 2 s .c o m ForStmt forStmt = new ForStmt(); jstBlock.addChild(forStmt); forStmt.getBody().getVarTable().addVarType(jstVar.getName(), jstVar.getType()); JstInitializer initializer = new JstInitializer(jstVar, null); forStmt.setInitializer(initializer); if (collectionType instanceof JstArray) { FieldAccessExpr lengthExpr; ArrayAccessExpr arrayAccessExpr; String tempIndexName = convention.getTempIndex() + getCtx().getTranslateInfo(jstBlock.getOwnerType()).getUniqueTempIndex(); JstIdentifier tmpIndex = new JstIdentifier(tempIndexName).setType(JstReservedTypes.JsNative.NUMBER); if (!(jstExpr instanceof JstIdentifier)) { JstIdentifier tmpArr = new JstIdentifier(convention.getTempArray()).setType(collectionType); initializer.addAssignment(new AssignExpr(tmpArr, jstExpr)); lengthExpr = new FieldAccessExpr(new JstIdentifier(convention.getArrayLength(), tmpArr) .setType(JstReservedTypes.JsNative.NUMBER)); arrayAccessExpr = new ArrayAccessExpr(tmpArr, tmpIndex); } else { lengthExpr = new FieldAccessExpr( new JstIdentifier(convention.getArrayLength()).setType(JstReservedTypes.JsNative.NUMBER), jstExpr); arrayAccessExpr = new ArrayAccessExpr(jstExpr, tmpIndex); } initializer.addAssignment(new AssignExpr(tmpIndex, SimpleLiteral.getIntLiteral(0))); forStmt.setCondition(new BoolExpr(tmpIndex, lengthExpr, Operator.LESS)); forStmt.addUpdater(new PostfixExpr(tmpIndex, PostfixExpr.Operator.INCREMENT)); forStmt.addStmt(new AssignExpr(new JstIdentifier(jstVar.getName()), arrayAccessExpr)); } else { //Iteratable JstIdentifier tmpItr = new JstIdentifier(convention.getTempIterator()); initializer.addAssignment( new AssignExpr(tmpItr, new MtdInvocationExpr(new JstIdentifier(convention.getIteratorMethod())) .setQualifyExpr(jstExpr))); forStmt.setCondition(new BoolExpr( new MtdInvocationExpr(new JstIdentifier(convention.getIteratorHasNext(), tmpItr)))); forStmt.addStmt(new AssignExpr(new JstIdentifier(jstVar.getName()), new MtdInvocationExpr(new JstIdentifier(convention.getIteratorNext(), tmpItr)))); } Statement body = astStmt.getBody(); if (body instanceof Block) { Block block = (Block) body; for (Object o : block.statements()) { if (o instanceof Statement) { forStmt.addStmt(processStatement((Statement) o, forStmt.getBody())); } else { getLogger().logUnhandledNode(this, (ASTNode) o, jstBlock); } } } else if (body instanceof Statement) { forStmt.addStmt(processStatement((Statement) body, forStmt.getBody())); } else { getLogger().logUnhandledNode(this, (ASTNode) body, jstBlock); } return forStmt; }
From source file:org.ebayopensource.dsf.javatojs.translate.StatementTranslator.java
License:Open Source License
private IStmt toForInStmt(final EnhancedForStatement astStmt, final BaseJstNode jstBlock, final JstVar jstVar, final IExpr jstExpr) { ForInStmt forInStmt = new ForInStmt(jstVar, jstExpr); jstBlock.addChild(forInStmt);//from ww w. j a v a2s . c o m forInStmt.getBody().getVarTable().addVarType(jstVar.getName(), jstVar.getType()); Statement body = astStmt.getBody(); if (body instanceof Block) { Block block = (Block) body; for (Object o : block.statements()) { if (o instanceof Statement) { forInStmt.addStmt(processStatement((Statement) o, forInStmt.getBody())); } else { getLogger().logUnhandledNode(this, (ASTNode) o, jstBlock); } } } else if (body instanceof Statement) { forInStmt.addStmt(processStatement((Statement) body, forInStmt.getBody())); } else { getLogger().logUnhandledNode(this, (ASTNode) body, jstBlock); } return forInStmt; }
From source file:org.ebayopensource.dsf.javatojs.translate.StatementTranslator.java
License:Open Source License
private WhileStmt toWhileStmt(final WhileStatement astStmt, final BaseJstNode jstBlock) { WhileStmt whileStmt = new WhileStmt(); jstBlock.addChild(whileStmt);// w w w . j a v a 2 s.co m Expression astCond = astStmt.getExpression(); if (astCond != null) { whileStmt.setCondition(getExprTranslator().toBoolExpr(astCond, jstBlock)); } Statement body = astStmt.getBody(); if (body instanceof Block) { Block block = (Block) body; for (Object o : block.statements()) { whileStmt.addStmt(processStatement((Statement) o, whileStmt.getBody())); } } else if (body instanceof Statement) { whileStmt.addStmt(processStatement((Statement) body, whileStmt.getBody())); } else { getLogger().logUnhandledNode(this, (ASTNode) body, jstBlock); } return whileStmt; }
From source file:org.ebayopensource.dsf.javatojs.translate.StatementTranslator.java
License:Open Source License
private DoStmt toDoStmt(final DoStatement astStmt, final BaseJstNode jstBlock) { DoStmt doStmt = new DoStmt(); jstBlock.addChild(doStmt);/*www . j a va 2 s . c o m*/ Expression astCond = astStmt.getExpression(); if (astCond != null) { if (astCond instanceof Expression) { doStmt.setCondition(getExprTranslator().toBoolExpr(astCond, jstBlock)); } else { getLogger().logUnhandledNode(this, (ASTNode) astCond, jstBlock); } } Statement body = astStmt.getBody(); if (body instanceof Block) { Block block = (Block) body; for (Object o : block.statements()) { if (o instanceof Statement) { doStmt.addStmt(processStatement((Statement) o, doStmt.getBody())); } else { getLogger().logUnhandledNode(this, (ASTNode) o, jstBlock); } } } else { getLogger().logUnhandledNode(this, (ASTNode) body, jstBlock); } return doStmt; }
From source file:org.ebayopensource.dsf.javatojs.translate.StatementTranslator.java
License:Open Source License
private IfStmt toIfStmt(final IfStatement astStmt, final BaseJstNode jstNode) { IfStmt ifStmt = new IfStmt(); jstNode.addChild(ifStmt);//from w ww . jav a 2 s. c om Expression astCond = astStmt.getExpression(); if (astCond != null) { IExpr expr = getExprTranslator().processExpression(astCond, ifStmt.getBody()); if (!(expr instanceof IBoolExpr)) { expr = new BoolExpr(expr); } ifStmt.setCondition((IBoolExpr) expr); } Statement thenBlock = astStmt.getThenStatement(); if (thenBlock instanceof Block) { Block block = (Block) thenBlock; for (Object o : block.statements()) { if (o instanceof Statement) { ifStmt.addThenStmt(processStatement((Statement) o, ifStmt.getBody())); } else { getLogger().logUnhandledNode(this, (ASTNode) o, jstNode); } } } else if (thenBlock instanceof Statement) { ifStmt.addThenStmt(processStatement((Statement) thenBlock, ifStmt.getBody())); } else { getLogger().logUnhandledNode(this, thenBlock, jstNode); } Statement elseStmt = astStmt.getElseStatement(); if (elseStmt != null) { if (elseStmt instanceof IfStatement) { ifStmt.addElseStmt(toIfStmt((IfStatement) elseStmt, ifStmt)); } else if (elseStmt instanceof Block) { Block block = (Block) elseStmt; for (Object o : block.statements()) { if (o instanceof Statement) { ifStmt.addElseStmt(processStatement((Statement) o, ifStmt.getElseBlock(true))); } else { getLogger().logUnhandledNode(this, (ASTNode) o, jstNode); } } } else if (elseStmt instanceof Statement) { ifStmt.addElseStmt(processStatement((Statement) elseStmt, ifStmt.getElseBlock(true))); } else { getLogger().logUnhandledNode(this, elseStmt, jstNode); } } return ifStmt; }
From source file:org.eclipse.andmore.android.generatecode.AbstractCodeGenerator.java
License:Apache License
/** * Adds a statement into block if the statement is not already declared. If * the last statement is a {@link ReturnStatement}, it inserts before it, * otherwise it inserts as the last statement in the block * //ww w . ja va 2s .c o m * @param block * @param declarationStatement * @param sameClass * true, it will compare if there is the same statement class in * the body of the methodDeclaration (but the content may be * different), false it will ignore the class and it will compare * if the content is the same (given by toString.equals()) */ @SuppressWarnings("unchecked") protected void addStatementIfNotFound(Block block, Statement declarationStatement, boolean sameClass) { boolean alreadyDeclared = false; List<Statement> statements = block.statements(); alreadyDeclared = isStatementAlreadyDeclared(declarationStatement, sameClass, statements); if (!alreadyDeclared) { Statement statement = block.statements().size() > 0 ? (Statement) block.statements().get(block.statements().size() - 1) : null; if ((statement instanceof ReturnStatement) && !(declarationStatement instanceof ReturnStatement)) { // need to insert before return block.statements().add(block.statements().size() - 1, declarationStatement); } else { block.statements().add(declarationStatement); } } }
From source file:org.eclipse.andmore.android.generatemenucode.model.codegenerators.CodeGeneratorBasedOnMenuVisitor.java
License:Apache License
/** * Visit method body from onCreateOptionsMenu declaration to the inflated * menu// w ww . j av a2 s . com * * @param node */ protected void visitMethodBodyToIdentifyMenu(MethodDeclaration node) { // Navigate through statements... Block body = node.getBody(); if (body != null) { List<?> statements = body.statements(); if (statements != null) { for (Object statement : statements) { if ((statement != null) && (statement instanceof ExpressionStatement)) { Expression argumentExpression = ((ExpressionStatement) statement).getExpression(); if ((argumentExpression != null) && (argumentExpression instanceof MethodInvocation)) { String methodSimpleName = ((MethodInvocation) argumentExpression).getName().toString(); if ((methodSimpleName != null) && (methodSimpleName.equals(INFLATE_METHOD))) { if ((((MethodInvocation) argumentExpression).arguments() != null) && (((MethodInvocation) argumentExpression).arguments().size() > 0)) { String menuBeingInflated = ((MethodInvocation) argumentExpression).arguments() .get(0).toString(); if ((menuBeingInflated != null) && (menuBeingInflated.indexOf('.') > 0)) { setInflatedMenuName( menuBeingInflated.substring(menuBeingInflated.lastIndexOf('.') + 1, menuBeingInflated.length())); } break; } } } } } } } }
From source file:org.eclipse.andmore.android.generatemenucode.model.codegenerators.MenuHandlerCodeGenerator.java
License:Apache License
/** * Creates method with handle for menu items (if android:onClick) is not * defined on menu.xml//from ww w. ja v a 2 s. c o m * <code>public boolean onOptionsItemSelected(MenuItem item)</code> */ @SuppressWarnings("unchecked") private IfStatement createOnOptionsItemSelectedForMenuItems(MenuItemNode node) { IfStatement ifSt; // declare method MethodDeclaration methodDeclaration = addMethodDeclaration(ModifierKeyword.PUBLIC_KEYWORD, CodeGeneratorBasedOnMenuConstants.ON_OPTIONS_ITEM_SELECTED, PrimitiveType.BOOLEAN, CodeGeneratorBasedOnMenuConstants.MENU_ITEM, CodeGeneratorBasedOnMenuConstants.ITEM); MethodDeclaration foundMethod = isMethodAlreadyDeclared(methodDeclaration, ON_OPTIONS_ITEM_SELECTED_MENU_ITEM_METHODBINDING); Block block = null; if (foundMethod != null) { // method onOptionsItemSelected is already created => use the found // method instead of the new created one methodDeclaration = foundMethod; block = methodDeclaration.getBody(); } else { // method onOptionsItemSelected not found => create block to insert // statements block = typeDeclaration.getAST().newBlock(); methodDeclaration.setBody(block); } // create if and else-if's ifSt = createElseIfForEachMenuItemId(node); IfStatement foundIfStatement = (IfStatement) findIfStatementAlreadyDeclared(ifSt, true, block.statements()); if (foundIfStatement != null) { ifSt = foundIfStatement; } else { // if not existent yet then: // 1-add else addingElseExpression(ifSt, methodDeclaration); // 2-add if statement only if there is not another one addStatementIfNotFound(block, ifSt, true); } createReturnStatement(methodDeclaration); if (foundMethod == null) { // method onOptionsItemSelected was not yet declared typeDeclaration.bodyDeclarations().add(methodDeclaration); } return ifSt; }
From source file:org.eclipse.imp.analysis.reachingdefs.ReachingDefsConstraintCreator.java
License:Open Source License
public IConstraint[] create(Block block) { List<Statement> stmts = block.statements(); List<IConstraint> result = new ArrayList<IConstraint>(); if (stmts.size() < 1) return createPassThroughConstraint(block); result.add(newSubsetConstraint(fVariableFactory.createEntryLabel(block), fVariableFactory.createEntryLabel((ASTNode) stmts.get(0)))); for (int i = 1; i < stmts.size(); i++) { result.add(newSubsetConstraint(fVariableFactory.createExitLabel((ASTNode) stmts.get(i - 1)), fVariableFactory.createEntryLabel((ASTNode) stmts.get(i)))); }//from w w w . j a v a 2s . c o m result.add(newSubsetConstraint(fVariableFactory.createExitLabel((ASTNode) stmts.get(stmts.size() - 1)), fVariableFactory.createExitLabel(block))); return (IConstraint[]) result.toArray(new IConstraint[result.size()]); }
From source file:org.eclipse.jdt.core.dom.ASTConverter.java
License:Open Source License
public ASTNode convert(boolean isInterface, org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration methodDeclaration) { checkCanceled();//from ww w.ja va 2 s .c o m if (methodDeclaration instanceof org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration) { return convert((org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration) methodDeclaration); } MethodDeclaration methodDecl = new MethodDeclaration(this.ast); setModifiers(methodDecl, methodDeclaration); boolean isConstructor = methodDeclaration.isConstructor(); methodDecl.setConstructor(isConstructor); final SimpleName methodName = new SimpleName(this.ast); methodName.internalSetIdentifier(new String(methodDeclaration.selector)); int start = methodDeclaration.sourceStart; // GROOVY start // why does this do what it does? /* old { int end = retrieveIdentifierEndPosition(start, methodDeclaration.sourceEnd); } new */ int end = (scannerAvailable(methodDeclaration.scope) ? retrieveIdentifierEndPosition(start, methodDeclaration.sourceEnd) : methodDeclaration.sourceEnd); // GROOVY end methodName.setSourceRange(start, end - start + 1); methodDecl.setName(methodName); org.eclipse.jdt.internal.compiler.ast.TypeReference[] thrownExceptions = methodDeclaration.thrownExceptions; int methodHeaderEnd = methodDeclaration.sourceEnd; int thrownExceptionsLength = thrownExceptions == null ? 0 : thrownExceptions.length; if (thrownExceptionsLength > 0) { Name thrownException; int i = 0; do { thrownException = convert(thrownExceptions[i++]); methodDecl.thrownExceptions().add(thrownException); } while (i < thrownExceptionsLength); methodHeaderEnd = thrownException.getStartPosition() + thrownException.getLength(); } org.eclipse.jdt.internal.compiler.ast.Argument[] parameters = methodDeclaration.arguments; int parametersLength = parameters == null ? 0 : parameters.length; if (parametersLength > 0) { SingleVariableDeclaration parameter; int i = 0; do { // GROOVY start // make sure the scope is available just in case it is necessary for varargs // new code BlockScope origScope = null; if (parameters[i].binding != null) { origScope = parameters[i].binding.declaringScope; parameters[i].binding.declaringScope = methodDeclaration.scope; } // GROOVY end parameter = convert(parameters[i++]); // GROOVY start // unset the scope // new code if (parameters[i - 1].binding != null) { parameters[i - 1].binding.declaringScope = origScope; } // GROOVY end methodDecl.parameters().add(parameter); } while (i < parametersLength); if (thrownExceptionsLength == 0) { methodHeaderEnd = parameter.getStartPosition() + parameter.getLength(); } } org.eclipse.jdt.internal.compiler.ast.ExplicitConstructorCall explicitConstructorCall = null; if (isConstructor) { if (isInterface) { // interface cannot have a constructor methodDecl.setFlags(methodDecl.getFlags() | ASTNode.MALFORMED); } org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration constructorDeclaration = (org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration) methodDeclaration; explicitConstructorCall = constructorDeclaration.constructorCall; switch (this.ast.apiLevel) { case AST.JLS2_INTERNAL: // set the return type to VOID PrimitiveType returnType = new PrimitiveType(this.ast); returnType.setPrimitiveTypeCode(PrimitiveType.VOID); returnType.setSourceRange(methodDeclaration.sourceStart, 0); methodDecl.internalSetReturnType(returnType); break; default: methodDecl.setReturnType2(null); } } else if (methodDeclaration instanceof org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) { org.eclipse.jdt.internal.compiler.ast.MethodDeclaration method = (org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) methodDeclaration; org.eclipse.jdt.internal.compiler.ast.TypeReference typeReference = method.returnType; if (typeReference != null) { Type returnType = convertType(typeReference); // get the positions of the right parenthesis int rightParenthesisPosition = retrieveEndOfRightParenthesisPosition(end, method.bodyEnd); int extraDimensions = retrieveExtraDimension(rightParenthesisPosition, method.bodyEnd); methodDecl.setExtraDimensions(extraDimensions); setTypeForMethodDeclaration(methodDecl, returnType, extraDimensions); } else { // no return type for a method that is not a constructor methodDecl.setFlags(methodDecl.getFlags() | ASTNode.MALFORMED); switch (this.ast.apiLevel) { case AST.JLS2_INTERNAL: break; default: methodDecl.setReturnType2(null); } } } int declarationSourceStart = methodDeclaration.declarationSourceStart; int bodyEnd = methodDeclaration.bodyEnd; methodDecl.setSourceRange(declarationSourceStart, bodyEnd - declarationSourceStart + 1); int declarationSourceEnd = methodDeclaration.declarationSourceEnd; int rightBraceOrSemiColonPositionStart = bodyEnd == declarationSourceEnd ? bodyEnd : bodyEnd + 1; int closingPosition = retrieveRightBraceOrSemiColonPosition(rightBraceOrSemiColonPositionStart, declarationSourceEnd); if (closingPosition != -1) { int startPosition = methodDecl.getStartPosition(); methodDecl.setSourceRange(startPosition, closingPosition - startPosition + 1); org.eclipse.jdt.internal.compiler.ast.Statement[] statements = methodDeclaration.statements; start = retrieveStartBlockPosition(methodHeaderEnd, methodDeclaration.bodyStart); if (start == -1) start = methodDeclaration.bodyStart; // use recovery position for body start end = retrieveRightBrace(methodDeclaration.bodyEnd, declarationSourceEnd); Block block = null; if (start != -1 && end != -1) { /* * start or end can be equal to -1 if we have an interface's method. */ block = new Block(this.ast); block.setSourceRange(start, closingPosition - start + 1); methodDecl.setBody(block); } if (block != null && (statements != null || explicitConstructorCall != null)) { if (explicitConstructorCall != null && explicitConstructorCall.accessMode != org.eclipse.jdt.internal.compiler.ast.ExplicitConstructorCall.ImplicitSuper) { block.statements().add(convert(explicitConstructorCall)); } int statementsLength = statements == null ? 0 : statements.length; for (int i = 0; i < statementsLength; i++) { if (statements[i] instanceof org.eclipse.jdt.internal.compiler.ast.LocalDeclaration) { checkAndAddMultipleLocalDeclaration(statements, i, block.statements()); } else { final Statement statement = convert(statements[i]); if (statement != null) { block.statements().add(statement); } } } } if (block != null && (Modifier.isAbstract(methodDecl.getModifiers()) || Modifier.isNative(methodDecl.getModifiers()) || isInterface)) { methodDecl.setFlags(methodDecl.getFlags() | ASTNode.MALFORMED); } } else { // syntax error in this method declaration methodDecl.setFlags(methodDecl.getFlags() | ASTNode.MALFORMED); if (!methodDeclaration.isNative() && !methodDeclaration.isAbstract()) { start = retrieveStartBlockPosition(methodHeaderEnd, bodyEnd); if (start == -1) start = methodDeclaration.bodyStart; // use recovery position for body start end = methodDeclaration.bodyEnd; // try to get the best end position CategorizedProblem[] problems = methodDeclaration.compilationResult().problems; if (problems != null) { for (int i = 0, max = methodDeclaration.compilationResult().problemCount; i < max; i++) { CategorizedProblem currentProblem = problems[i]; if (currentProblem.getSourceStart() == start && currentProblem.getID() == IProblem.ParsingErrorInsertToComplete) { end = currentProblem.getSourceEnd(); break; } } } int startPosition = methodDecl.getStartPosition(); methodDecl.setSourceRange(startPosition, end - startPosition + 1); if (start != -1 && end != -1) { /* * start or end can be equal to -1 if we have an interface's method. */ Block block = new Block(this.ast); block.setSourceRange(start, end - start + 1); methodDecl.setBody(block); } } } org.eclipse.jdt.internal.compiler.ast.TypeParameter[] typeParameters = methodDeclaration.typeParameters(); if (typeParameters != null) { switch (this.ast.apiLevel) { case AST.JLS2_INTERNAL: methodDecl.setFlags(methodDecl.getFlags() | ASTNode.MALFORMED); break; default: for (int i = 0, max = typeParameters.length; i < max; i++) { methodDecl.typeParameters().add(convert(typeParameters[i])); } } } // The javadoc comment is now got from list store in compilation unit declaration convert(methodDeclaration.javadoc, methodDecl); if (this.resolveBindings) { recordNodes(methodDecl, methodDeclaration); recordNodes(methodName, methodDeclaration); methodDecl.resolveBinding(); } return methodDecl; }