List of usage examples for org.eclipse.jdt.core.dom PrimitiveType PrimitiveType
PrimitiveType(AST ast)
From source file:org.eclipse.jdt.core.dom.ASTConverter.java
License:Open Source License
public ASTNode convert(boolean isInterface, org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration methodDeclaration) { checkCanceled();//from w w w .java 2 s . c om if (methodDeclaration instanceof org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration) { return convert((org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration) methodDeclaration); } MethodDeclaration methodDecl = new MethodDeclaration(this.ast); setModifiers(methodDecl, methodDeclaration); boolean isConstructor = methodDeclaration.isConstructor(); methodDecl.setConstructor(isConstructor); final SimpleName methodName = new SimpleName(this.ast); methodName.internalSetIdentifier(new String(methodDeclaration.selector)); int start = methodDeclaration.sourceStart; // GROOVY start // why does this do what it does? /* old { int end = retrieveIdentifierEndPosition(start, methodDeclaration.sourceEnd); } new */ int end = (scannerAvailable(methodDeclaration.scope) ? retrieveIdentifierEndPosition(start, methodDeclaration.sourceEnd) : methodDeclaration.sourceEnd); // GROOVY end methodName.setSourceRange(start, end - start + 1); methodDecl.setName(methodName); org.eclipse.jdt.internal.compiler.ast.TypeReference[] thrownExceptions = methodDeclaration.thrownExceptions; int methodHeaderEnd = methodDeclaration.sourceEnd; int thrownExceptionsLength = thrownExceptions == null ? 0 : thrownExceptions.length; if (thrownExceptionsLength > 0) { Name thrownException; int i = 0; do { thrownException = convert(thrownExceptions[i++]); methodDecl.thrownExceptions().add(thrownException); } while (i < thrownExceptionsLength); methodHeaderEnd = thrownException.getStartPosition() + thrownException.getLength(); } org.eclipse.jdt.internal.compiler.ast.Argument[] parameters = methodDeclaration.arguments; int parametersLength = parameters == null ? 0 : parameters.length; if (parametersLength > 0) { SingleVariableDeclaration parameter; int i = 0; do { // GROOVY start // make sure the scope is available just in case it is necessary for varargs // new code BlockScope origScope = null; if (parameters[i].binding != null) { origScope = parameters[i].binding.declaringScope; parameters[i].binding.declaringScope = methodDeclaration.scope; } // GROOVY end parameter = convert(parameters[i++]); // GROOVY start // unset the scope // new code if (parameters[i - 1].binding != null) { parameters[i - 1].binding.declaringScope = origScope; } // GROOVY end methodDecl.parameters().add(parameter); } while (i < parametersLength); if (thrownExceptionsLength == 0) { methodHeaderEnd = parameter.getStartPosition() + parameter.getLength(); } } org.eclipse.jdt.internal.compiler.ast.ExplicitConstructorCall explicitConstructorCall = null; if (isConstructor) { if (isInterface) { // interface cannot have a constructor methodDecl.setFlags(methodDecl.getFlags() | ASTNode.MALFORMED); } org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration constructorDeclaration = (org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration) methodDeclaration; explicitConstructorCall = constructorDeclaration.constructorCall; switch (this.ast.apiLevel) { case AST.JLS2_INTERNAL: // set the return type to VOID PrimitiveType returnType = new PrimitiveType(this.ast); returnType.setPrimitiveTypeCode(PrimitiveType.VOID); returnType.setSourceRange(methodDeclaration.sourceStart, 0); methodDecl.internalSetReturnType(returnType); break; default: methodDecl.setReturnType2(null); } } else if (methodDeclaration instanceof org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) { org.eclipse.jdt.internal.compiler.ast.MethodDeclaration method = (org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) methodDeclaration; org.eclipse.jdt.internal.compiler.ast.TypeReference typeReference = method.returnType; if (typeReference != null) { Type returnType = convertType(typeReference); // get the positions of the right parenthesis int rightParenthesisPosition = retrieveEndOfRightParenthesisPosition(end, method.bodyEnd); int extraDimensions = retrieveExtraDimension(rightParenthesisPosition, method.bodyEnd); methodDecl.setExtraDimensions(extraDimensions); setTypeForMethodDeclaration(methodDecl, returnType, extraDimensions); } else { // no return type for a method that is not a constructor methodDecl.setFlags(methodDecl.getFlags() | ASTNode.MALFORMED); switch (this.ast.apiLevel) { case AST.JLS2_INTERNAL: break; default: methodDecl.setReturnType2(null); } } } int declarationSourceStart = methodDeclaration.declarationSourceStart; int bodyEnd = methodDeclaration.bodyEnd; methodDecl.setSourceRange(declarationSourceStart, bodyEnd - declarationSourceStart + 1); int declarationSourceEnd = methodDeclaration.declarationSourceEnd; int rightBraceOrSemiColonPositionStart = bodyEnd == declarationSourceEnd ? bodyEnd : bodyEnd + 1; int closingPosition = retrieveRightBraceOrSemiColonPosition(rightBraceOrSemiColonPositionStart, declarationSourceEnd); if (closingPosition != -1) { int startPosition = methodDecl.getStartPosition(); methodDecl.setSourceRange(startPosition, closingPosition - startPosition + 1); org.eclipse.jdt.internal.compiler.ast.Statement[] statements = methodDeclaration.statements; start = retrieveStartBlockPosition(methodHeaderEnd, methodDeclaration.bodyStart); if (start == -1) start = methodDeclaration.bodyStart; // use recovery position for body start end = retrieveRightBrace(methodDeclaration.bodyEnd, declarationSourceEnd); Block block = null; if (start != -1 && end != -1) { /* * start or end can be equal to -1 if we have an interface's method. */ block = new Block(this.ast); block.setSourceRange(start, closingPosition - start + 1); methodDecl.setBody(block); } if (block != null && (statements != null || explicitConstructorCall != null)) { if (explicitConstructorCall != null && explicitConstructorCall.accessMode != org.eclipse.jdt.internal.compiler.ast.ExplicitConstructorCall.ImplicitSuper) { block.statements().add(convert(explicitConstructorCall)); } int statementsLength = statements == null ? 0 : statements.length; for (int i = 0; i < statementsLength; i++) { if (statements[i] instanceof org.eclipse.jdt.internal.compiler.ast.LocalDeclaration) { checkAndAddMultipleLocalDeclaration(statements, i, block.statements()); } else { final Statement statement = convert(statements[i]); if (statement != null) { block.statements().add(statement); } } } } if (block != null && (Modifier.isAbstract(methodDecl.getModifiers()) || Modifier.isNative(methodDecl.getModifiers()) || isInterface)) { methodDecl.setFlags(methodDecl.getFlags() | ASTNode.MALFORMED); } } else { // syntax error in this method declaration methodDecl.setFlags(methodDecl.getFlags() | ASTNode.MALFORMED); if (!methodDeclaration.isNative() && !methodDeclaration.isAbstract()) { start = retrieveStartBlockPosition(methodHeaderEnd, bodyEnd); if (start == -1) start = methodDeclaration.bodyStart; // use recovery position for body start end = methodDeclaration.bodyEnd; // try to get the best end position CategorizedProblem[] problems = methodDeclaration.compilationResult().problems; if (problems != null) { for (int i = 0, max = methodDeclaration.compilationResult().problemCount; i < max; i++) { CategorizedProblem currentProblem = problems[i]; if (currentProblem.getSourceStart() == start && currentProblem.getID() == IProblem.ParsingErrorInsertToComplete) { end = currentProblem.getSourceEnd(); break; } } } int startPosition = methodDecl.getStartPosition(); methodDecl.setSourceRange(startPosition, end - startPosition + 1); if (start != -1 && end != -1) { /* * start or end can be equal to -1 if we have an interface's method. */ Block block = new Block(this.ast); block.setSourceRange(start, end - start + 1); methodDecl.setBody(block); } } } org.eclipse.jdt.internal.compiler.ast.TypeParameter[] typeParameters = methodDeclaration.typeParameters(); if (typeParameters != null) { switch (this.ast.apiLevel) { case AST.JLS2_INTERNAL: methodDecl.setFlags(methodDecl.getFlags() | ASTNode.MALFORMED); break; default: for (int i = 0, max = typeParameters.length; i < max; i++) { methodDecl.typeParameters().add(convert(typeParameters[i])); } } } // The javadoc comment is now got from list store in compilation unit declaration convert(methodDeclaration.javadoc, methodDecl); if (this.resolveBindings) { recordNodes(methodDecl, methodDeclaration); recordNodes(methodName, methodDeclaration); methodDecl.resolveBinding(); } return methodDecl; }
From source file:org.eclipse.jdt.core.dom.ASTConverter.java
License:Open Source License
public Type convertType(TypeReference typeReference) { if (typeReference instanceof Wildcard) { final Wildcard wildcard = (Wildcard) typeReference; final WildcardType wildcardType = new WildcardType(this.ast); if (wildcard.bound != null) { final Type bound = convertType(wildcard.bound); wildcardType.setBound(bound, wildcard.kind == Wildcard.EXTENDS); int start = wildcard.sourceStart; wildcardType.setSourceRange(start, bound.getStartPosition() + bound.getLength() - start); } else {//from ww w . j av a 2 s. c om final int start = wildcard.sourceStart; final int end = wildcard.sourceEnd; wildcardType.setSourceRange(start, end - start + 1); } if (this.resolveBindings) { recordNodes(wildcardType, typeReference); } return wildcardType; } Type type = null; int sourceStart = -1; int length = 0; int dimensions = typeReference.dimensions(); if (typeReference instanceof org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) { // this is either an ArrayTypeReference or a SingleTypeReference char[] name = ((org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) typeReference) .getTypeName()[0]; sourceStart = typeReference.sourceStart; length = typeReference.sourceEnd - typeReference.sourceStart + 1; // need to find out if this is an array type of primitive types or not if (isPrimitiveType(name)) { int end = retrieveEndOfElementTypeNamePosition(sourceStart, sourceStart + length); if (end == -1) { end = sourceStart + length - 1; } final PrimitiveType primitiveType = new PrimitiveType(this.ast); primitiveType.setPrimitiveTypeCode(getPrimitiveTypeCode(name)); primitiveType.setSourceRange(sourceStart, end - sourceStart + 1); type = primitiveType; } else if (typeReference instanceof ParameterizedSingleTypeReference) { ParameterizedSingleTypeReference parameterizedSingleTypeReference = (ParameterizedSingleTypeReference) typeReference; final SimpleName simpleName = new SimpleName(this.ast); simpleName.internalSetIdentifier(new String(name)); int end = retrieveEndOfElementTypeNamePosition(sourceStart, sourceStart + length); if (end == -1) { end = sourceStart + length - 1; } simpleName.setSourceRange(sourceStart, end - sourceStart + 1); switch (this.ast.apiLevel) { case AST.JLS2_INTERNAL: SimpleType simpleType = new SimpleType(this.ast); simpleType.setName(simpleName); simpleType.setFlags(simpleType.getFlags() | ASTNode.MALFORMED); simpleType.setSourceRange(sourceStart, end - sourceStart + 1); type = simpleType; if (this.resolveBindings) { this.recordNodes(simpleName, typeReference); } break; default: simpleType = new SimpleType(this.ast); simpleType.setName(simpleName); simpleType.setSourceRange(simpleName.getStartPosition(), simpleName.getLength()); final ParameterizedType parameterizedType = new ParameterizedType(this.ast); parameterizedType.setType(simpleType); type = parameterizedType; TypeReference[] typeArguments = parameterizedSingleTypeReference.typeArguments; if (typeArguments != null) { Type type2 = null; for (int i = 0, max = typeArguments.length; i < max; i++) { type2 = convertType(typeArguments[i]); ((ParameterizedType) type).typeArguments().add(type2); end = type2.getStartPosition() + type2.getLength() - 1; } end = retrieveClosingAngleBracketPosition(end + 1); type.setSourceRange(sourceStart, end - sourceStart + 1); } else { type.setSourceRange(sourceStart, end - sourceStart + 1); } if (this.resolveBindings) { this.recordNodes(simpleName, typeReference); this.recordNodes(simpleType, typeReference); } } } else { final SimpleName simpleName = new SimpleName(this.ast); simpleName.internalSetIdentifier(new String(name)); // we need to search for the starting position of the first brace in order to set the proper length // PR http://dev.eclipse.org/bugs/show_bug.cgi?id=10759 int end = retrieveEndOfElementTypeNamePosition(sourceStart, sourceStart + length); if (end == -1) { end = sourceStart + length - 1; } simpleName.setSourceRange(sourceStart, end - sourceStart + 1); final SimpleType simpleType = new SimpleType(this.ast); simpleType.setName(simpleName); type = simpleType; type.setSourceRange(sourceStart, end - sourceStart + 1); type = simpleType; if (this.resolveBindings) { this.recordNodes(simpleName, typeReference); } } if (dimensions != 0) { type = this.ast.newArrayType(type, dimensions); type.setSourceRange(sourceStart, length); ArrayType subarrayType = (ArrayType) type; int index = dimensions - 1; while (index > 0) { subarrayType = (ArrayType) subarrayType.getComponentType(); int end = retrieveProperRightBracketPosition(index, sourceStart); subarrayType.setSourceRange(sourceStart, end - sourceStart + 1); index--; } if (this.resolveBindings) { // store keys for inner types completeRecord((ArrayType) type, typeReference); } } } else { if (typeReference instanceof ParameterizedQualifiedTypeReference) { ParameterizedQualifiedTypeReference parameterizedQualifiedTypeReference = (ParameterizedQualifiedTypeReference) typeReference; char[][] tokens = parameterizedQualifiedTypeReference.tokens; TypeReference[][] typeArguments = parameterizedQualifiedTypeReference.typeArguments; long[] positions = parameterizedQualifiedTypeReference.sourcePositions; sourceStart = (int) (positions[0] >>> 32); switch (this.ast.apiLevel) { case AST.JLS2_INTERNAL: { char[][] name = ((org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) typeReference) .getTypeName(); int nameLength = name.length; sourceStart = (int) (positions[0] >>> 32); length = (int) (positions[nameLength - 1] & 0xFFFFFFFF) - sourceStart + 1; Name qualifiedName = this.setQualifiedNameNameAndSourceRanges(name, positions, typeReference); final SimpleType simpleType = new SimpleType(this.ast); simpleType.setName(qualifiedName); simpleType.setSourceRange(sourceStart, length); type = simpleType; } break; default: if (typeArguments != null) { int numberOfEnclosingType = 0; int startingIndex = 0; int endingIndex = 0; for (int i = 0, max = typeArguments.length; i < max; i++) { if (typeArguments[i] != null) { numberOfEnclosingType++; } else if (numberOfEnclosingType == 0) { endingIndex++; } } Name name = null; if (endingIndex - startingIndex == 0) { final SimpleName simpleName = new SimpleName(this.ast); simpleName.internalSetIdentifier(new String(tokens[startingIndex])); recordPendingNameScopeResolution(simpleName); int start = (int) (positions[startingIndex] >>> 32); int end = (int) positions[startingIndex]; simpleName.setSourceRange(start, end - start + 1); simpleName.index = 1; name = simpleName; if (this.resolveBindings) { recordNodes(simpleName, typeReference); } } else { name = this.setQualifiedNameNameAndSourceRanges(tokens, positions, endingIndex, typeReference); } SimpleType simpleType = new SimpleType(this.ast); simpleType.setName(name); int start = (int) (positions[startingIndex] >>> 32); int end = (int) positions[endingIndex]; simpleType.setSourceRange(start, end - start + 1); ParameterizedType parameterizedType = new ParameterizedType(this.ast); parameterizedType.setType(simpleType); if (this.resolveBindings) { recordNodes(simpleType, typeReference); recordNodes(parameterizedType, typeReference); } start = simpleType.getStartPosition(); end = start + simpleType.getLength() - 1; for (int i = 0, max = typeArguments[endingIndex].length; i < max; i++) { final Type type2 = convertType(typeArguments[endingIndex][i]); parameterizedType.typeArguments().add(type2); end = type2.getStartPosition() + type2.getLength() - 1; } int indexOfEnclosingType = 1; parameterizedType.index = indexOfEnclosingType; end = retrieveClosingAngleBracketPosition(end + 1); length = end + 1; parameterizedType.setSourceRange(start, end - start + 1); startingIndex = endingIndex + 1; Type currentType = parameterizedType; while (startingIndex < typeArguments.length) { SimpleName simpleName = new SimpleName(this.ast); simpleName.internalSetIdentifier(new String(tokens[startingIndex])); simpleName.index = startingIndex + 1; start = (int) (positions[startingIndex] >>> 32); end = (int) positions[startingIndex]; simpleName.setSourceRange(start, end - start + 1); recordPendingNameScopeResolution(simpleName); QualifiedType qualifiedType = new QualifiedType(this.ast); qualifiedType.setQualifier(currentType); qualifiedType.setName(simpleName); if (this.resolveBindings) { recordNodes(simpleName, typeReference); recordNodes(qualifiedType, typeReference); } start = currentType.getStartPosition(); end = simpleName.getStartPosition() + simpleName.getLength() - 1; qualifiedType.setSourceRange(start, end - start + 1); indexOfEnclosingType++; if (typeArguments[startingIndex] != null) { qualifiedType.index = indexOfEnclosingType; ParameterizedType parameterizedType2 = new ParameterizedType(this.ast); parameterizedType2.setType(qualifiedType); parameterizedType2.index = indexOfEnclosingType; if (this.resolveBindings) { recordNodes(parameterizedType2, typeReference); } for (int i = 0, max = typeArguments[startingIndex].length; i < max; i++) { final Type type2 = convertType(typeArguments[startingIndex][i]); parameterizedType2.typeArguments().add(type2); end = type2.getStartPosition() + type2.getLength() - 1; } end = retrieveClosingAngleBracketPosition(end + 1); length = end + 1; parameterizedType2.setSourceRange(start, end - start + 1); currentType = parameterizedType2; } else { currentType = qualifiedType; qualifiedType.index = indexOfEnclosingType; } startingIndex++; } if (this.resolveBindings) { this.recordNodes(currentType, typeReference); } type = currentType; length -= sourceStart; } } } else if (typeReference instanceof org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) { char[][] name = ((org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) typeReference) .getTypeName(); int nameLength = name.length; long[] positions = ((org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) typeReference).sourcePositions; sourceStart = (int) (positions[0] >>> 32); length = (int) (positions[nameLength - 1] & 0xFFFFFFFF) - sourceStart + 1; final Name qualifiedName = this.setQualifiedNameNameAndSourceRanges(name, positions, typeReference); final SimpleType simpleType = new SimpleType(this.ast); simpleType.setName(qualifiedName); type = simpleType; type.setSourceRange(sourceStart, length); } else { TypeReference[] typeReferences = ((org.eclipse.jdt.internal.compiler.ast.UnionTypeReference) typeReference).typeReferences; switch (this.ast.apiLevel) { case AST.JLS2_INTERNAL: case AST.JLS3: // recovery type = this.convertType(typeReferences[0]); int start = typeReference.sourceStart; int endPosition = typeReference.sourceEnd; length = endPosition - start + 1; type.setSourceRange(start, length); type.setFlags(type.getFlags() | ASTNode.MALFORMED); break; default: // union type reference final UnionType unionType = new UnionType(this.ast); for (int i = 0, max = typeReferences.length; i < max; i++) { unionType.types().add(this.convertType(typeReferences[i])); } type = unionType; List types = unionType.types(); int size = types.size(); start = ((Type) types.get(0)).getStartPosition(); Type lastType = (Type) types.get(size - 1); endPosition = lastType.getStartPosition() + lastType.getLength(); length = endPosition - start; /* + 1 - 1 == 0 */ type.setSourceRange(start, length); } } length = typeReference.sourceEnd - sourceStart + 1; if (dimensions != 0) { type = this.ast.newArrayType(type, dimensions); if (this.resolveBindings) { completeRecord((ArrayType) type, typeReference); } int end = retrieveEndOfDimensionsPosition(sourceStart + length, this.compilationUnitSourceLength); if (end != -1) { type.setSourceRange(sourceStart, end - sourceStart + 1); } else { type.setSourceRange(sourceStart, length); } ArrayType subarrayType = (ArrayType) type; int index = dimensions - 1; while (index > 0) { subarrayType = (ArrayType) subarrayType.getComponentType(); end = retrieveProperRightBracketPosition(index, sourceStart); subarrayType.setSourceRange(sourceStart, end - sourceStart + 1); index--; } } } if (this.resolveBindings) { this.recordNodes(type, typeReference); } boolean sawDiamond = false; if (typeReference instanceof ParameterizedSingleTypeReference) { ParameterizedSingleTypeReference pstr = (ParameterizedSingleTypeReference) typeReference; if (pstr.typeArguments == TypeReference.NO_TYPE_ARGUMENTS) { sawDiamond = true; } } else if (typeReference instanceof ParameterizedQualifiedTypeReference) { ParameterizedQualifiedTypeReference pqtr = (ParameterizedQualifiedTypeReference) typeReference; for (int i = 0, len = pqtr.typeArguments.length; i < len; i++) { if (pqtr.typeArguments[i] == TypeReference.NO_TYPE_ARGUMENTS) { sawDiamond = true; break; } } } if (sawDiamond) { switch (this.ast.apiLevel) { case AST.JLS2_INTERNAL: case AST.JLS3: type.setFlags(type.getFlags() | ASTNode.MALFORMED); } } return type; }