@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; } }
public static Annotation[] createConstructorProperties(ASTNode source, Collection<EclipseNode> fields) { if (fields.isEmpty()) return null; int pS = source.sourceStart, pE = source.sourceEnd; long p = (long) pS << 32 | pE; long[] poss = new long[3]; Arrays.fill(poss, p); QualifiedTypeReference constructorPropertiesType = new QualifiedTypeReference(JAVA_BEANS_CONSTRUCTORPROPERTIES, poss); setGeneratedBy(constructorPropertiesType, source); SingleMemberAnnotation ann = new SingleMemberAnnotation(constructorPropertiesType, pS); ann.declarationSourceEnd = pE; ArrayInitializer fieldNames = new ArrayInitializer(); fieldNames.sourceStart = pS; fieldNames.sourceEnd = pE; fieldNames.expressions = new Expression[fields.size()]; int ctr = 0; for (EclipseNode field : fields) { char[] fieldName = removePrefixFromField(field); fieldNames.expressions[ctr] = new StringLiteral(fieldName, pS, pE, 0); setGeneratedBy(fieldNames.expressions[ctr], source); ctr++; } ann.memberValue = fieldNames; setGeneratedBy(ann, source); setGeneratedBy(ann.memberValue, source); return new Annotation[] { ann }; }
@Override public void handle(AnnotationValues<SneakyThrows> annotation, Annotation source, EclipseNode annotationNode) { handleFlagUsage(annotationNode, ConfigurationKeys.SNEAKY_THROWS_FLAG_USAGE, "@SneakyThrows"); List<String> exceptionNames = annotation.getRawExpressions("value"); List<DeclaredException> exceptions = new ArrayList<DeclaredException>(); MemberValuePair[] memberValuePairs = source.memberValuePairs(); if (memberValuePairs == null || memberValuePairs.length == 0) { exceptions.add(new DeclaredException("java.lang.Throwable", source)); } else { Expression arrayOrSingle = memberValuePairs[0].value; final Expression[] exceptionNameNodes; if (arrayOrSingle instanceof ArrayInitializer) { exceptionNameNodes = ((ArrayInitializer)arrayOrSingle).expressions; } else exceptionNameNodes = new Expression[] { arrayOrSingle }; if (exceptionNames.size() != exceptionNameNodes.length) { annotationNode.addError( "LOMBOK BUG: The number of exception classes in the annotation isn't the same pre- and post- guessing."); } int idx = 0; for (String exceptionName : exceptionNames) { if (exceptionName.endsWith(".class")) exceptionName = exceptionName.substring(0, exceptionName.length() - 6); exceptions.add(new DeclaredException(exceptionName, exceptionNameNodes[idx++])); } } EclipseNode owner = annotationNode.up(); switch (owner.getKind()) { // case FIELD: // return handleField(annotationNode, (FieldDeclaration)owner.get(), exceptions); case METHOD: handleMethod(annotationNode, (AbstractMethodDeclaration)owner.get(), exceptions); break; default: annotationNode.addError("@SneakyThrows is legal only on methods and constructors."); } }
@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."); } }
public static Annotation[] createConstructorProperties(ASTNode source, Collection<EclipseNode> fields) { if (fields.isEmpty()) return null; int pS = source.sourceStart, pE = source.sourceEnd; long p = (long)pS << 32 | pE; long[] poss = new long[3]; Arrays.fill(poss, p); QualifiedTypeReference constructorPropertiesType = new QualifiedTypeReference(JAVA_BEANS_CONSTRUCTORPROPERTIES, poss); setGeneratedBy(constructorPropertiesType, source); SingleMemberAnnotation ann = new SingleMemberAnnotation(constructorPropertiesType, pS); ann.declarationSourceEnd = pE; ArrayInitializer fieldNames = new ArrayInitializer(); fieldNames.sourceStart = pS; fieldNames.sourceEnd = pE; fieldNames.expressions = new Expression[fields.size()]; int ctr = 0; for (EclipseNode field : fields) { char[] fieldName = removePrefixFromField(field); fieldNames.expressions[ctr] = new StringLiteral(fieldName, pS, pE, 0); setGeneratedBy(fieldNames.expressions[ctr], source); ctr++; } ann.memberValue = fieldNames; setGeneratedBy(ann, source); setGeneratedBy(ann.memberValue, source); return new Annotation[] { ann }; }
public void arrayInitializer(int length) { //length is the size of the array Initializer //expressionPtr points on the last elt of the arrayInitializer, // in other words, it has not been decremented yet. ArrayInitializer ai = new ArrayInitializer(); if (length != 0) { this.expressionPtr -= length; System.arraycopy(this.expressionStack, this.expressionPtr + 1, ai.expressions = new Expression[length], 0, length); } pushOnExpressionStack(ai); //positionning ai.sourceEnd = this.endStatementPosition; ai.sourceStart = this.intStack[this.intPtr--]; }
protected void consumeArrayCreationExpressionWithInitializer() { // ArrayCreationWithArrayInitializer ::= 'new' PrimitiveType DimWithOrWithOutExprs ArrayInitializer // ArrayCreationWithArrayInitializer ::= 'new' ClassOrInterfaceType DimWithOrWithOutExprs ArrayInitializer int length; ArrayAllocationExpression arrayAllocation = new ArrayAllocationExpression(); this.expressionLengthPtr -- ; arrayAllocation.initializer = (ArrayInitializer) this.expressionStack[this.expressionPtr--]; length = (this.expressionLengthStack[this.expressionLengthPtr--]); this.expressionPtr -= length ; System.arraycopy( this.expressionStack, this.expressionPtr+1, arrayAllocation.dimensions = new Expression[length], 0, length); Annotation[][] annotationsOnDimensions = getAnnotationsOnDimensions(length); arrayAllocation.annotationsOnDimensions = annotationsOnDimensions; arrayAllocation.type = getTypeReference(0); arrayAllocation.type.bits |= ASTNode.IgnoreRawTypeCheck; // no need to worry about raw type usage if (annotationsOnDimensions != null) { arrayAllocation.bits |= ASTNode.HasTypeAnnotations; arrayAllocation.type.bits |= ASTNode.HasTypeAnnotations; } arrayAllocation.sourceStart = this.intStack[this.intPtr--]; if (arrayAllocation.initializer == null) { arrayAllocation.sourceEnd = this.endStatementPosition; } else { arrayAllocation.sourceEnd = arrayAllocation.initializer.sourceEnd ; } pushOnExpressionStack(arrayAllocation); }
protected void ignoreExpressionAssignment() { // Assignment ::= InvalidArrayInitializerAssignement // encoded operator would be: this.intStack[this.intPtr] this.intPtr--; ArrayInitializer arrayInitializer = (ArrayInitializer) this.expressionStack[this.expressionPtr--]; this.expressionLengthPtr -- ; // report a syntax error and abort parsing if(!this.statementRecoveryActivated) problemReporter().arrayConstantsOnlyInArrayInitializers(arrayInitializer.sourceStart, arrayInitializer.sourceEnd); }
/** * @see org.eclipse.jdt.internal.compiler.ASTVisitor#visit(org.eclipse.jdt.internal.compiler.ast.ArrayAllocationExpression, org.eclipse.jdt.internal.compiler.lookup.BlockScope) */ public boolean visit( ArrayAllocationExpression arrayAllocationExpression, BlockScope scope) { final int numberOfParens = (arrayAllocationExpression.bits & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT; if (numberOfParens > 0) { manageOpeningParenthesizedExpression(arrayAllocationExpression, numberOfParens); } this.scribe.printNextToken(TerminalTokens.TokenNamenew); this.scribe.space(); arrayAllocationExpression.type.traverse(this, scope); final Expression[] dimensions = arrayAllocationExpression.dimensions; int dimensionsLength = dimensions.length; for (int i = 0; i < dimensionsLength; i++) { if (this.preferences.insert_space_before_opening_bracket_in_array_allocation_expression) { this.scribe.space(); } this.scribe.printNextToken(TerminalTokens.TokenNameLBRACKET, false); if (dimensions[i] != null) { if (this.preferences.insert_space_after_opening_bracket_in_array_allocation_expression) { this.scribe.space(); } dimensions[i].traverse(this, scope); this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET, this.preferences.insert_space_before_closing_bracket_in_array_allocation_expression); } else { this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET, this.preferences.insert_space_between_empty_brackets_in_array_allocation_expression); } } final ArrayInitializer initializer = arrayAllocationExpression.initializer; if (initializer != null) { initializer.traverse(this, scope); } if (numberOfParens > 0) { manageClosingParenthesizedExpression(arrayAllocationExpression, numberOfParens); } return false; }
@Override public void endVisit(ArrayInitializer x, BlockScope scope) { try { SourceInfo info = makeSourceInfo(x); JArrayType type = (JArrayType) typeMap.get(x.resolvedType); List<JExpression> expressions = pop(x.expressions); push(JNewArray.createInitializers(info, type, expressions)); } catch (Throwable e) { throw translateException(x, e); } }
private static Annotation[] createConstructorProperties(ASTNode source, Collection<EclipseNode> fields) { if (fields.isEmpty()) return null; int pS = source.sourceStart, pE = source.sourceEnd; long p = (long)pS << 32 | pE; long[] poss = new long[3]; Arrays.fill(poss, p); QualifiedTypeReference constructorPropertiesType = new QualifiedTypeReference(JAVA_BEANS_CONSTRUCTORPROPERTIES, poss); setGeneratedBy(constructorPropertiesType, source); SingleMemberAnnotation ann = new SingleMemberAnnotation(constructorPropertiesType, pS); ann.declarationSourceEnd = pE; ArrayInitializer fieldNames = new ArrayInitializer(); fieldNames.sourceStart = pS; fieldNames.sourceEnd = pE; fieldNames.expressions = new Expression[fields.size()]; int ctr = 0; for (EclipseNode field : fields) { fieldNames.expressions[ctr] = new StringLiteral(field.getName().toCharArray(), pS, pE, 0); setGeneratedBy(fieldNames.expressions[ctr], source); ctr++; } ann.memberValue = fieldNames; setGeneratedBy(ann, source); setGeneratedBy(ann.memberValue, source); return new Annotation[] { ann }; }
@Override public void handle(AnnotationValues<SneakyThrows> annotation, Annotation source, EclipseNode annotationNode) { List<String> exceptionNames = annotation.getRawExpressions("value"); List<DeclaredException> exceptions = new ArrayList<DeclaredException>(); MemberValuePair[] memberValuePairs = source.memberValuePairs(); if (memberValuePairs == null || memberValuePairs.length == 0) { exceptions.add(new DeclaredException("java.lang.Throwable", source)); } else { Expression arrayOrSingle = memberValuePairs[0].value; final Expression[] exceptionNameNodes; if (arrayOrSingle instanceof ArrayInitializer) { exceptionNameNodes = ((ArrayInitializer)arrayOrSingle).expressions; } else exceptionNameNodes = new Expression[] { arrayOrSingle }; if (exceptionNames.size() != exceptionNameNodes.length) { annotationNode.addError( "LOMBOK BUG: The number of exception classes in the annotation isn't the same pre- and post- guessing."); } int idx = 0; for (String exceptionName : exceptionNames) { if (exceptionName.endsWith(".class")) exceptionName = exceptionName.substring(0, exceptionName.length() - 6); exceptions.add(new DeclaredException(exceptionName, exceptionNameNodes[idx++])); } } EclipseNode owner = annotationNode.up(); switch (owner.getKind()) { // case FIELD: // return handleField(annotationNode, (FieldDeclaration)owner.get(), exceptions); case METHOD: handleMethod(annotationNode, (AbstractMethodDeclaration)owner.get(), exceptions); break; default: annotationNode.addError("@SneakyThrows is legal only on methods and constructors."); } }
@Override public boolean visit(ArrayInitializer node, BlockScope scope) { fixPositions(setGeneratedBy(node, source)); return super.visit(node, scope); }
@Override public boolean visit(ArrayInitializer node, ClassScope scope) { fixPositions(setGeneratedBy(node, source)); return super.visit(node, scope); }
private FieldDeclaration convert( SourceField fieldHandle, TypeDeclaration type, CompilationResult compilationResult) throws JavaModelException { SourceFieldElementInfo fieldInfo = (SourceFieldElementInfo) fieldHandle.getElementInfo(); FieldDeclaration field = new FieldDeclaration(); int start = fieldInfo.getNameSourceStart(); int end = fieldInfo.getNameSourceEnd(); field.name = fieldHandle.getElementName().toCharArray(); field.sourceStart = start; field.sourceEnd = end; field.declarationSourceStart = fieldInfo.getDeclarationSourceStart(); field.declarationSourceEnd = fieldInfo.getDeclarationSourceEnd(); int modifiers = fieldInfo.getModifiers(); boolean isEnumConstant = (modifiers & ClassFileConstants.AccEnum) != 0; if (isEnumConstant) { field.modifiers = modifiers & ~ClassFileConstants.AccEnum; // clear AccEnum bit onto AST (binding will add it) } else { field.modifiers = modifiers; field.type = createTypeReference(fieldInfo.getTypeName(), start, end); } // convert 1.5 specific constructs only if compliance is 1.5 or above if (this.has1_5Compliance) { /* convert annotations */ field.annotations = convertAnnotations(fieldHandle); } /* conversion of field constant */ if ((this.flags & FIELD_INITIALIZATION) != 0) { char[] initializationSource = fieldInfo.getInitializationSource(); if (initializationSource != null) { if (this.parser == null) { this.parser = new Parser(this.problemReporter, true); } this.parser.parse(field, type, this.unit, initializationSource); } } /* conversion of local and anonymous types */ if ((this.flags & LOCAL_TYPE) != 0) { IJavaElement[] children = fieldInfo.getChildren(); int childrenLength = children.length; if (childrenLength == 1) { field.initialization = convert(children[0], isEnumConstant ? field : null, compilationResult); } else if (childrenLength > 1) { ArrayInitializer initializer = new ArrayInitializer(); field.initialization = initializer; Expression[] expressions = new Expression[childrenLength]; initializer.expressions = expressions; for (int i = 0; i < childrenLength; i++) { expressions[i] = convert(children[i], isEnumConstant ? field : null, compilationResult); } } } return field; }
public boolean visit(ArrayInitializer arrayInitializer, BlockScope scope) { addRealFragment(arrayInitializer); return false; }
/** * @see org.eclipse.jdt.internal.compiler.ASTVisitor#visit(org.eclipse.jdt.internal.compiler.ast.ArrayAllocationExpression, org.eclipse.jdt.internal.compiler.lookup.BlockScope) */ public boolean visit( ArrayAllocationExpression arrayAllocationExpression, BlockScope scope) { final int numberOfParens = (arrayAllocationExpression.bits & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT; if (numberOfParens > 0) { manageOpeningParenthesizedExpression(arrayAllocationExpression, numberOfParens); } this.scribe.printNextToken(TerminalTokens.TokenNamenew); this.scribe.space(); arrayAllocationExpression.type.traverse(this, scope); final Expression[] dimensions = arrayAllocationExpression.dimensions; int dimensionsLength = dimensions.length; for (int i = 0; i < dimensionsLength; i++) { if (arrayAllocationExpression.annotationsOnDimensions != null) { formatInlineAnnotations(arrayAllocationExpression.annotationsOnDimensions[i], true); } if (this.preferences.insert_space_before_opening_bracket_in_array_allocation_expression) { this.scribe.space(); } this.scribe.printNextToken(TerminalTokens.TokenNameLBRACKET, false); if (dimensions[i] != null) { if (this.preferences.insert_space_after_opening_bracket_in_array_allocation_expression) { this.scribe.space(); } dimensions[i].traverse(this, scope); this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET, this.preferences.insert_space_before_closing_bracket_in_array_allocation_expression); } else { this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET, this.preferences.insert_space_between_empty_brackets_in_array_allocation_expression); } } final ArrayInitializer initializer = arrayAllocationExpression.initializer; if (initializer != null) { initializer.traverse(this, scope); } if (numberOfParens > 0) { manageClosingParenthesizedExpression(arrayAllocationExpression, numberOfParens); } return false; }
protected Object getMemberValue(org.eclipse.jdt.internal.core.MemberValuePair memberValuePair, Expression expression) { if (expression instanceof NullLiteral) { return null; } else if (expression instanceof Literal) { ((Literal) expression).computeConstant(); return Util.getAnnotationMemberValue(memberValuePair, expression.constant); } else if (expression instanceof org.eclipse.jdt.internal.compiler.ast.Annotation) { org.eclipse.jdt.internal.compiler.ast.Annotation annotation = (org.eclipse.jdt.internal.compiler.ast.Annotation) expression; Object handle = acceptAnnotation(annotation, null, (JavaElement) this.handleStack.peek()); memberValuePair.valueKind = IMemberValuePair.K_ANNOTATION; return handle; } else if (expression instanceof ClassLiteralAccess) { ClassLiteralAccess classLiteral = (ClassLiteralAccess) expression; char[] name = CharOperation.concatWith(classLiteral.type.getTypeName(), '.'); memberValuePair.valueKind = IMemberValuePair.K_CLASS; return new String(name); } else if (expression instanceof QualifiedNameReference) { char[] qualifiedName = CharOperation.concatWith(((QualifiedNameReference) expression).tokens, '.'); memberValuePair.valueKind = IMemberValuePair.K_QUALIFIED_NAME; return new String(qualifiedName); } else if (expression instanceof SingleNameReference) { char[] simpleName = ((SingleNameReference) expression).token; if (simpleName == RecoveryScanner.FAKE_IDENTIFIER) { memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN; return null; } memberValuePair.valueKind = IMemberValuePair.K_SIMPLE_NAME; return new String(simpleName); } else if (expression instanceof ArrayInitializer) { memberValuePair.valueKind = -1; // modified below by the first call to getMemberValue(...) Expression[] expressions = ((ArrayInitializer) expression).expressions; int length = expressions == null ? 0 : expressions.length; Object[] values = new Object[length]; for (int i = 0; i < length; i++) { int previousValueKind = memberValuePair.valueKind; Object value = getMemberValue(memberValuePair, expressions[i]); if (previousValueKind != -1 && memberValuePair.valueKind != previousValueKind) { // values are heterogeneous, value kind is thus unknown memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN; } values[i] = value; } if (memberValuePair.valueKind == -1) memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN; return values; } else if (expression instanceof UnaryExpression) { // to deal with negative numerals (see bug - 248312) UnaryExpression unaryExpression = (UnaryExpression) expression; if ((unaryExpression.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT == OperatorIds.MINUS) { if (unaryExpression.expression instanceof Literal) { Literal subExpression = (Literal) unaryExpression.expression; subExpression.computeConstant(); return Util.getNegativeAnnotationMemberValue(memberValuePair, subExpression.constant); } } memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN; return null; } else { memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN; return null; } }
private Object getAnnotationMemberValue(MemberValuePair memberValuePair, Expression expression, JavaElement parentElement) { if (expression instanceof NullLiteral) { return null; } else if (expression instanceof Literal) { ((Literal) expression).computeConstant(); return Util.getAnnotationMemberValue(memberValuePair, expression.constant); } else if (expression instanceof org.eclipse.jdt.internal.compiler.ast.Annotation) { memberValuePair.valueKind = IMemberValuePair.K_ANNOTATION; return getAnnotation((org.eclipse.jdt.internal.compiler.ast.Annotation) expression, parentElement); } else if (expression instanceof ClassLiteralAccess) { ClassLiteralAccess classLiteral = (ClassLiteralAccess) expression; char[] typeName = CharOperation.concatWith(classLiteral.type.getTypeName(), '.'); memberValuePair.valueKind = IMemberValuePair.K_CLASS; return new String(typeName); } else if (expression instanceof QualifiedNameReference) { char[] qualifiedName = CharOperation.concatWith(((QualifiedNameReference) expression).tokens, '.'); memberValuePair.valueKind = IMemberValuePair.K_QUALIFIED_NAME; return new String(qualifiedName); } else if (expression instanceof SingleNameReference) { char[] simpleName = ((SingleNameReference) expression).token; if (simpleName == RecoveryScanner.FAKE_IDENTIFIER) { memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN; return null; } memberValuePair.valueKind = IMemberValuePair.K_SIMPLE_NAME; return new String(simpleName); } else if (expression instanceof ArrayInitializer) { memberValuePair.valueKind = -1; // modified below by the first call to getMemberValue(...) Expression[] expressions = ((ArrayInitializer) expression).expressions; int length = expressions == null ? 0 : expressions.length; Object[] values = new Object[length]; for (int i = 0; i < length; i++) { int previousValueKind = memberValuePair.valueKind; Object value = getAnnotationMemberValue(memberValuePair, expressions[i], parentElement); if (previousValueKind != -1 && memberValuePair.valueKind != previousValueKind) { // values are heterogeneous, value kind is thus unknown memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN; } values[i] = value; } if (memberValuePair.valueKind == -1) memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN; return values; } else if (expression instanceof UnaryExpression) { //to deal with negative numerals (see bug - 248312) UnaryExpression unaryExpression = (UnaryExpression) expression; if ((unaryExpression.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT == OperatorIds.MINUS) { if (unaryExpression.expression instanceof Literal) { Literal subExpression = (Literal) unaryExpression.expression; subExpression.computeConstant(); return Util.getNegativeAnnotationMemberValue(memberValuePair, subExpression.constant); } } memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN; return null; } else { memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN; return null; } }
@Override public boolean visit(ArrayInitializer node, BlockScope scope) { setGeneratedBy(node, source); applyOffsetExpression(node); return super.visit(node, scope); }