public char[] returnVarNameIfNullCheck(Statement stat) { if (!(stat instanceof IfStatement)) return null; /* Check that the if's statement is a throw statement, possibly in a block. */ { Statement then = ((IfStatement) stat).thenStatement; if (then instanceof Block) { Statement[] blockStatements = ((Block) then).statements; if (blockStatements == null || blockStatements.length == 0) return null; then = blockStatements[0]; } if (!(then instanceof ThrowStatement)) return null; } /* Check that the if's conditional is like 'x == null'. Return from this method (don't generate a nullcheck) if 'x' is equal to our own variable's name: There's already a nullcheck here. */ { Expression cond = ((IfStatement) stat).condition; if (!(cond instanceof EqualExpression)) return null; EqualExpression bin = (EqualExpression) cond; int operatorId = ((bin.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT); if (operatorId != OperatorIds.EQUAL_EQUAL) return null; if (!(bin.left instanceof SingleNameReference)) return null; if (!(bin.right instanceof NullLiteral)) return null; return ((SingleNameReference) bin.left).token; } }
public RecoveredElement add(Block nestedBlockDeclaration, int bracketBalanceValue) { /* default behavior is to delegate recording to parent if any, do not consider elements passed the known end (if set) it must be belonging to an enclosing element */ if (this.fieldDeclaration.declarationSourceEnd > 0 && nestedBlockDeclaration.sourceStart > this.fieldDeclaration.declarationSourceEnd){ resetPendingModifiers(); if (this.parent == null) return this; // ignore return this.parent.add(nestedBlockDeclaration, bracketBalanceValue); } /* consider that if the opening brace was not found, it is there */ if (!this.foundOpeningBrace){ this.foundOpeningBrace = true; this.bracketBalance++; } this.initializerBody = new RecoveredBlock(nestedBlockDeclaration, this, bracketBalanceValue); if (nestedBlockDeclaration.sourceEnd == 0) return this.initializerBody; return this; }
public RecoveredElement add(LocalDeclaration localDeclaration, int bracketBalanceValue) { /* do not consider a type starting passed the type end (if set) it must be belonging to an enclosing type */ if (this.fieldDeclaration.declarationSourceEnd != 0 && localDeclaration.declarationSourceStart > this.fieldDeclaration.declarationSourceEnd){ resetPendingModifiers(); if (this.parent == null) return this; // ignore return this.parent.add(localDeclaration, bracketBalanceValue); } /* method body should have been created */ Block block = new Block(0); block.sourceStart = ((Initializer)this.fieldDeclaration).sourceStart; RecoveredElement element = this.add(block, 1); if (this.initializerBody != null) { this.initializerBody.attachPendingModifiers( this.pendingAnnotations, this.pendingAnnotationCount, this.pendingModifiers, this.pendingModifersSourceStart); } resetPendingModifiers(); return element.add(localDeclaration, bracketBalanceValue); }
public RecoveredElement add(Statement statement, int bracketBalanceValue) { /* do not consider a statement starting passed the initializer end (if set) it must be belonging to an enclosing type */ if (this.fieldDeclaration.declarationSourceEnd != 0 && statement.sourceStart > this.fieldDeclaration.declarationSourceEnd){ resetPendingModifiers(); if (this.parent == null) return this; // ignore return this.parent.add(statement, bracketBalanceValue); } /* initializer body should have been created */ Block block = new Block(0); block.sourceStart = ((Initializer)this.fieldDeclaration).sourceStart; RecoveredElement element = this.add(block, 1); if (this.initializerBody != null) { this.initializerBody.attachPendingModifiers( this.pendingAnnotations, this.pendingAnnotationCount, this.pendingModifiers, this.pendingModifersSourceStart); } resetPendingModifiers(); return element.add(statement, bracketBalanceValue); }
public FieldDeclaration updatedFieldDeclaration(int depth, Set knownTypes){ if (this.initializerBody != null){ Block block = this.initializerBody.updatedBlock(depth, knownTypes); if (block != null){ Initializer initializer = (Initializer) this.fieldDeclaration; initializer.block = block; if (initializer.declarationSourceEnd == 0) { initializer.declarationSourceEnd = block.sourceEnd; initializer.bodyEnd = block.sourceEnd; } } if (this.localTypeCount > 0) this.fieldDeclaration.bits |= ASTNode.HasLocalType; } if (this.fieldDeclaration.sourceEnd == 0){ this.fieldDeclaration.sourceEnd = this.fieldDeclaration.declarationSourceEnd; } return this.fieldDeclaration; }
public RecoveredElement add(Block nestedBlockDeclaration, int bracketBalanceValue) { /* default behavior is to delegate recording to parent if any, do not consider elements passed the known end (if set) it must be belonging to an enclosing element */ if (this.methodDeclaration.declarationSourceEnd > 0 && nestedBlockDeclaration.sourceStart > this.methodDeclaration.declarationSourceEnd){ resetPendingModifiers(); if (this.parent == null){ return this; // ignore } else { return this.parent.add(nestedBlockDeclaration, bracketBalanceValue); } } /* consider that if the opening brace was not found, it is there */ if (!this.foundOpeningBrace){ this.foundOpeningBrace = true; this.bracketBalance++; } this.methodBody = new RecoveredBlock(nestedBlockDeclaration, this, bracketBalanceValue); if (nestedBlockDeclaration.sourceEnd == 0) return this.methodBody; return this; }
protected void consumeClassBodyDeclaration() { // ClassBodyDeclaration ::= Diet NestedMethod CreateInitializer Block //push an Initializer //optimize the push/pop this.nestedMethod[this.nestedType]--; Block block = (Block) this.astStack[this.astPtr--]; this.astLengthPtr--; if (this.diet) block.bits &= ~ASTNode.UndocumentedEmptyBlock; // clear bit since was diet Initializer initializer = (Initializer) this.astStack[this.astPtr]; initializer.declarationSourceStart = initializer.sourceStart = block.sourceStart; initializer.block = block; this.intPtr--; // pop sourcestart left on the stack by consumeNestedMethod. initializer.bodyStart = this.intStack[this.intPtr--]; this.realBlockPtr--; // pop the block variable counter left on the stack by consumeNestedMethod int javadocCommentStart = this.intStack[this.intPtr--]; if (javadocCommentStart != -1) { initializer.declarationSourceStart = javadocCommentStart; initializer.javadoc = this.javadoc; this.javadoc = null; } initializer.bodyEnd = this.endPosition; initializer.sourceEnd = this.endStatementPosition; initializer.declarationSourceEnd = flushCommentsDefinedPriorTo(this.endStatementPosition); }
protected void consumeStatementSynchronized() { // SynchronizedStatement ::= OnlySynchronized '(' Expression ')' Block //optimize the push/pop if (this.astLengthStack[this.astLengthPtr] == 0) { this.astLengthStack[this.astLengthPtr] = 1; this.expressionLengthPtr--; this.astStack[++this.astPtr] = new SynchronizedStatement( this.expressionStack[this.expressionPtr--], null, this.intStack[this.intPtr--], this.endStatementPosition); } else { this.expressionLengthPtr--; this.astStack[this.astPtr] = new SynchronizedStatement( this.expressionStack[this.expressionPtr--], (Block) this.astStack[this.astPtr], this.intStack[this.intPtr--], this.endStatementPosition); } this.modifiers = ClassFileConstants.AccDefault; this.modifiersSourceStart = -1; // <-- see comment into modifiersFlag(int) }
protected void consumeStaticInitializer() { // StaticInitializer ::= StaticOnly Block //push an Initializer //optimize the push/pop Block block = (Block) this.astStack[this.astPtr]; if (this.diet) block.bits &= ~ASTNode.UndocumentedEmptyBlock; // clear bit set since was diet Initializer initializer = new Initializer(block, ClassFileConstants.AccStatic); this.astStack[this.astPtr] = initializer; initializer.sourceEnd = this.endStatementPosition; initializer.declarationSourceEnd = flushCommentsDefinedPriorTo(this.endStatementPosition); this.nestedMethod[this.nestedType] --; initializer.declarationSourceStart = this.intStack[this.intPtr--]; initializer.bodyStart = this.intStack[this.intPtr--]; initializer.bodyEnd = this.endPosition; // doc comment initializer.javadoc = this.javadoc; this.javadoc = null; // recovery if (this.currentElement != null){ this.lastCheckPoint = initializer.declarationSourceEnd; this.currentElement = this.currentElement.add(initializer, 0); this.lastIgnoredToken = -1; } }
public RecoveredElement add(Block nestedBlockDeclaration, int bracketBalanceValue) { resetPendingModifiers(); /* do not consider a nested block starting passed the block end (if set) it must be belonging to an enclosing block */ if (this.blockDeclaration.sourceEnd != 0 && nestedBlockDeclaration.sourceStart > this.blockDeclaration.sourceEnd){ return this.parent.add(nestedBlockDeclaration, bracketBalanceValue); } RecoveredBlock element = new RecoveredBlock(nestedBlockDeclaration, this, bracketBalanceValue); // if we have a pending Argument, promote it into the new block if (this.pendingArgument != null){ element.attach(this.pendingArgument); this.pendingArgument = null; } if(parser().statementRecoveryActivated) { addBlockStatement(element); } attach(element); if (nestedBlockDeclaration.sourceEnd == 0) return element; return this; }
public void visitInitializer(EclipseNode node, Initializer initializer) { Block block = initializer.block; boolean s = (block != null && block.statements != null); print("<%s INITIALIZER: %s%s%s>", (initializer.modifiers & Modifier.STATIC) != 0 ? "static" : "instance", s ? "filled" : "blank", isGenerated(initializer) ? " (GENERATED)" : "", position(node)); indent++; if (printContent) { if (initializer.block != null) print("%s", initializer.block); disablePrinting++; } }
private void generateClearMethod(TypeReference returnType, Statement returnStatement, SingularData data, EclipseNode builderType) { MethodDeclaration md = new MethodDeclaration(((CompilationUnitDeclaration) builderType.top().get()).compilationResult); md.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG; md.modifiers = ClassFileConstants.AccPublic; String pN = new String(data.getPluralName()); char[] keyFieldName = (pN + "$key").toCharArray(); char[] valueFieldName = (pN + "$value").toCharArray(); FieldReference thisDotField = new FieldReference(keyFieldName, 0L); thisDotField.receiver = new ThisReference(0, 0); FieldReference thisDotField2 = new FieldReference(keyFieldName, 0L); thisDotField2.receiver = new ThisReference(0, 0); FieldReference thisDotField3 = new FieldReference(valueFieldName, 0L); thisDotField3.receiver = new ThisReference(0, 0); md.selector = HandlerUtil.buildAccessorName("clear", new String(data.getPluralName())).toCharArray(); MessageSend clearMsg1 = new MessageSend(); clearMsg1.receiver = thisDotField2; clearMsg1.selector = "clear".toCharArray(); MessageSend clearMsg2 = new MessageSend(); clearMsg2.receiver = thisDotField3; clearMsg2.selector = "clear".toCharArray(); Block clearMsgs = new Block(2); clearMsgs.statements = new Statement[] {clearMsg1, clearMsg2}; Statement clearStatement = new IfStatement(new EqualExpression(thisDotField, new NullLiteral(0, 0), OperatorIds.NOT_EQUAL), clearMsgs, 0, 0); md.statements = returnStatement != null ? new Statement[] {clearStatement, returnStatement} : new Statement[] {clearStatement}; md.returnType = returnType; injectMethod(builderType, md); }
private Initializer convert( InitializerElementInfo initializerInfo, CompilationResult compilationResult) throws JavaModelException { Block block = new Block(0); Initializer initializer = new Initializer(block, ClassFileConstants.AccDefault); int start = initializerInfo.getDeclarationSourceStart(); int end = initializerInfo.getDeclarationSourceEnd(); initializer.sourceStart = initializer.declarationSourceStart = start; initializer.sourceEnd = initializer.declarationSourceEnd = end; initializer.modifiers = initializerInfo.getModifiers(); /* convert local and anonymous types */ IJavaElement[] children = initializerInfo.getChildren(); int typesLength = children.length; if (typesLength > 0) { Statement[] statements = new Statement[typesLength]; for (int i = 0; i < typesLength; i++) { SourceType type = (SourceType) children[i]; TypeDeclaration localType = convert(type, compilationResult); if ((localType.bits & ASTNode.IsAnonymousType) != 0) { QualifiedAllocationExpression expression = new QualifiedAllocationExpression(localType); expression.type = localType.superclass; localType.superclass = null; localType.superInterfaces = null; localType.allocation = expression; statements[i] = expression; } else { statements[i] = localType; } } block.statements = statements; } return initializer; }
private void formatGuardClauseBlock(Block block, BlockScope scope) { this.scribe.printNextToken(TerminalTokens.TokenNameLBRACE, this.preferences.insert_space_before_opening_brace_in_block); this.scribe.space(); final Statement[] statements = block.statements; statements[0].traverse(this, scope); this.scribe.printNextToken(TerminalTokens.TokenNameRBRACE, true); this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT); }
/** * @see org.eclipse.jdt.internal.compiler.ASTVisitor#visit(org.eclipse.jdt.internal.compiler.ast.LambdaExpression, org.eclipse.jdt.internal.compiler.lookup.BlockScope) */ public boolean visit(LambdaExpression lambdaExpression, BlockScope scope) { final int numberOfParens = (lambdaExpression.bits & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT; if (numberOfParens > 0) { manageOpeningParenthesizedExpression(lambdaExpression, numberOfParens); } if (isNextToken(TerminalTokens.TokenNameLPAREN)) { // Format arguments formatMethodArguments( null, lambdaExpression.arguments(), lambdaExpression.getScope(), this.preferences.insert_space_before_opening_paren_in_method_declaration, this.preferences.insert_space_between_empty_parens_in_method_declaration, this.preferences.insert_space_before_closing_paren_in_method_declaration, this.preferences.insert_space_after_opening_paren_in_method_declaration, this.preferences.insert_space_before_comma_in_method_declaration_parameters, this.preferences.insert_space_after_comma_in_method_declaration_parameters, this.preferences.alignment_for_parameters_in_method_declaration); } else { // This MUST be a single, untyped parameter this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier); } if (this.preferences.insert_space_before_lambda_arrow) this.scribe.space(); this.scribe.printNextToken(TerminalTokens.TokenNameARROW); if (this.preferences.insert_space_after_lambda_arrow) this.scribe.space(); final Statement body = lambdaExpression.body(); if (body instanceof Block) { formatBlock((Block) body, scope, this.preferences.brace_position_for_lambda_body, this.preferences.insert_space_before_opening_brace_in_block); } else { body.traverse(this, scope); } if (numberOfParens > 0) { manageClosingParenthesizedExpression(lambdaExpression, numberOfParens); } return false; }
public RecoveredElement add(AbstractMethodDeclaration methodDeclaration, int bracketBalanceValue) { /* attach it to last type - if any */ if (this.typeCount > 0){ RecoveredType type = this.types[this.typeCount -1]; int start = type.bodyEnd; int end = type.typeDeclaration.bodyEnd; type.bodyEnd = 0; // reset position type.typeDeclaration.declarationSourceEnd = 0; // reset position type.typeDeclaration.bodyEnd = 0; int kind = TypeDeclaration.kind(type.typeDeclaration.modifiers); if(start > 0 && start < end && kind != TypeDeclaration.INTERFACE_DECL && kind != TypeDeclaration.ANNOTATION_TYPE_DECL) { // the } of the last type can be considered as the end of an initializer Initializer initializer = new Initializer(new Block(0), 0); initializer.bodyStart = end; initializer.bodyEnd = end; initializer.declarationSourceStart = end; initializer.declarationSourceEnd = end; initializer.sourceStart = end; initializer.sourceEnd = end; type.add(initializer, bracketBalanceValue); } resetPendingModifiers(); return type.add(methodDeclaration, bracketBalanceValue); } return this; // ignore }
public RecoveredElement add(Block nestedBlockDeclaration, int bracketBalanceValue) { /* default behavior is to delegate recording to parent if any */ resetPendingModifiers(); if (this.parent == null) return this; // ignore this.updateSourceEndIfNecessary(previousAvailableLineEnd(nestedBlockDeclaration.sourceStart - 1)); return this.parent.add(nestedBlockDeclaration, bracketBalanceValue); }
public RecoveredElement add(Block nestedBlockDeclaration,int bracketBalanceValue) { this.pendingTypeParameters = null; resetPendingModifiers(); int mods = ClassFileConstants.AccDefault; if(parser().recoveredStaticInitializerStart != 0) { mods = ClassFileConstants.AccStatic; } return this.add(new Initializer(nestedBlockDeclaration, mods), bracketBalanceValue); }
public RecoveredElement add(Statement statement, int bracketBalanceValue) { resetPendingModifiers(); /* do not consider a type starting passed the type end (if set) it must be belonging to an enclosing type */ if (this.methodDeclaration.declarationSourceEnd != 0 && statement.sourceStart > this.methodDeclaration.declarationSourceEnd){ if (this.parent == null) { return this; // ignore } else { return this.parent.add(statement, bracketBalanceValue); } } if (this.methodBody == null){ Block block = new Block(0); block.sourceStart = this.methodDeclaration.bodyStart; RecoveredElement currentBlock = this.add(block, 1); if (this.bracketBalance > 0){ for (int i = 0; i < this.bracketBalance - 1; i++){ currentBlock = currentBlock.add(new Block(0), 1); } this.bracketBalance = 1; } return currentBlock.add(statement, bracketBalanceValue); } return this.methodBody.add(statement, bracketBalanceValue, true); }
public RecoveredBlock(Block block, RecoveredElement parent, int bracketBalance){ super(block, parent, bracketBalance); this.blockDeclaration = block; this.foundOpeningBrace = true; this.preserveContent = parser().methodRecoveryActivated || parser().statementRecoveryActivated; }
public RecoveredElement updateOnOpeningBrace(int braceStart, int braceEnd){ // create a nested block Block block = new Block(0); block.sourceStart = parser().scanner.startPosition; return this.add(block, 1); }
public void finallyMustCompleteNormally(Block finallyBlock) { this.handle( IProblem.FinallyMustCompleteNormally, NoArgument, NoArgument, finallyBlock.sourceStart, finallyBlock.sourceEnd); }
private boolean methodHasMissingSwitchDefault() { MethodScope methodScope = null; if (this.referenceContext instanceof Block) { methodScope = ((Block)this.referenceContext).scope.methodScope(); } else if (this.referenceContext instanceof AbstractMethodDeclaration) { methodScope = ((AbstractMethodDeclaration)this.referenceContext).scope; } return methodScope != null && methodScope.hasMissingSwitchDefault; }