/** * Checks if there is a (non-default) constructor. In case of multiple constructors (overloading), only * the first constructor decides if EXISTS_BY_USER or EXISTS_BY_LOMBOK is returned. * * @param node Any node that represents the Type (TypeDeclaration) to look in, or any child node thereof. */ public static MemberExistsResult constructorExists(EclipseNode node) { while (node != null && !(node.get() instanceof TypeDeclaration)) { node = node.up(); } if (node != null && node.get() instanceof TypeDeclaration) { TypeDeclaration typeDecl = (TypeDeclaration)node.get(); if (typeDecl.methods != null) top: for (AbstractMethodDeclaration def : typeDecl.methods) { if (def instanceof ConstructorDeclaration) { if ((def.bits & ASTNode.IsDefaultConstructor) != 0) continue; if (def.annotations != null) for (Annotation anno : def.annotations) { if (typeMatches(Tolerate.class, node, anno.type)) continue top; } return getGeneratedBy(def) == null ? MemberExistsResult.EXISTS_BY_USER : MemberExistsResult.EXISTS_BY_LOMBOK; } } } return MemberExistsResult.NOT_EXISTS; }
static VariableKind kind(lombok.ast.VariableDefinition node) { //TODO rewrite this whole thing. lombok.ast.Node parent = node.getParent(); if (parent instanceof lombok.ast.VariableDeclaration) { if (parent.getParent() instanceof lombok.ast.TypeBody || parent.getParent() instanceof lombok.ast.EnumTypeBody) { return FIELD; } else { return LOCAL; } } if (parent instanceof lombok.ast.For || parent instanceof lombok.ast.ForEach){ return LOCAL; } if (parent instanceof lombok.ast.Catch || parent instanceof lombok.ast.MethodDeclaration || parent instanceof lombok.ast.ConstructorDeclaration){ return ARGUMENT; } return UNSUPPORTED; }
private void visitIfNeeded(AbstractMethodDeclaration method) { if (this.localDeclarationVisitor != null && (method.bits & ASTNode.HasLocalType) != 0) { if (method instanceof ConstructorDeclaration) { ConstructorDeclaration constructorDeclaration = (ConstructorDeclaration) method; if (constructorDeclaration.constructorCall != null) { constructorDeclaration.constructorCall.traverse(this.localDeclarationVisitor, method.scope); } } if (method.statements != null) { int statementsLength = method.statements.length; for (int i = 0; i < statementsLength; i++) method.statements[i].traverse(this.localDeclarationVisitor, method.scope); } } }
void attach(TypeParameter[] parameters, int startPos) { if(this.methodDeclaration.modifiers != ClassFileConstants.AccDefault) return; int lastParameterEnd = parameters[parameters.length - 1].sourceEnd; Parser parser = parser(); Scanner scanner = parser.scanner; if(Util.getLineNumber(this.methodDeclaration.declarationSourceStart, scanner.lineEnds, 0, scanner.linePtr) != Util.getLineNumber(lastParameterEnd, scanner.lineEnds, 0, scanner.linePtr)) return; if(parser.modifiersSourceStart > lastParameterEnd && parser.modifiersSourceStart < this.methodDeclaration.declarationSourceStart) return; if (this.methodDeclaration instanceof MethodDeclaration) { ((MethodDeclaration)this.methodDeclaration).typeParameters = parameters; this.methodDeclaration.declarationSourceStart = startPos; } else if (this.methodDeclaration instanceof ConstructorDeclaration){ ((ConstructorDeclaration)this.methodDeclaration).typeParameters = parameters; this.methodDeclaration.declarationSourceStart = startPos; } }
public MethodDeclaration convertToMethodDeclaration(ConstructorDeclaration c, CompilationResult compilationResult) { MethodDeclaration m = new MethodDeclaration(compilationResult); m.typeParameters = c.typeParameters; m.sourceStart = c.sourceStart; m.sourceEnd = c.sourceEnd; m.bodyStart = c.bodyStart; m.bodyEnd = c.bodyEnd; m.declarationSourceEnd = c.declarationSourceEnd; m.declarationSourceStart = c.declarationSourceStart; m.selector = c.selector; m.statements = c.statements; m.modifiers = c.modifiers; m.annotations = c.annotations; m.arguments = c.arguments; m.thrownExceptions = c.thrownExceptions; m.explicitDeclarations = c.explicitDeclarations; m.returnType = null; m.javadoc = c.javadoc; m.bits = c.bits; return m; }
public void parseErrorUnexpectedEnd( int start, int end){ String[] arguments; if(this.referenceContext instanceof ConstructorDeclaration) { arguments = new String[] {Messages.parser_endOfConstructor}; } else if(this.referenceContext instanceof MethodDeclaration) { arguments = new String[] {Messages.parser_endOfMethod}; } else if(this.referenceContext instanceof TypeDeclaration) { arguments = new String[] {Messages.parser_endOfInitializer}; } else { arguments = new String[] {Messages.parser_endOfFile}; } this.handle( IProblem.ParsingErrorUnexpectedEOF, arguments, arguments, start, end); }
public void unhandledException(TypeBinding exceptionType, ASTNode location) { boolean insideDefaultConstructor = (this.referenceContext instanceof ConstructorDeclaration) && ((ConstructorDeclaration)this.referenceContext).isDefaultConstructor(); boolean insideImplicitConstructorCall = (location instanceof ExplicitConstructorCall) && (((ExplicitConstructorCall) location).accessMode == ExplicitConstructorCall.ImplicitSuper); int sourceEnd = location.sourceEnd; if (location instanceof LocalDeclaration) { sourceEnd = ((LocalDeclaration) location).declarationEnd; } this.handle( insideDefaultConstructor ? IProblem.UnhandledExceptionInDefaultConstructor : (insideImplicitConstructorCall ? IProblem.UndefinedConstructorInImplicitConstructorCall : IProblem.UnhandledException), new String[] {new String(exceptionType.readableName())}, new String[] {new String(exceptionType.shortReadableName())}, location.sourceStart, sourceEnd); }
public void unusedPrivateConstructor(ConstructorDeclaration constructorDecl) { int severity = computeSeverity(IProblem.UnusedPrivateConstructor); if (severity == ProblemSeverities.Ignore) return; if (excludeDueToAnnotation(constructorDecl.annotations, IProblem.UnusedPrivateConstructor)) return; MethodBinding constructor = constructorDecl.binding; this.handle( IProblem.UnusedPrivateConstructor, new String[] { new String(constructor.declaringClass.readableName()), typesAsString(constructor, false) }, new String[] { new String(constructor.declaringClass.shortReadableName()), typesAsString(constructor, true) }, severity, constructorDecl.sourceStart, constructorDecl.sourceEnd); }
@Override public boolean visit(ConstructorDeclaration constructorDeclaration, ClassScope scope) { Annotation[] annotations = constructorDeclaration.annotations; if (annotations != null) { MethodBinding constructorBinding = constructorDeclaration.binding; if (constructorBinding == null) { return false; } ((SourceTypeBinding) constructorBinding.declaringClass).resolveTypesFor(constructorBinding); this.resolveAnnotations( constructorDeclaration.scope, annotations, constructorBinding); } Argument[] arguments = constructorDeclaration.arguments; if (arguments != null) { int argumentLength = arguments.length; for (int i = 0; i < argumentLength; i++) { arguments[i].traverse(this, constructorDeclaration.scope); } } return false; }
public void unusedPrivateConstructor(ConstructorDeclaration constructorDecl) { int severity = computeSeverity(IProblem.UnusedPrivateConstructor); if (severity == ProblemSeverities.Ignore) return; if (excludeDueToAnnotation(constructorDecl.annotations)) return; MethodBinding constructor = constructorDecl.binding; this.handle( IProblem.UnusedPrivateConstructor, new String[] { new String(constructor.declaringClass.readableName()), typesAsString(constructor, false) }, new String[] { new String(constructor.declaringClass.shortReadableName()), typesAsString(constructor, true) }, severity, constructorDecl.sourceStart, constructorDecl.sourceEnd); }
/** * Checks if there is a (non-default) constructor. In case of multiple constructors (overloading), only * the first constructor decides if EXISTS_BY_USER or EXISTS_BY_LOMBOK is returned. * * @param node Any node that represents the Type (TypeDeclaration) to look in, or any child node thereof. */ public static MemberExistsResult constructorExists(EclipseNode node) { while (node != null && !(node.get() instanceof TypeDeclaration)) { node = node.up(); } if (node != null && node.get() instanceof TypeDeclaration) { TypeDeclaration typeDecl = (TypeDeclaration)node.get(); if (typeDecl.methods != null) for (AbstractMethodDeclaration def : typeDecl.methods) { if (def instanceof ConstructorDeclaration) { if ((def.bits & ASTNode.IsDefaultConstructor) != 0) continue; return getGeneratedBy(def) == null ? MemberExistsResult.EXISTS_BY_USER : MemberExistsResult.EXISTS_BY_LOMBOK; } } } return MemberExistsResult.NOT_EXISTS; }
public void visitMethod(EclipseNode node, AbstractMethodDeclaration method) { String type = method instanceof ConstructorDeclaration ? "CONSTRUCTOR" : "METHOD"; print("<%s %s: %s%s%s>", type, str(method.selector), method.statements != null ? "filled" : "blank", isGenerated(method) ? " (GENERATED)" : "", position(node)); indent++; if (printContent) { if (method.statements != null) print("%s", method); disablePrinting++; } }
private EclipseNode buildMethod(AbstractMethodDeclaration method) { if (setAndGetAsHandled(method)) return null; List<EclipseNode> childNodes = new ArrayList<EclipseNode>(); childNodes.addAll(buildArguments(method.arguments)); if (method instanceof ConstructorDeclaration) { ConstructorDeclaration constructor = (ConstructorDeclaration) method; addIfNotNull(childNodes, buildStatement(constructor.constructorCall)); } childNodes.addAll(buildStatements(method.statements)); childNodes.addAll(buildAnnotations(method.annotations, false)); return putInMap(new EclipseNode(this, method, childNodes, Kind.METHOD)); }
private void createPrivateDefaultConstructor(EclipseNode typeNode, EclipseNode sourceNode) { ASTNode source = sourceNode.get(); TypeDeclaration typeDeclaration = ((TypeDeclaration) typeNode.get()); long p = (long) source.sourceStart << 32 | source.sourceEnd; ConstructorDeclaration constructor = new ConstructorDeclaration(((CompilationUnitDeclaration) typeNode.top().get()).compilationResult); constructor.modifiers = ClassFileConstants.AccPrivate; constructor.selector = typeDeclaration.name; constructor.constructorCall = new ExplicitConstructorCall(ExplicitConstructorCall.ImplicitSuper); constructor.constructorCall.sourceStart = source.sourceStart; constructor.constructorCall.sourceEnd = source.sourceEnd; constructor.thrownExceptions = null; constructor.typeParameters = null; constructor.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG; constructor.bodyStart = constructor.declarationSourceStart = constructor.sourceStart = source.sourceStart; constructor.bodyEnd = constructor.declarationSourceEnd = constructor.sourceEnd = source.sourceEnd; constructor.arguments = null; AllocationExpression exception = new AllocationExpression(); setGeneratedBy(exception, source); long[] ps = new long[JAVA_LANG_UNSUPPORTED_OPERATION_EXCEPTION.length]; Arrays.fill(ps, p); exception.type = new QualifiedTypeReference(JAVA_LANG_UNSUPPORTED_OPERATION_EXCEPTION, ps); setGeneratedBy(exception.type, source); exception.arguments = new Expression[] { new StringLiteral(UNSUPPORTED_MESSAGE, source.sourceStart, source.sourceEnd, 0) }; setGeneratedBy(exception.arguments[0], source); ThrowStatement throwStatement = new ThrowStatement(exception, source.sourceStart, source.sourceEnd); setGeneratedBy(throwStatement, source); constructor.statements = new Statement[] {throwStatement}; injectMethod(typeNode, constructor); }
public void handleMethod(EclipseNode annotation, AbstractMethodDeclaration method, List<DeclaredException> exceptions) { if (method.isAbstract()) { annotation.addError("@SneakyThrows can only be used on concrete methods."); return; } if (method.statements == null || method.statements.length == 0) { boolean hasConstructorCall = false; if (method instanceof ConstructorDeclaration) { ExplicitConstructorCall constructorCall = ((ConstructorDeclaration) method).constructorCall; hasConstructorCall = constructorCall != null && !constructorCall.isImplicitSuper() && !constructorCall.isImplicitThis(); } if (hasConstructorCall) { annotation.addWarning("Calls to sibling / super constructors are always excluded from @SneakyThrows; @SneakyThrows has been ignored because there is no other code in this constructor."); } else { annotation.addWarning("This method or constructor is empty; @SneakyThrows has been ignored."); } return; } Statement[] contents = method.statements; for (DeclaredException exception : exceptions) { contents = new Statement[] { buildTryCatchBlock(contents, exception, exception.node, method) }; } method.statements = contents; annotation.up().rebuild(); }
/** * Inserts a method into an existing type. The type must represent a {@code TypeDeclaration}. */ public static EclipseNode injectMethod(EclipseNode type, AbstractMethodDeclaration method) { method.annotations = addSuppressWarningsAll(type, method, method.annotations); method.annotations = addGenerated(type, method, method.annotations); TypeDeclaration parent = (TypeDeclaration) type.get(); if (parent.methods == null) { parent.methods = new AbstractMethodDeclaration[1]; parent.methods[0] = method; } else { if (method instanceof ConstructorDeclaration) { for (int i = 0 ; i < parent.methods.length ; i++) { if (parent.methods[i] instanceof ConstructorDeclaration && (parent.methods[i].bits & ASTNode.IsDefaultConstructor) != 0) { EclipseNode tossMe = type.getNodeFor(parent.methods[i]); AbstractMethodDeclaration[] withoutGeneratedConstructor = new AbstractMethodDeclaration[parent.methods.length - 1]; System.arraycopy(parent.methods, 0, withoutGeneratedConstructor, 0, i); System.arraycopy(parent.methods, i + 1, withoutGeneratedConstructor, i, parent.methods.length - i - 1); parent.methods = withoutGeneratedConstructor; if (tossMe != null) tossMe.up().removeChild(tossMe); break; } } } //We insert the method in the last position of the methods registered to the type //When changing this behavior, this may trigger issue #155 and #377 AbstractMethodDeclaration[] newArray = new AbstractMethodDeclaration[parent.methods.length + 1]; System.arraycopy(parent.methods, 0, newArray, 0, parent.methods.length); newArray[parent.methods.length] = method; parent.methods = newArray; } return type.add(method, Kind.METHOD); }
@Override public boolean visit(ConstructorDeclaration constructorDeclaration, ClassScope scope) { Annotation[] annotations = constructorDeclaration.annotations; if (annotations != null) { MethodBinding constructorBinding = constructorDeclaration.binding; if (constructorBinding == null) { return false; } ((SourceTypeBinding) constructorBinding.declaringClass).resolveTypesFor(constructorBinding); this.resolveAnnotations( constructorDeclaration.scope, annotations, constructorBinding); } TypeParameter[] typeParameters = constructorDeclaration.typeParameters; if (typeParameters != null) { int typeParametersLength = typeParameters.length; for (int i = 0; i < typeParametersLength; i++) { typeParameters[i].traverse(this, constructorDeclaration.scope); } } Argument[] arguments = constructorDeclaration.arguments; if (arguments != null) { int argumentLength = arguments.length; for (int i = 0; i < argumentLength; i++) { arguments[i].traverse(this, constructorDeclaration.scope); } } return false; }
public boolean visit(ConstructorDeclaration constructorDeclaration, ClassScope classScope) { if (((constructorDeclaration.bits & ASTNode.IsDefaultConstructor) == 0) && !constructorDeclaration.isClinit()) { removeLocals( constructorDeclaration.arguments, constructorDeclaration.declarationSourceStart, constructorDeclaration.declarationSourceEnd); removeLocals( constructorDeclaration.statements, constructorDeclaration.declarationSourceStart, constructorDeclaration.declarationSourceEnd); } pushParent(constructorDeclaration); return true; }
/** * Parse the block statements inside the given method declaration and try to complete at the * cursor location. */ public void parseBlockStatements(AbstractMethodDeclaration md, CompilationUnitDeclaration unit) { if (md instanceof MethodDeclaration) { parseBlockStatements((MethodDeclaration) md, unit); } else if (md instanceof ConstructorDeclaration) { parseBlockStatements((ConstructorDeclaration) md, unit); } }
private TextEdit formatStatements(String source, int indentationLevel, String lineSeparator, IRegion[] regions, boolean includeComments) { ConstructorDeclaration constructorDeclaration = this.codeSnippetParsingUtil.parseStatements(source.toCharArray(), getDefaultCompilerOptions(), true, false); if (constructorDeclaration.statements == null) { // a problem occured while parsing the source return null; } return internalFormatStatements(source, indentationLevel, lineSeparator, constructorDeclaration, regions, includeComments); }
private TextEdit internalFormatStatements(String source, int indentationLevel, String lineSeparator, ConstructorDeclaration constructorDeclaration, IRegion[] regions, boolean includeComments) { if (lineSeparator != null) { this.preferences.line_separator = lineSeparator; } else { this.preferences.line_separator = Util.LINE_SEPARATOR; } this.preferences.initial_indentation_level = indentationLevel; this.newCodeFormatter = new CodeFormatterVisitor(this.preferences, this.options, regions, this.codeSnippetParsingUtil, includeComments); return this.newCodeFormatter.format(source, constructorDeclaration); }
public int match(ConstructorDeclaration node, MatchingNodeSet nodeSet) { int level = IMPOSSIBLE_MATCH; for (int i = 0, length = this.patternLocators.length; i < length; i++) { int newLevel = this.patternLocators[i].match(node, nodeSet); if (newLevel > level) { if (newLevel == ACCURATE_MATCH) return ACCURATE_MATCH; level = newLevel; } } return level; }
public boolean visit( ConstructorDeclaration constructorDeclaration, ClassScope scope) { if (constructorDeclaration.declarationSourceStart <= this.position && this.position <= constructorDeclaration.declarationSourceEnd) { this.found = constructorDeclaration; return false; } return true; }
protected void consumeInvalidConstructorDeclaration() { // ConstructorDeclaration ::= ConstructorHeader ';' // now we know that the top of stack is a constructorDeclaration ConstructorDeclaration cd = (ConstructorDeclaration) this.astStack[this.astPtr]; cd.bodyEnd = this.endPosition; // position just before the trailing semi-colon cd.declarationSourceEnd = flushCommentsDefinedPriorTo(this.endStatementPosition); // report the problem and continue the parsing - narrowing the problem onto the method cd.modifiers |= ExtraCompilerModifiers.AccSemicolonBody; // remember semi-colon body }
protected void consumeInvalidConstructorDeclaration(boolean hasBody) { // InvalidConstructorDeclaration ::= ConstructorHeader ConstructorBody ==> true // InvalidConstructorDeclaration ::= ConstructorHeader ';' ==> false /* this.astStack : modifiers arguments throws statements this.identifierStack : name ==> this.astStack : MethodDeclaration this.identifierStack : */ if (hasBody) { // pop the position of the { (body of the method) pushed in block decl this.intPtr--; } //statements if (hasBody) { this.realBlockPtr--; } int length; if (hasBody && ((length = this.astLengthStack[this.astLengthPtr--]) != 0)) { this.astPtr -= length; } ConstructorDeclaration constructorDeclaration = (ConstructorDeclaration) this.astStack[this.astPtr]; constructorDeclaration.bodyEnd = this.endStatementPosition; constructorDeclaration.declarationSourceEnd = flushCommentsDefinedPriorTo(this.endStatementPosition); if (!hasBody) { constructorDeclaration.modifiers |= ExtraCompilerModifiers.AccSemicolonBody; } }
public void annotationTypeDeclarationCannotHaveConstructor(ConstructorDeclaration constructorDeclaration) { this.handle( IProblem.AnnotationTypeDeclarationCannotHaveConstructor, NoArgument, NoArgument, constructorDeclaration.sourceStart, constructorDeclaration.sourceEnd); }
public void interfaceCannotHaveConstructors(ConstructorDeclaration constructor) { this.handle( IProblem.InterfaceCannotHaveConstructors, NoArgument, NoArgument, constructor.sourceStart, constructor.sourceEnd, constructor, constructor.compilationResult()); }
public static void describeDeprecatedMethod(StringBuilder desc, SourceCodeChange insert) { if(insert.getChangedEntity() != null && insert.getChangedEntity().getAstNode() != null) { Annotation[] annotations = ((ConstructorDeclaration)insert.getChangedEntity().getAstNode()).annotations; if(annotations != null && annotations.length > 0) { for (Annotation annotation : annotations) { if(annotation.type.toString().equals("Deprecated")) { desc.append("deprecated "); break; } } } } }