List of usage examples for org.eclipse.jdt.core.dom TypeDeclaration TypeDeclaration
TypeDeclaration(AST ast)
From source file:org.eclipse.jdt.core.dom.ASTConverter.java
License:Open Source License
public TypeDeclaration convert(org.eclipse.jdt.internal.compiler.ast.ASTNode[] nodes) { final TypeDeclaration typeDecl = new TypeDeclaration(this.ast); typeDecl.setInterface(false);// w ww.j a v a 2 s. co m int nodesLength = nodes.length; for (int i = 0; i < nodesLength; i++) { org.eclipse.jdt.internal.compiler.ast.ASTNode node = nodes[i]; if (node instanceof org.eclipse.jdt.internal.compiler.ast.Initializer) { org.eclipse.jdt.internal.compiler.ast.Initializer oldInitializer = (org.eclipse.jdt.internal.compiler.ast.Initializer) node; Initializer initializer = new Initializer(this.ast); initializer.setBody(convert(oldInitializer.block)); setModifiers(initializer, oldInitializer); initializer.setSourceRange(oldInitializer.declarationSourceStart, oldInitializer.sourceEnd - oldInitializer.declarationSourceStart + 1); // setJavaDocComment(initializer); // initializer.setJavadoc(convert(oldInitializer.javadoc)); convert(oldInitializer.javadoc, initializer); typeDecl.bodyDeclarations().add(initializer); } else if (node instanceof org.eclipse.jdt.internal.compiler.ast.FieldDeclaration) { org.eclipse.jdt.internal.compiler.ast.FieldDeclaration fieldDeclaration = (org.eclipse.jdt.internal.compiler.ast.FieldDeclaration) node; if (i > 0 && (nodes[i - 1] instanceof org.eclipse.jdt.internal.compiler.ast.FieldDeclaration) && ((org.eclipse.jdt.internal.compiler.ast.FieldDeclaration) nodes[i - 1]).declarationSourceStart == fieldDeclaration.declarationSourceStart) { // we have a multiple field declaration // We retrieve the existing fieldDeclaration to add the new VariableDeclarationFragment FieldDeclaration currentFieldDeclaration = (FieldDeclaration) typeDecl.bodyDeclarations() .get(typeDecl.bodyDeclarations().size() - 1); currentFieldDeclaration.fragments().add(convertToVariableDeclarationFragment(fieldDeclaration)); } else { // we can create a new FieldDeclaration typeDecl.bodyDeclarations().add(convertToFieldDeclaration(fieldDeclaration)); } } else if (node instanceof org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) { AbstractMethodDeclaration nextMethodDeclaration = (AbstractMethodDeclaration) node; if (!nextMethodDeclaration.isDefaultConstructor() && !nextMethodDeclaration.isClinit()) { typeDecl.bodyDeclarations().add(convert(false, nextMethodDeclaration)); } } else if (node instanceof org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) { org.eclipse.jdt.internal.compiler.ast.TypeDeclaration nextMemberDeclaration = (org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) node; ASTNode nextMemberDeclarationNode = convert(nextMemberDeclaration); if (nextMemberDeclarationNode == null) { typeDecl.setFlags(typeDecl.getFlags() | ASTNode.MALFORMED); } else { typeDecl.bodyDeclarations().add(nextMemberDeclarationNode); } } } return typeDecl; }
From source file:org.eclipse.jdt.core.dom.ASTConverter.java
License:Open Source License
public ASTNode convert(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration typeDeclaration) { int kind = org.eclipse.jdt.internal.compiler.ast.TypeDeclaration.kind(typeDeclaration.modifiers); switch (kind) { case org.eclipse.jdt.internal.compiler.ast.TypeDeclaration.ENUM_DECL: if (this.ast.apiLevel == AST.JLS2_INTERNAL) { return null; } else {//w w w . j a v a 2 s . c o m return convertToEnumDeclaration(typeDeclaration); } case org.eclipse.jdt.internal.compiler.ast.TypeDeclaration.ANNOTATION_TYPE_DECL: if (this.ast.apiLevel == AST.JLS2_INTERNAL) { return null; } else { return convertToAnnotationDeclaration(typeDeclaration); } } checkCanceled(); TypeDeclaration typeDecl = new TypeDeclaration(this.ast); if (typeDeclaration.modifiersSourceStart != -1) { setModifiers(typeDecl, typeDeclaration); } boolean isInterface = kind == org.eclipse.jdt.internal.compiler.ast.TypeDeclaration.INTERFACE_DECL; typeDecl.setInterface(isInterface); final SimpleName typeName = new SimpleName(this.ast); typeName.internalSetIdentifier(new String(typeDeclaration.name)); typeName.setSourceRange(typeDeclaration.sourceStart, typeDeclaration.sourceEnd - typeDeclaration.sourceStart + 1); typeDecl.setName(typeName); typeDecl.setSourceRange(typeDeclaration.declarationSourceStart, typeDeclaration.bodyEnd - typeDeclaration.declarationSourceStart + 1); // need to set the superclass and super interfaces here since we cannot distinguish them at // the type references level. if (typeDeclaration.superclass != null) { switch (this.ast.apiLevel) { case AST.JLS2_INTERNAL: typeDecl.internalSetSuperclass(convert(typeDeclaration.superclass)); break; default: typeDecl.setSuperclassType(convertType(typeDeclaration.superclass)); break; } } org.eclipse.jdt.internal.compiler.ast.TypeReference[] superInterfaces = typeDeclaration.superInterfaces; if (superInterfaces != null) { switch (this.ast.apiLevel) { case AST.JLS2_INTERNAL: for (int index = 0, length = superInterfaces.length; index < length; index++) { typeDecl.internalSuperInterfaces().add(convert(superInterfaces[index])); } break; default: for (int index = 0, length = superInterfaces.length; index < length; index++) { typeDecl.superInterfaceTypes().add(convertType(superInterfaces[index])); } } } org.eclipse.jdt.internal.compiler.ast.TypeParameter[] typeParameters = typeDeclaration.typeParameters; if (typeParameters != null) { switch (this.ast.apiLevel) { case AST.JLS2_INTERNAL: typeDecl.setFlags(typeDecl.getFlags() | ASTNode.MALFORMED); break; default: for (int index = 0, length = typeParameters.length; index < length; index++) { typeDecl.typeParameters().add(convert(typeParameters[index])); } } } buildBodyDeclarations(typeDeclaration, typeDecl, isInterface); if (this.resolveBindings) { recordNodes(typeDecl, typeDeclaration); recordNodes(typeName, typeDeclaration); typeDecl.resolveBinding(); } return typeDecl; }