下面列出了org.eclipse.jdt.core.dom.CompilationUnit#getPackage ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static void insertToCu(ASTRewrite rewrite, ASTNode node, CompilationUnit cuNode) {
switch (node.getNodeType()) {
case ASTNode.TYPE_DECLARATION:
case ASTNode.ENUM_DECLARATION:
case ASTNode.ANNOTATION_TYPE_DECLARATION:
rewrite.getListRewrite(cuNode, CompilationUnit.TYPES_PROPERTY).insertAt(node, ASTNodes.getInsertionIndex((AbstractTypeDeclaration) node, cuNode.types()), null);
break;
case ASTNode.IMPORT_DECLARATION:
rewrite.getListRewrite(cuNode, CompilationUnit.IMPORTS_PROPERTY).insertLast(node, null);
break;
case ASTNode.PACKAGE_DECLARATION:
// only insert if none exists
if (cuNode.getPackage() == null)
rewrite.set(cuNode, CompilationUnit.PACKAGE_PROPERTY, node, null);
break;
default:
Assert.isTrue(false, String.valueOf(node.getNodeType()));
}
}
private void createPackageObjects() {
checkNotNull(compilationUnitList);
String packageName;
for (CompilationUnit unit : compilationUnitList) {
if (unit.getPackage() != null) {
packageName = unit.getPackage().getName().toString();
} else {
packageName = "(default package)";
}
SM_Package pkgObj = searchPackage(packageName);
// If pkgObj is null, package has not yet created
if (pkgObj == null) {
pkgObj = new SM_Package(packageName, this, inputArgs);
packageList.add(pkgObj);
}
pkgObj.addCompilationUnit(unit);
}
}
/**
* Evaluates fully qualified name of the TypeDeclaration object.
*/
public static String getFullName(TypeDeclaration decl) {
String name = decl.getName().getIdentifier();
ASTNode parent = decl.getParent();
// resolve full name e.g.: A.B
while (parent != null && parent.getClass() == TypeDeclaration.class) {
name = ((TypeDeclaration) parent).getName().getIdentifier() + "." + name;
parent = parent.getParent();
}
// resolve fully qualified name e.g.: some.package.A.B
if (decl.getRoot().getClass() == CompilationUnit.class) {
CompilationUnit root = (CompilationUnit) decl.getRoot();
if (root.getPackage() != null) {
PackageDeclaration pack = root.getPackage();
name = pack.getName().getFullyQualifiedName() + "." + name;
}
}
return name;
}
@Override
public boolean visit(final CompilationUnit node) {
if (node.getPackage() != null) {
currentPackageName = node.getPackage().getName()
.getFullyQualifiedName();
}
for (final Object decl : node.imports()) {
final ImportDeclaration imp = (ImportDeclaration) decl;
if (!imp.isStatic()) {
final String fqn = imp.getName().getFullyQualifiedName();
importedNames.put(fqn.substring(fqn.lastIndexOf('.') + 1),
fqn);
}
}
return true;
}
public static StubTypeContext createStubTypeContext(ICompilationUnit cu, CompilationUnit root, int focalPosition) throws CoreException {
StringBuffer bufBefore= new StringBuffer();
StringBuffer bufAfter= new StringBuffer();
int introEnd= 0;
PackageDeclaration pack= root.getPackage();
if (pack != null)
introEnd= pack.getStartPosition() + pack.getLength();
List<ImportDeclaration> imports= root.imports();
if (imports.size() > 0) {
ImportDeclaration lastImport= imports.get(imports.size() - 1);
introEnd= lastImport.getStartPosition() + lastImport.getLength();
}
bufBefore.append(cu.getBuffer().getText(0, introEnd));
fillWithTypeStubs(bufBefore, bufAfter, focalPosition, root.types());
bufBefore.append(' ');
bufAfter.insert(0, ' ');
return new StubTypeContext(cu, bufBefore.toString(), bufAfter.toString());
}
@Override
public boolean visit(final CompilationUnit node) {
if (node.getPackage() != null) {
currentPackageName = node.getPackage().getName()
.getFullyQualifiedName();
}
for (final Object decl : node.imports()) {
final ImportDeclaration imp = (ImportDeclaration) decl;
if (!imp.isStatic()) {
final String fqn = imp.getName().getFullyQualifiedName();
importedNames.put(fqn.substring(fqn.lastIndexOf('.') + 1),
fqn);
}
}
return true;
}
/** Visit the given Java compilation unit.
*
* @param jfile the compilation unit.
*/
protected void visitJavaCompilationUnit(IFile jfile) {
final ICompilationUnit cu = JavaCore.createCompilationUnitFrom(jfile);
if (cu != null) {
final ASTParser parser = ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL);
parser.setSource(cu);
parser.setFocalPosition(0);
final CompilationUnit root = (CompilationUnit) parser.createAST(null);
final PackageDeclaration packDecl = root.getPackage();
final IPath packPath = jfile.getParent().getFullPath();
final String cuName = jfile.getName();
if (packDecl == null) {
addToMap(this.sourceFolders, packPath, new Path(cuName));
} else {
final IPath relPath = new Path(packDecl.getName().getFullyQualifiedName().replace('.', '/'));
final IPath folderPath = getFolderPath(packPath, relPath);
if (folderPath != null) {
addToMap(this.sourceFolders, folderPath, relPath.append(cuName));
}
}
}
}
public static String getFullyQualifiedName(AbstractTypeDeclaration node) {
StringBuilder sb = new StringBuilder();
sb.append(node.getName().getIdentifier());
ASTNode n = node;
while (n.getParent() != null) {
n = n.getParent();
if (n instanceof CompilationUnit) {
CompilationUnit cu = (CompilationUnit) n;
if (cu.getPackage() != null)
sb.insert(0, cu.getPackage().getName().getFullyQualifiedName() + ".");
} else if (n instanceof AbstractTypeDeclaration)
sb.insert(0, ((AbstractTypeDeclaration) n).getName().getIdentifier() + ".");
else
return "";
}
return sb.toString();
}
public static String qualifiedName(TypeDeclaration decl) {
String name = decl.getName().getIdentifier();
ASTNode parent = decl.getParent();
// resolve full name e.g.: A.B
while (parent != null && parent.getClass() == TypeDeclaration.class) {
name = ((TypeDeclaration) parent).getName().getIdentifier() + "." + name;
parent = parent.getParent();
}
// resolve fully qualified name e.g.: some.package.A.B
if (decl.getRoot().getClass() == CompilationUnit.class) {
CompilationUnit root = (CompilationUnit) decl.getRoot();
if (root.getPackage() != null) {
PackageDeclaration pack = root.getPackage();
name = pack.getName().getFullyQualifiedName() + "." + name;
}
}
return name;
}
/**
* Checks whether {@link CompilationUnit} has copyright header
*
* @param compilationUnit
* checked compilation unit
* @return true if {@link CompilationUnit} has copyright header
*/
public boolean hasCopyrightsComment(final CompilationUnit compilationUnit) {
final List<Comment> comments = getCommentList(compilationUnit);
boolean hasCopyrights = false;
if (!comments.isEmpty()) {
final PackageDeclaration packageNode = compilationUnit.getPackage();
final boolean commentBeforePackage = comments.get(0).getStartPosition() <= packageNode.getStartPosition();
final boolean hasJavaDoc = packageNode.getJavadoc() != null;
hasCopyrights = commentBeforePackage || hasJavaDoc;
}
return hasCopyrights;
}
private int getPackageStatementEndPos(CompilationUnit root) {
PackageDeclaration packDecl= root.getPackage();
if (packDecl != null) {
int afterPackageStatementPos= -1;
int lineNumber= root.getLineNumber(packDecl.getStartPosition() + packDecl.getLength());
if (lineNumber >= 0) {
int lineAfterPackage= lineNumber + 1;
afterPackageStatementPos= root.getPosition(lineAfterPackage, 0);
}
if (afterPackageStatementPos < 0) {
this.flags|= F_NEEDS_LEADING_DELIM;
return packDecl.getStartPosition() + packDecl.getLength();
}
int firstTypePos= getFirstTypeBeginPos(root);
if (firstTypePos != -1 && firstTypePos <= afterPackageStatementPos) {
this.flags|= F_NEEDS_TRAILING_DELIM;
if (firstTypePos == afterPackageStatementPos) {
this.flags|= F_NEEDS_LEADING_DELIM;
}
return firstTypePos;
}
this.flags|= F_NEEDS_LEADING_DELIM;
return afterPackageStatementPos; // insert a line after after package statement
}
this.flags |= F_NEEDS_TRAILING_DELIM;
return 0;
}
private static Javadoc getPackageJavadocNode(IJavaElement element, String cuSource) {
CompilationUnit cu= createAST(element, cuSource);
if (cu != null) {
PackageDeclaration packDecl= cu.getPackage();
if (packDecl != null) {
return packDecl.getJavadoc();
}
}
return null;
}
private static Javadoc getPackageJavadocNode(IJavaElement element, String cuSource) {
CompilationUnit cu = createAST(element, cuSource);
if (cu != null) {
PackageDeclaration packDecl = cu.getPackage();
if (packDecl != null) {
return packDecl.getJavadoc();
}
}
return null;
}
private static Javadoc getPackageJavadocNode(IJavaElement element, String cuSource) {
CompilationUnit cu= createAST(element, cuSource);
if (cu != null) {
PackageDeclaration packDecl= cu.getPackage();
if (packDecl != null) {
return packDecl.getJavadoc();
}
}
return null;
}
@Override
public boolean visit(final CompilationUnit node) {
if (node.getPackage() != null) {
currentPackageName = node.getPackage().getName()
.getFullyQualifiedName();
}
return super.visit(node);
}
@Override
public boolean visit(final CompilationUnit node) {
if (node.getPackage() != null) {
currentPackageName = node.getPackage().getName()
.getFullyQualifiedName();
}
return super.visit(node);
}
private void copyPackageDeclarationToDestination(IPackageDeclaration declaration, ASTRewrite targetRewrite, CompilationUnit sourceCuNode, CompilationUnit destinationCuNode) throws JavaModelException {
if (destinationCuNode.getPackage() != null)
return;
PackageDeclaration sourceNode= ASTNodeSearchUtil.getPackageDeclarationNode(declaration, sourceCuNode);
PackageDeclaration copiedNode= (PackageDeclaration) ASTNode.copySubtree(targetRewrite.getAST(), sourceNode);
targetRewrite.set(destinationCuNode, CompilationUnit.PACKAGE_PROPERTY, copiedNode, null);
}
@Override
public boolean visit(final CompilationUnit node) {
if (node.getPackage() != null) {
currentPackageName = node.getPackage().getName()
.getFullyQualifiedName();
}
return super.visit(node);
}
@Override
public boolean visit(CompilationUnit node) {
Namespace namespace;
if (node.getPackage() == null)
/* This is the default package */
namespace = importer.ensureNamespaceNamed("");
else
namespace = importer.ensureNamespaceFromPackageBinding(node.getPackage().resolveBinding());
namespace.setIsStub(false);
importer.pushOnContainerStack(namespace);
return true;
}
public static String getCompilationUnitPackageName(CompilationUnit compilationUnit) {
return compilationUnit.getPackage() == null
? ""
: compilationUnit.getPackage().getName().getFullyQualifiedName();
}