List of usage examples for org.eclipse.jdt.core.dom SimpleType setName
public void setName(Name typeName)
From source file:com.google.gdt.eclipse.designer.builders.GwtBuilder.java
License:Open Source License
/** * Keeps Async interfaces for services and remove other interfaces. *//* w w w . ja va 2s. c om*/ private static void updateInterfacesOfAsync(IJavaProject javaProject, CompilationUnit serviceUnit, TypeDeclaration serviceType) throws Exception { String serviceName = AstNodeUtils.getFullyQualifiedName(serviceType, false); AST ast = serviceType.getAST(); for (Iterator<?> I = serviceType.superInterfaceTypes().iterator(); I.hasNext();) { Type type = (Type) I.next(); ITypeBinding typeBinding = AstNodeUtils.getTypeBinding(type); if (AstNodeUtils.isSuccessorOf(typeBinding, Constants.CLASS_REMOTE_SERVICE)) { String superServiceName = AstNodeUtils.getFullyQualifiedName(typeBinding, false); String superAsyncName = superServiceName + "Async"; if (javaProject.findType(superAsyncName) != null) { if (type instanceof SimpleType) { { SimpleType simpleType = (SimpleType) type; String superAsyncNameSimple = CodeUtils.getShortClass(superAsyncName); simpleType.setName(ast.newSimpleName(superAsyncNameSimple)); } if (!CodeUtils.isSamePackage(serviceName, superAsyncName)) { addImport(serviceUnit, superAsyncName); } continue; } } } I.remove(); } }
From source file:com.windowtester.eclipse.ui.convert.WTConvertAPIContext.java
License:Open Source License
/** * Change the name of the type being referenced * // w w w . j a v a 2 s . co m * @param type the type to be changed (not <code>null</code> and must be in the * receiver's compilation unit) * @param the new type name (not <code>null</code>, not empty) */ public void setTypeName(SimpleType type, String newTypeName) { Name newNameNode = newName(newTypeName, 0); replacing(type.getName(), newNameNode); type.setName(newNameNode); }
From source file:de.crowdcode.kissmda.extensions.importpacker.ImportPacker.java
License:Apache License
protected void useSimpleNameAndAddToImport(SimpleType simpleType) { if (simpleType.getName().isQualifiedName()) { QualifiedName qualifiedName = (QualifiedName) simpleType.getName(); String qualifier = qualifiedName.getQualifier().getFullyQualifiedName(); String simpleName = simpleName(qualifiedName); AST ast = compilationUnit.getAST(); if (isAlreadyImported(simpleName, qualifier)) { simpleType.setName(ast.newName(simpleName)); } else if (isNewSimpleName(simpleName)) { simpleType.setName(ast.newName(simpleName)); importedTypes.put(simpleName, qualifiedName); }//from w ww. ja v a 2 s .co m } }
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 w w w .ja va 2s . c o m*/ 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; }
From source file:org.eclipse.wb.internal.core.utils.ast.AstEditor.java
License:Open Source License
/** * Ensures that {@link MethodDeclaration} declares as thrown exception with given name, or its * superclass./* w ww . j av a 2 s . c o m*/ */ public void ensureThrownException(MethodDeclaration method, String requiredException) throws Exception { ITypeHierarchy requiredHierarchy; { IType requiredExceptionType = getJavaProject().findType(requiredException); Assert.isNotNull2(requiredExceptionType, "No such exception type: {0}", requiredException); requiredHierarchy = requiredExceptionType.newSupertypeHierarchy(null); } // find last exception and check, may be required exception is already declared Name lastName = null; for (Name declaredTypeName : DomGenerics.thrownExceptions(method)) { String declaredName = AstNodeUtils.getFullyQualifiedName(declaredTypeName, false); IType declaredType = getJavaProject().findType(declaredName); if (requiredHierarchy.contains(declaredType)) { return; } lastName = declaredTypeName; } // prepare position int pos; String codePrefix; if (lastName == null) { ASTNode name = method.getName(); pos = AstNodeUtils.getSourceEnd(name); pos = indexOf(")", pos) + 1; codePrefix = " throws "; } else { pos = AstNodeUtils.getSourceEnd(lastName); codePrefix = ", "; } // prepare new exception nodes SimpleType newExceptionType; Name newExceptionTypeName; { TypeLiteral typeLiteral = (TypeLiteral) m_parser.parseExpression(pos, requiredException + ".class"); AstNodeUtils.moveNode(typeLiteral, pos + codePrefix.length()); newExceptionType = (SimpleType) typeLiteral.getType(); newExceptionTypeName = newExceptionType.getName(); newExceptionType.setName(typeLiteral.getAST().newSimpleName("filler")); } // update source/AST replaceSubstring(pos, 0, codePrefix + requiredException); DomGenerics.thrownExceptions(method).add(newExceptionTypeName); // update ITypeBinding { ITypeBinding newExceptionBinding = AstNodeUtils.getTypeBinding(newExceptionType); DesignerMethodBinding methodBinding = getDesignerMethodBinding(method); methodBinding.addExceptionType(newExceptionBinding); } // finalize resolveImports(method); }