List of usage examples for org.eclipse.jdt.internal.compiler.ast TryStatement TryStatement
TryStatement
From source file:lombok.eclipse.handlers.ast.EclipseASTMaker.java
License:Open Source License
@Override public ASTNode visitTry(final lombok.ast.Try node, final Void p) { final TryStatement tryStatement = new TryStatement(); setGeneratedByAndCopyPos(tryStatement, source, posHintOf(node)); tryStatement.tryBlock = build(node.getTryBlock()); tryStatement.catchArguments = toArray(build(node.getCatchArguments()), new Argument[0]); tryStatement.catchBlocks = toArray(build(node.getCatchBlocks()), new Block[0]); if (node.getFinallyBlock() != null) { tryStatement.finallyBlock = build(node.getFinallyBlock()); }//from w w w .j a v a 2 s. co m return tryStatement; }
From source file:lombok.eclipse.handlers.HandleCleanup.java
License:Open Source License
public void handle(AnnotationValues<Cleanup> annotation, Annotation ast, EclipseNode annotationNode) { handleFlagUsage(annotationNode, ConfigurationKeys.CLEANUP_FLAG_USAGE, "@Cleanup"); String cleanupName = annotation.getInstance().value(); if (cleanupName.length() == 0) { annotationNode.addError("cleanupName cannot be the empty string."); return;/*from w w w .j a va 2s . c o m*/ } if (annotationNode.up().getKind() != Kind.LOCAL) { annotationNode.addError("@Cleanup is legal only on local variable declarations."); return; } LocalDeclaration decl = (LocalDeclaration) annotationNode.up().get(); if (decl.initialization == null) { annotationNode.addError("@Cleanup variable declarations need to be initialized."); return; } EclipseNode ancestor = annotationNode.up().directUp(); ASTNode blockNode = ancestor.get(); final boolean isSwitch; final Statement[] statements; if (blockNode instanceof AbstractMethodDeclaration) { isSwitch = false; statements = ((AbstractMethodDeclaration) blockNode).statements; } else if (blockNode instanceof Block) { isSwitch = false; statements = ((Block) blockNode).statements; } else if (blockNode instanceof SwitchStatement) { isSwitch = true; statements = ((SwitchStatement) blockNode).statements; } else { annotationNode.addError("@Cleanup is legal only on a local variable declaration inside a block."); return; } if (statements == null) { annotationNode.addError("LOMBOK BUG: Parent block does not contain any statements."); return; } int start = 0; for (; start < statements.length; start++) { if (statements[start] == decl) break; } if (start == statements.length) { annotationNode.addError("LOMBOK BUG: Can't find this local variable declaration inside its parent."); return; } start++; //We start with try{} *AFTER* the var declaration. int end; if (isSwitch) { end = start + 1; for (; end < statements.length; end++) { if (statements[end] instanceof CaseStatement) { break; } } } else end = statements.length; //At this point: // start-1 = Local Declaration marked with @Cleanup // start = first instruction that needs to be wrapped into a try block // end = last instruction of the scope -OR- last instruction before the next case label in switch statements. // hence: // [start, end) = statements for the try block. Statement[] tryBlock = new Statement[end - start]; System.arraycopy(statements, start, tryBlock, 0, end - start); //Remove the stuff we just dumped into the tryBlock, and then leave room for the try node. int newStatementsLength = statements.length - (end - start); //Remove room for every statement moved into try block... newStatementsLength += 1; //But add room for the TryStatement node itself. Statement[] newStatements = new Statement[newStatementsLength]; System.arraycopy(statements, 0, newStatements, 0, start); //copy all statements before the try block verbatim. System.arraycopy(statements, end, newStatements, start + 1, statements.length - end); //For switch statements. doAssignmentCheck(annotationNode, tryBlock, decl.name); TryStatement tryStatement = new TryStatement(); setGeneratedBy(tryStatement, ast); tryStatement.tryBlock = new Block(0); tryStatement.tryBlock.statements = tryBlock; setGeneratedBy(tryStatement.tryBlock, ast); // Positions for in-method generated nodes are special int ss = decl.declarationSourceEnd + 1; int se = ss; if (tryBlock.length > 0) { se = tryBlock[tryBlock.length - 1].sourceEnd + 1; //+1 for the closing semicolon. Yes, there could be spaces. Bummer. tryStatement.sourceStart = ss; tryStatement.sourceEnd = se; tryStatement.tryBlock.sourceStart = ss; tryStatement.tryBlock.sourceEnd = se; } newStatements[start] = tryStatement; Statement[] finallyBlock = new Statement[1]; MessageSend unsafeClose = new MessageSend(); setGeneratedBy(unsafeClose, ast); unsafeClose.sourceStart = ast.sourceStart; unsafeClose.sourceEnd = ast.sourceEnd; SingleNameReference receiver = new SingleNameReference(decl.name, 0); setGeneratedBy(receiver, ast); unsafeClose.receiver = receiver; long nameSourcePosition = (long) ast.sourceStart << 32 | ast.sourceEnd; if (ast.memberValuePairs() != null) for (MemberValuePair pair : ast.memberValuePairs()) { if (pair.name != null && new String(pair.name).equals("value")) { nameSourcePosition = (long) pair.value.sourceStart << 32 | pair.value.sourceEnd; break; } } unsafeClose.nameSourcePosition = nameSourcePosition; unsafeClose.selector = cleanupName.toCharArray(); int pS = ast.sourceStart, pE = ast.sourceEnd; long p = (long) pS << 32 | pE; SingleNameReference varName = new SingleNameReference(decl.name, p); setGeneratedBy(varName, ast); NullLiteral nullLiteral = new NullLiteral(pS, pE); setGeneratedBy(nullLiteral, ast); MessageSend preventNullAnalysis = preventNullAnalysis(ast, varName); EqualExpression equalExpression = new EqualExpression(preventNullAnalysis, nullLiteral, OperatorIds.NOT_EQUAL); equalExpression.sourceStart = pS; equalExpression.sourceEnd = pE; setGeneratedBy(equalExpression, ast); Block closeBlock = new Block(0); closeBlock.statements = new Statement[1]; closeBlock.statements[0] = unsafeClose; setGeneratedBy(closeBlock, ast); IfStatement ifStatement = new IfStatement(equalExpression, closeBlock, 0, 0); setGeneratedBy(ifStatement, ast); finallyBlock[0] = ifStatement; tryStatement.finallyBlock = new Block(0); // Positions for in-method generated nodes are special if (!isSwitch) { tryStatement.finallyBlock.sourceStart = blockNode.sourceEnd; tryStatement.finallyBlock.sourceEnd = blockNode.sourceEnd; } setGeneratedBy(tryStatement.finallyBlock, ast); tryStatement.finallyBlock.statements = finallyBlock; tryStatement.catchArguments = null; tryStatement.catchBlocks = null; if (blockNode instanceof AbstractMethodDeclaration) { ((AbstractMethodDeclaration) blockNode).statements = newStatements; } else if (blockNode instanceof Block) { ((Block) blockNode).statements = newStatements; } else if (blockNode instanceof SwitchStatement) { ((SwitchStatement) blockNode).statements = newStatements; } ancestor.rebuild(); }
From source file:lombok.eclipse.handlers.HandleSneakyThrows.java
License:Open Source License
public Statement buildTryCatchBlock(Statement[] contents, DeclaredException exception, ASTNode source, AbstractMethodDeclaration method) { int methodStart = method.bodyStart; int methodEnd = method.bodyEnd; long methodPosEnd = ((long) methodEnd) << 32 | (methodEnd & 0xFFFFFFFFL); TryStatement tryStatement = new TryStatement(); setGeneratedBy(tryStatement, source); tryStatement.tryBlock = new Block(0); // Positions for in-method generated nodes are special tryStatement.tryBlock.sourceStart = methodStart; tryStatement.tryBlock.sourceEnd = methodEnd; setGeneratedBy(tryStatement.tryBlock, source); tryStatement.tryBlock.statements = contents; TypeReference typeReference;//from w ww . ja v a 2s. co m if (exception.exceptionName.indexOf('.') == -1) { typeReference = new SingleTypeReference(exception.exceptionName.toCharArray(), methodPosEnd); typeReference.statementEnd = methodEnd; } else { String[] x = exception.exceptionName.split("\\."); char[][] elems = new char[x.length][]; long[] poss = new long[x.length]; Arrays.fill(poss, methodPosEnd); for (int i = 0; i < x.length; i++) { elems[i] = x[i].trim().toCharArray(); } typeReference = new QualifiedTypeReference(elems, poss); } setGeneratedBy(typeReference, source); Argument catchArg = new Argument("$ex".toCharArray(), methodPosEnd, typeReference, Modifier.FINAL); setGeneratedBy(catchArg, source); catchArg.declarationSourceEnd = catchArg.declarationEnd = catchArg.sourceEnd = methodEnd; catchArg.declarationSourceStart = catchArg.modifiersSourceStart = catchArg.sourceStart = methodEnd; tryStatement.catchArguments = new Argument[] { catchArg }; MessageSend sneakyThrowStatement = new MessageSend(); setGeneratedBy(sneakyThrowStatement, source); sneakyThrowStatement.receiver = new QualifiedNameReference( new char[][] { "lombok".toCharArray(), "Lombok".toCharArray() }, new long[2], methodEnd, methodEnd); setGeneratedBy(sneakyThrowStatement.receiver, source); sneakyThrowStatement.receiver.statementEnd = methodEnd; sneakyThrowStatement.selector = "sneakyThrow".toCharArray(); SingleNameReference exRef = new SingleNameReference("$ex".toCharArray(), methodPosEnd); setGeneratedBy(exRef, source); exRef.statementEnd = methodEnd; sneakyThrowStatement.arguments = new Expression[] { exRef }; // This is the magic fix for rendering issues // In org.eclipse.jdt.core.dom.ASTConverter#convert(org.eclipse.jdt.internal.compiler.ast.MessageSend) // a new SimpleName is created and the setSourceRange should receive -1, 0. That's why we provide -2L :-) sneakyThrowStatement.nameSourcePosition = -2L; sneakyThrowStatement.sourceStart = methodEnd; sneakyThrowStatement.sourceEnd = sneakyThrowStatement.statementEnd = methodEnd; Statement rethrowStatement = new ThrowStatement(sneakyThrowStatement, methodEnd, methodEnd); setGeneratedBy(rethrowStatement, source); Block block = new Block(0); block.sourceStart = methodEnd; block.sourceEnd = methodEnd; setGeneratedBy(block, source); block.statements = new Statement[] { rethrowStatement }; tryStatement.catchBlocks = new Block[] { block }; // Positions for in-method generated nodes are special tryStatement.sourceStart = method.bodyStart; tryStatement.sourceEnd = method.bodyEnd; return tryStatement; }
From source file:org.eclipse.jdt.internal.compiler.parser.Parser.java
License:Open Source License
protected void consumeStatementTry(boolean withFinally, boolean hasResources) { //TryStatement ::= 'try' Block Catches //TryStatement ::= 'try' Block Catchesopt Finally // TryStatementWithResources ::= 'try' ResourceSpecification TryBlock Catchesopt // TryStatementWithResources ::= 'try' ResourceSpecification TryBlock Catchesopt Finally int length;/*w w w.j a v a 2 s .c om*/ TryStatement tryStmt = new TryStatement(); //finally if (withFinally) { this.astLengthPtr--; tryStmt.finallyBlock = (Block) this.astStack[this.astPtr--]; } //catches are handle by two <argument-block> [see statementCatch] if ((length = this.astLengthStack[this.astLengthPtr--]) != 0) { if (length == 1) { tryStmt.catchBlocks = new Block[] { (Block) this.astStack[this.astPtr--] }; tryStmt.catchArguments = new Argument[] { (Argument) this.astStack[this.astPtr--] }; } else { Block[] bks = (tryStmt.catchBlocks = new Block[length]); Argument[] args = (tryStmt.catchArguments = new Argument[length]); while (length-- > 0) { bks[length] = (Block) this.astStack[this.astPtr--]; args[length] = (Argument) this.astStack[this.astPtr--]; } } } //try this.astLengthPtr--; tryStmt.tryBlock = (Block) this.astStack[this.astPtr--]; if (hasResources) { // get the resources length = this.astLengthStack[this.astLengthPtr--]; LocalDeclaration[] resources = new LocalDeclaration[length]; System.arraycopy(this.astStack, (this.astPtr -= length) + 1, resources, 0, length); tryStmt.resources = resources; if (this.options.sourceLevel < ClassFileConstants.JDK1_7) { problemReporter().autoManagedResourcesNotBelow17(resources); } } //positions tryStmt.sourceEnd = this.endStatementPosition; tryStmt.sourceStart = this.intStack[this.intPtr--]; pushOnAstStack(tryStmt); }
From source file:org.eclipse.objectteams.otdt.internal.core.compiler.ast.GuardPredicateDeclaration.java
License:Open Source License
Statement tryCatch(Statement tryStatement, TypeReference exceptionType, Statement catchStatement) { TryStatement result = new TryStatement() { @Override// w w w .j av a 2 s .co m protected ExceptionHandlingFlowContext createFlowContext(FlowContext flowContext, FlowInfo flowInfo) { return new ExceptionHandlingFlowContext(flowContext, this, Binding.NO_EXCEPTIONS, // treat all exceptions as undeclared, want to see the error/warning new int[0], null, // initializationParent this.scope, flowInfo) { @Override public UnconditionalFlowInfo initsOnException(int index) { return new UnconditionalFlowInfo(); // empty, avoid AIOOBE in super method (empty array initsOnExceptions) } }; } }; result.sourceStart = this.sourceStart; result.sourceEnd = this.sourceEnd; // fill sub-elements: AstGenerator gen = new AstGenerator(this.sourceStart, this.sourceEnd); result.tryBlock = gen.block(new Statement[] { tryStatement }); result.catchArguments = new Argument[] { gen.argument("exc".toCharArray(), exceptionType) }; //$NON-NLS-1$ result.catchBlocks = new Block[] { gen.block(new Statement[] { catchStatement }) }; return result; }
From source file:org.eclipse.objectteams.otdt.internal.core.compiler.ast.WithinStatement.java
License:Open Source License
public WithinStatement(Expression teamExpr, Statement action, int s, int e) { super(2); // two declarations: the team variable and the level variable this.sourceStart = s; this.sourceEnd = e; AstGenerator gen = new AstGenerator(s, e); // GEN: org.objectteams.Team __OT__team$<pos> = <teamExpr>; // use source position to generate unique name: this.teamVarName = (WithinStatement.TEAM_VAR_PREFIX + s).toCharArray(); char[] saveVarName = (SAVE_VAR_PREFIX + s).toCharArray(); this.teamVarDecl = gen.localVariable(this.teamVarName, gen.qualifiedTypeReference(ORG_OBJECTTEAMS_ITEAM), teamExpr);/* ww w . j a v a 2 s . c om*/ // GEN: int _OT$save<pos> = __OT__team$<pos>._OT$saveActivationState(); LocalDeclaration saveVarDecl = gen.localVariable(saveVarName, gen.singleTypeReference(TypeBinding.INT), teamMethodInvocation(SAVE_STATE_NAME, gen, null)); // GEN: __OT__team$<pos>.activate(); MessageSend activateSend = teamMethodInvocation(ACTIVATE_NAME, gen, null); // ensure action is a Block this.body = action; Block actionBlock; if (action instanceof Block) { actionBlock = (Block) action; } else { actionBlock = new Block(0); // no declarations actionBlock.statements = new Statement[] { action }; } /* GEN resetter block: * { * __OT__team$<pos>_OT$restoreActivationState(_OT$save<pos>); * } */ Block deactivateBlock = new Block(0); deactivateBlock.sourceStart = s; deactivateBlock.sourceEnd = e; deactivateBlock.statements = new Statement[] { teamMethodInvocation(RESTORE_ACTIVATION_NAME, gen, gen.singleNameReference(saveVarName)) }; // assemble action and deactivation into a try-finally: TryStatement tryStatement = new TryStatement(); tryStatement.sourceStart = s; tryStatement.sourceEnd = e; tryStatement.tryBlock = actionBlock; // the actual source body tryStatement.finallyBlock = deactivateBlock; // final assembly: this.statements = new Statement[] { this.teamVarDecl, // Team __OT__team$<pos> = <teamExpr>; saveVarDecl, // _OT$save<pos> = __OT__team$<pos>._OT$saveActivationState(); activateSend, // __OT__team$<pos>.activate(); tryStatement // try { <action> } finally { <reset> } }; }
From source file:org.eclipse.objectteams.otdt.internal.core.compiler.lifting.Lifting.java
License:Open Source License
private Block createElseBlock(ReferenceBinding returnType, ReferenceBinding teamType) { // else{ ... } // try { myRole = (MyRole)role } // catch (ClassCastException ex) { throw new WrongRoleException ( ... ); } TryStatement tryStatement = new TryStatement(); tryStatement.sourceStart = this._gen.sourceStart; tryStatement.sourceEnd = this._gen.sourceEnd; tryStatement.tryBlock = createTryCastBlock(returnType); createCatchClassCastExceptionBlock(returnType, teamType, tryStatement); return this._gen.block2(createCacheLookupLocalDeclaration(), tryStatement); }
From source file:org.eclipse.objectteams.otdt.internal.core.compiler.util.AstGenerator.java
License:Open Source License
public TryStatement tryFinally(Statement[] tryStatements, Statement[] finallyStatements) { TryStatement stat = new TryStatement(); stat.sourceStart = this.sourceStart; stat.sourceEnd = this.sourceEnd; stat.tryBlock = block(tryStatements); stat.finallyBlock = block(finallyStatements); if (finallyStatements != null) { int len = finallyStatements.length; if (len > 0) { stat.finallyBlock.sourceStart = finallyStatements[0].sourceStart; stat.finallyBlock.sourceEnd = finallyStatements[len - 1].sourceEnd; }/*from w ww.j a va 2 s .c o m*/ } return stat; }
From source file:org.eclipse.objectteams.otdt.internal.core.compiler.util.AstGenerator.java
License:Open Source License
public TryStatement tryCatch(Statement[] tryStatements, Argument exceptionArgument, Statement[] catchStatements) { TryStatement stat = new TryStatement(); stat.sourceStart = this.sourceStart; stat.sourceEnd = this.sourceEnd; stat.tryBlock = block(tryStatements); stat.catchArguments = new Argument[] { exceptionArgument }; stat.catchBlocks = new Block[] { block(catchStatements) }; return stat;//from www . j ava 2 s. c o m }
From source file:org.eclipse.objectteams.otdt.internal.core.compiler.util.AstGenerator.java
License:Open Source License
public TryStatement tryCatch(Statement[] tryStatements, Argument[] exceptionArguments, Statement[][] catchStatementss) { TryStatement stat = new TryStatement(); stat.sourceStart = this.sourceStart; stat.sourceEnd = this.sourceEnd; stat.tryBlock = block(tryStatements); stat.catchArguments = exceptionArguments; stat.catchBlocks = new Block[catchStatementss.length]; for (int i = 0; i < catchStatementss.length; i++) stat.catchBlocks[i] = block(catchStatementss[i]); return stat;// w ww .j a v a 2 s . co m }