public static boolean handleValForForEach(ForeachStatement forEach, BlockScope scope) { if (forEach.elementVariable == null) return false; if (!isVal(forEach.elementVariable.type, scope)) return false; TypeBinding component = getForEachComponentType(forEach.collection, scope); if (component == null) return false; TypeReference replacement = makeType(component, forEach.elementVariable.type, false); forEach.elementVariable.modifiers |= ClassFileConstants.AccFinal; forEach.elementVariable.annotations = addValAnnotation(forEach.elementVariable.annotations, forEach.elementVariable.type, scope); forEach.elementVariable.type = replacement != null ? replacement : new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, poss(forEach.elementVariable.type, 3)); return false; }
public Statement convert(ForeachStatement statement) { switch(this.ast.apiLevel) { case AST.JLS2_INTERNAL : return createFakeEmptyStatement(statement); default : EnhancedForStatement enhancedForStatement = new EnhancedForStatement(this.ast); enhancedForStatement.setParameter(convertToSingleVariableDeclaration(statement.elementVariable)); org.eclipse.jdt.internal.compiler.ast.Expression collection = statement.collection; if (collection == null) return null; enhancedForStatement.setExpression(convert(collection)); final Statement action = convert(statement.action); if (action == null) return null; enhancedForStatement.setBody(action); int start = statement.sourceStart; int end = statement.sourceEnd; enhancedForStatement.setSourceRange(start, end - start + 1); return enhancedForStatement; } }
protected void consumeEnhancedForStatement() { // EnhancedForStatement ::= EnhancedForStatementHeader Statement // EnhancedForStatementNoShortIf ::= EnhancedForStatementHeader StatementNoShortIf //statements this.astLengthPtr--; Statement statement = (Statement) this.astStack[this.astPtr--]; // foreach statement is on the ast stack ForeachStatement foreachStatement = (ForeachStatement) this.astStack[this.astPtr]; foreachStatement.action = statement; // remember useful empty statement if (statement instanceof EmptyStatement) statement.bits |= ASTNode.IsUsefulEmptyStatement; foreachStatement.sourceEnd = this.endStatementPosition; }
protected void consumeEnhancedForStatementHeader(){ // EnhancedForStatementHeader ::= EnhancedForStatementHeaderInit ':' Expression ')' final ForeachStatement statement = (ForeachStatement) this.astStack[this.astPtr]; //updates are on the expression stack this.expressionLengthPtr--; final Expression collection = this.expressionStack[this.expressionPtr--]; statement.collection = collection; // https://bugs.eclipse.org/393719 - [compiler] inconsistent warnings on iteration variables // let declaration(Source)End include the collection to achieve that @SuppressWarnings affects this part, too: statement.elementVariable.declarationSourceEnd = collection.sourceEnd; statement.elementVariable.declarationEnd = collection.sourceEnd; statement.sourceEnd = this.rParenPos; if(!this.statementRecoveryActivated && this.options.sourceLevel < ClassFileConstants.JDK1_5 && this.lastErrorEndPositionBeforeRecovery < this.scanner.currentPosition) { problemReporter().invalidUsageOfForeachStatements(statement.elementVariable, collection); } }
@Override public void visitLocal(EclipseNode localNode, LocalDeclaration local) { if (!EclipseHandlerUtil.typeMatches(val.class, localNode, local.type)) return; boolean variableOfForEach = false; if (localNode.directUp().get() instanceof ForeachStatement) { ForeachStatement fs = (ForeachStatement) localNode.directUp().get(); variableOfForEach = fs.elementVariable == local; } if (local.initialization == null && !variableOfForEach) { localNode.addError("'val' on a local variable requires an initializer expression"); return; } if (local.initialization instanceof ArrayInitializer) { localNode.addError("'val' is not compatible with array initializer expressions. Use the full form (new int[] { ... } instead of just { ... })"); return; } if (localNode.directUp().get() instanceof ForStatement) { localNode.addError("'val' is not allowed in old-style for loops"); return; } }
@Override public void visitLocal(EclipseNode localNode, LocalDeclaration local) { if (!EclipseHandlerUtil.typeMatches(val.class, localNode, local.type)) return; handleFlagUsage(localNode, ConfigurationKeys.VAL_FLAG_USAGE, "val"); boolean variableOfForEach = false; if (localNode.directUp().get() instanceof ForeachStatement) { ForeachStatement fs = (ForeachStatement) localNode.directUp().get(); variableOfForEach = fs.elementVariable == local; } if (local.initialization == null && !variableOfForEach) { localNode.addError("'val' on a local variable requires an initializer expression"); return; } if (local.initialization instanceof ArrayInitializer) { localNode.addError("'val' is not compatible with array initializer expressions. Use the full form (new int[] { ... } instead of just { ... })"); return; } if (localNode.directUp().get() instanceof ForStatement) { localNode.addError("'val' is not allowed in old-style for loops"); return; } if (local.initialization != null && local.initialization.getClass().getName().equals("org.eclipse.jdt.internal.compiler.ast.LambdaExpression")) { localNode.addError("'val' is not allowed with lambda expressions."); } }
protected void consumeEnhancedForStatementHeaderInit(boolean hasModifiers) { super.consumeEnhancedForStatementHeaderInit(hasModifiers); if (this.currentElement != null) { LocalDeclaration localDecl = ((ForeachStatement)this.astStack[this.astPtr]).elementVariable; this.lastCheckPoint = localDecl.sourceEnd + 1; this.currentElement = this.currentElement.add(localDecl, 0); } }
public static boolean handleValForForEach(ForeachStatement forEach, BlockScope scope) { return (Boolean) Util.invokeMethod(HANDLE_VAL_FOR_FOR_EACH, forEach, scope); }
@Override public boolean visit(ForeachStatement node, BlockScope scope) { fixPositions(setGeneratedBy(node, source)); return super.visit(node, scope); }
private void generatePluralMethod(TypeReference returnType, Statement returnStatement, SingularData data, EclipseNode builderType, boolean fluent) { 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(); List<Statement> statements = new ArrayList<Statement>(); statements.add(createConstructBuilderVarIfNeeded(data, builderType, true)); char[] entryName = "$lombokEntry".toCharArray(); TypeReference forEachType = new QualifiedTypeReference(JAVA_UTIL_MAP_ENTRY, NULL_POSS); forEachType = addTypeArgs(2, true, builderType, forEachType, data.getTypeArgs()); MessageSend keyArg = new MessageSend(); keyArg.receiver = new SingleNameReference(entryName, 0L); keyArg.selector = "getKey".toCharArray(); MessageSend addKey = new MessageSend(); FieldReference thisDotKeyField = new FieldReference(keyFieldName, 0L); thisDotKeyField.receiver = new ThisReference(0, 0); addKey.receiver = thisDotKeyField; addKey.selector = new char[] {'a', 'd', 'd'}; addKey.arguments = new Expression[] {keyArg}; MessageSend valueArg = new MessageSend(); valueArg.receiver = new SingleNameReference(entryName, 0L); valueArg.selector = "getValue".toCharArray(); MessageSend addValue = new MessageSend(); FieldReference thisDotValueField = new FieldReference(valueFieldName, 0L); thisDotValueField.receiver = new ThisReference(0, 0); addValue.receiver = thisDotValueField; addValue.selector = new char[] {'a', 'd', 'd'}; addValue.arguments = new Expression[] {valueArg}; LocalDeclaration elementVariable = new LocalDeclaration(entryName, 0, 0); elementVariable.type = forEachType; ForeachStatement forEach = new ForeachStatement(elementVariable, 0); MessageSend invokeEntrySet = new MessageSend(); invokeEntrySet.selector = new char[] { 'e', 'n', 't', 'r', 'y', 'S', 'e', 't'}; invokeEntrySet.receiver = new SingleNameReference(data.getPluralName(), 0L); forEach.collection = invokeEntrySet; Block forEachContent = new Block(0); forEachContent.statements = new Statement[] {addKey, addValue}; forEach.action = forEachContent; statements.add(forEach); if (returnStatement != null) statements.add(returnStatement); md.statements = statements.toArray(new Statement[statements.size()]); TypeReference paramType = new QualifiedTypeReference(JAVA_UTIL_MAP, NULL_POSS); paramType = addTypeArgs(2, true, builderType, paramType, data.getTypeArgs()); Argument param = new Argument(data.getPluralName(), 0, paramType, 0); md.arguments = new Argument[] {param}; md.returnType = returnType; md.selector = fluent ? data.getPluralName() : HandlerUtil.buildAccessorName("putAll", new String(data.getPluralName())).toCharArray(); data.setGeneratedByRecursive(md); injectMethod(builderType, md); }
public boolean visit(ForeachStatement forStatement, BlockScope scope) { this.scribe.printNextToken(TerminalTokens.TokenNamefor); final int line = this.scribe.line; this.scribe.printNextToken(TerminalTokens.TokenNameLPAREN, this.preferences.insert_space_before_opening_paren_in_for); if (this.preferences.insert_space_after_opening_paren_in_for) { this.scribe.space(); } formatLocalDeclaration(forStatement.elementVariable, scope, false, false); this.scribe.printNextToken(TerminalTokens.TokenNameCOLON, this.preferences.insert_space_before_colon_in_for); if (this.preferences.insert_space_after_colon_in_for) { this.scribe.space(); } forStatement.collection.traverse(this, scope); this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_before_closing_paren_in_for); final Statement action = forStatement.action; if (action != null) { if (action instanceof Block) { formatLeftCurlyBrace(line, this.preferences.brace_position_for_block); action.traverse(this, scope); } else if (action instanceof EmptyStatement) { /* * This is an empty statement */ formatNecessaryEmptyStatement(); } else { this.scribe.indent(); this.scribe.printNewLine(); action.traverse(this, scope); this.scribe.unIndent(); } if (action instanceof Expression) { this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon); this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT); } } else { /* * This is an empty statement */ formatNecessaryEmptyStatement(); } return false; }
protected void consumeEnhancedForStatementHeaderInit(boolean hasModifiers) { TypeReference type; char[] identifierName = this.identifierStack[this.identifierPtr]; long namePosition = this.identifierPositionStack[this.identifierPtr]; LocalDeclaration localDeclaration = createLocalDeclaration(identifierName, (int) (namePosition >>> 32), (int) namePosition); localDeclaration.declarationSourceEnd = localDeclaration.declarationEnd; localDeclaration.bits |= ASTNode.IsForeachElementVariable; int extraDims = this.intStack[this.intPtr--]; Annotation [][] annotationsOnExtendedDimensions = extraDims == 0 ? null : getAnnotationsOnDimensions(extraDims); this.identifierPtr--; this.identifierLengthPtr--; // remove fake modifiers/modifiers start int declarationSourceStart = 0; int modifiersValue = 0; if (hasModifiers) { declarationSourceStart = this.intStack[this.intPtr--]; modifiersValue = this.intStack[this.intPtr--]; } else { this.intPtr-=2; } type = getTypeReference(this.intStack[this.intPtr--]); // type dimension // consume annotations int length; if ((length = this.expressionLengthStack[this.expressionLengthPtr--])!= 0) { System.arraycopy( this.expressionStack, (this.expressionPtr -= length) + 1, localDeclaration.annotations = new Annotation[length], 0, length); localDeclaration.bits |= ASTNode.HasTypeAnnotations; } if (extraDims != 0) { type = augmentTypeWithAdditionalDimensions(type, extraDims, annotationsOnExtendedDimensions, false); } if (hasModifiers) { localDeclaration.declarationSourceStart = declarationSourceStart; localDeclaration.modifiers = modifiersValue; } else { localDeclaration.declarationSourceStart = type.sourceStart; } localDeclaration.type = type; localDeclaration.bits |= (type.bits & ASTNode.HasTypeAnnotations); ForeachStatement iteratorForStatement = new ForeachStatement( localDeclaration, this.intStack[this.intPtr--]); pushOnAstStack(iteratorForStatement); iteratorForStatement.sourceEnd = localDeclaration.declarationSourceEnd; }
@Override public boolean visit(ForeachStatement node, BlockScope scope) { setGeneratedBy(node, source); applyOffsetASTNode(node); return super.visit(node, scope); }