Java Code Examples for org.eclipse.jdt.core.dom.CompilationUnit#getPackage()

The following examples show how to use org.eclipse.jdt.core.dom.CompilationUnit#getPackage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SM_Project.java    From DesigniteJava with Apache License 2.0 6 votes vote down vote up
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);
	}
}
 
Example 2
Source File: NameResolver.java    From SnowGraph with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 3
Source File: JavaTypeHierarchyExtractor.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@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;
}
 
Example 4
Source File: SharedUtils.java    From txtUML with Eclipse Public License 1.0 6 votes vote down vote up
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;
}
 
Example 5
Source File: JavaASTUtil.java    From compiler with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example 6
Source File: TypeContextChecker.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
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());
}
 
Example 7
Source File: PasteAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
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()));
	}
}
 
Example 8
Source File: JavaTypeHierarchyExtractor.java    From api-mining with GNU General Public License v3.0 6 votes vote down vote up
@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;
}
 
Example 9
Source File: SarlClassPathDetector.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** 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));
			}
		}
	}
}
 
Example 10
Source File: JavadocContentAccess2.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
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;
}
 
Example 11
Source File: AstVisitor.java    From jdt2famix with Eclipse Public License 1.0 5 votes vote down vote up
@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;
}
 
Example 12
Source File: MethodsInClass.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean visit(final CompilationUnit node) {
	if (node.getPackage() != null) {
		currentPackageName = node.getPackage().getName()
				.getFullyQualifiedName();
	}
	return super.visit(node);
}
 
Example 13
Source File: ReorgPolicyFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
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);
}
 
Example 14
Source File: MethodsInClass.java    From api-mining with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean visit(final CompilationUnit node) {
	if (node.getPackage() != null) {
		currentPackageName = node.getPackage().getName()
				.getFullyQualifiedName();
	}
	return super.visit(node);
}
 
Example 15
Source File: MethodsInClass.java    From tassal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean visit(final CompilationUnit node) {
	if (node.getPackage() != null) {
		currentPackageName = node.getPackage().getName()
				.getFullyQualifiedName();
	}
	return super.visit(node);
}
 
Example 16
Source File: JavadocContentAccess2.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
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;
}
 
Example 17
Source File: JavadocContentAccess.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
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;
}
 
Example 18
Source File: ImportRewriteAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
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;
}
 
Example 19
Source File: CopyrightManager.java    From hybris-commerce-eclipse-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 20
Source File: JdtUtils.java    From j2cl with Apache License 2.0 4 votes vote down vote up
public static String getCompilationUnitPackageName(CompilationUnit compilationUnit) {
  return compilationUnit.getPackage() == null
      ? ""
      : compilationUnit.getPackage().getName().getFullyQualifiedName();
}