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

The following examples show how to use org.eclipse.jdt.core.dom.CompilationUnit#imports() . 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: UnusedImportsNodeCollector.java    From j2cl with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void endVisit(CompilationUnit compilationUnit) {
  List<ImportDeclaration> imports = compilationUnit.imports();
  for (ImportDeclaration importDeclaration : imports) {
    if (importDeclaration.isOnDemand()) {
      // Assume .* imports are always needed.
      continue;
    }
    SimpleName importedClass =
        importDeclaration.getName().isSimpleName()
            ? (SimpleName) importDeclaration.getName()
            : ((QualifiedName) importDeclaration.getName()).getName();

    if (!referencedNames.contains(importedClass.getIdentifier())) {
      unusedImports.add(importDeclaration);
    }
  }
}
 
Example 2
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 3
Source File: UMLModelASTReader.java    From RefactoringMiner with MIT License 6 votes vote down vote up
protected void processCompilationUnit(String sourceFilePath, CompilationUnit compilationUnit) {
	PackageDeclaration packageDeclaration = compilationUnit.getPackage();
	String packageName = null;
	if(packageDeclaration != null)
		packageName = packageDeclaration.getName().getFullyQualifiedName();
	else
		packageName = "";
	
	List<ImportDeclaration> imports = compilationUnit.imports();
	List<String> importedTypes = new ArrayList<String>();
	for(ImportDeclaration importDeclaration : imports) {
		importedTypes.add(importDeclaration.getName().getFullyQualifiedName());
	}
	List<AbstractTypeDeclaration> topLevelTypeDeclarations = compilationUnit.types();
       for(AbstractTypeDeclaration abstractTypeDeclaration : topLevelTypeDeclarations) {
       	if(abstractTypeDeclaration instanceof TypeDeclaration) {
       		TypeDeclaration topLevelTypeDeclaration = (TypeDeclaration)abstractTypeDeclaration;
       		processTypeDeclaration(compilationUnit, topLevelTypeDeclaration, packageName, sourceFilePath, importedTypes);
       	}
       	else if(abstractTypeDeclaration instanceof EnumDeclaration) {
       		EnumDeclaration enumDeclaration = (EnumDeclaration)abstractTypeDeclaration;
       		processEnumDeclaration(compilationUnit, enumDeclaration, packageName, sourceFilePath, importedTypes);
       	}
       }
}
 
Example 4
Source File: JavaTypeHierarchyExtractor.java    From tassal 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 5
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 6
Source File: ImportRewriteAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private IRegion evaluateReplaceRange(CompilationUnit root) {
	List imports= root.imports();
	if (!imports.isEmpty()) {
		ImportDeclaration first= (ImportDeclaration) imports.get(0);
		ImportDeclaration last= (ImportDeclaration) imports.get(imports.size() - 1);

		int startPos= first.getStartPosition(); // no extended range for first: bug 121428
		int endPos= root.getExtendedStartPosition(last) + root.getExtendedLength(last);
		int endLine= root.getLineNumber(endPos);
		if (endLine > 0) {
			int nextLinePos= root.getPosition(endLine + 1, 0);
			if (nextLinePos >= 0) {
				int firstTypePos= getFirstTypeBeginPos(root);
				if (firstTypePos != -1 && firstTypePos < nextLinePos) {
					endPos= firstTypePos;
				} else {
					endPos= nextLinePos;
				}
			}
		}
		return new Region(startPos, endPos - startPos);
	} else {
		int start= getPackageStatementEndPos(root);
		return new Region(start, 0);
	}
}
 
Example 7
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 8
Source File: NewTypeWizardPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private Set<String> getExistingImports(CompilationUnit root) {
	List<ImportDeclaration> imports= root.imports();
	Set<String> res= new HashSet<String>(imports.size());
	for (int i= 0; i < imports.size(); i++) {
		res.add(ASTNodes.asString(imports.get(i)));
	}
	return res;
}