Java Code Examples for org.eclipse.jdt.core.dom.ImportDeclaration#isOnDemand()

The following examples show how to use org.eclipse.jdt.core.dom.ImportDeclaration#isOnDemand() . 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: NameResolver.java    From SnowGraph with Apache License 2.0 6 votes vote down vote up
public boolean visit(ImportDeclaration node) {
    if (node.isOnDemand()) {
        String pckgName = node.getName().getFullyQualifiedName();
        checkInDir(pckgName);
    } else {
        String importName = node.getName().getFullyQualifiedName();
        if (importName.endsWith("." + nameParts[0])) {
            fullName = importName;
            for (int i = 1; i < nameParts.length; i++) {
                fullName += "." + nameParts[i];
            }
            found = true;
        }
    }
    return true;
}
 
Example 3
Source File: ScopeAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public Collection<String> getUsedVariableNames(int offset, int length) {
	HashSet<String> result= new HashSet<String>();
	IBinding[] bindingsBefore= getDeclarationsInScope(offset, VARIABLES);
	for (int i= 0; i < bindingsBefore.length; i++) {
		result.add(bindingsBefore[i].getName());
	}
	IBinding[] bindingsAfter= getDeclarationsAfter(offset + length, VARIABLES);
	for (int i= 0; i < bindingsAfter.length; i++) {
		result.add(bindingsAfter[i].getName());
	}
	List<ImportDeclaration> imports= fRoot.imports();
	for (int i= 0; i < imports.size(); i++) {
		ImportDeclaration decl= imports.get(i);
		if (decl.isStatic() && !decl.isOnDemand()) {
			result.add(ASTNodes.getSimpleNameIdentifier(decl.getName()));
		}
	}
	return result;
}
 
Example 4
Source File: JavaASTFlattener.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean visit(final ImportDeclaration it) {
  this.appendToBuffer("import ");
  boolean _isStatic = it.isStatic();
  if (_isStatic) {
    this.appendToBuffer("static ");
  }
  it.getName().accept(this);
  boolean _isOnDemand = it.isOnDemand();
  if (_isOnDemand) {
    this.appendToBuffer(".*");
  }
  this.appendLineWrapToBuffer();
  return false;
}
 
Example 5
Source File: MoveInnerToTopRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void updateReferenceInImport(ImportDeclaration enclosingImport, ASTNode node, CompilationUnitRewrite rewrite) {
	final IBinding binding= enclosingImport.resolveBinding();
	if (binding instanceof ITypeBinding) {
		final ITypeBinding type= (ITypeBinding) binding;
		final ImportRewrite rewriter= rewrite.getImportRewrite();
		if (enclosingImport.isStatic()) {
			final String oldImport= ASTNodes.asString(node);
			final StringBuffer buffer= new StringBuffer(oldImport);
			final String typeName= fType.getDeclaringType().getElementName();
			final int index= buffer.indexOf(typeName);
			if (index >= 0) {
				buffer.delete(index, index + typeName.length() + 1);
				final String newImport= buffer.toString();
				if (enclosingImport.isOnDemand()) {
					rewriter.removeStaticImport(oldImport + ".*"); //$NON-NLS-1$
					rewriter.addStaticImport(newImport, "*", false); //$NON-NLS-1$
				} else {
					rewriter.removeStaticImport(oldImport);
					final int offset= newImport.lastIndexOf('.');
					if (offset >= 0 && offset < newImport.length() - 1) {
						rewriter.addStaticImport(newImport.substring(0, offset), newImport.substring(offset + 1), false);
					}
				}
			}
		} else
			rewriter.removeImport(type.getQualifiedName());
	}
}
 
Example 6
Source File: JavaParseTreeBuilder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean visit(ImportDeclaration node) {
       int s= node.getStartPosition();
       int l= node.getLength();
       int declarationEnd= s + l;
       if (fImportContainer == null)
           fImportContainer= new JavaNode(getCurrentContainer(), JavaNode.IMPORT_CONTAINER, null, s, l);
       String nm= node.getName().toString();
       if (node.isOnDemand())
           nm+= ".*"; //$NON-NLS-1$
       new JavaNode(fImportContainer, JavaNode.IMPORT, nm, s, l);
       fImportContainer.setLength(declarationEnd - fImportContainer.getRange().getOffset() + 1);
       fImportContainer.setAppendPosition(declarationEnd + 2); // FIXME
       return false;
   }
 
Example 7
Source File: ImportRewriteAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private String getQualifier(ImportDeclaration decl) {
	String name = decl.getName().getFullyQualifiedName();
	/*
	 * If it's on demand import, return the fully qualified name. (e.g. pack1.Foo.* => pack.Foo, pack1.* => pack1)
	 * This is because we need to have pack1.Foo.* and pack1.Bar under different qualifier groups.
	 */
	if (decl.isOnDemand()) {
		return name;
	}
	return getQualifier(name, decl.isStatic());
}
 
Example 8
Source File: ReferenceResolvingVisitor.java    From windup with Eclipse Public License 1.0 5 votes vote down vote up
private void processImport(ImportDeclaration node)
{
    String name = node.getName().toString();
    if (node.isOnDemand())
    {
        state.getWildcardImports().add(name);

        String[] resolvedNames = this.wildcardImportResolver.resolve(name);
        for (String resolvedName : resolvedNames)
        {
            processImport(resolvedName, ResolutionStatus.RESOLVED, compilationUnit.getLineNumber(node.getName().getStartPosition()),
                        compilationUnit.getColumnNumber(node.getName().getStartPosition()), node.getName().getLength(), node.toString().trim());
        }

        /*
         * Also, register the wildcard itself so that we can have rules that match against wildcard imports, event if we do not know what classes
         * may be contained in that wildcard
         */
        processImport(name + ".*", ResolutionStatus.RESOLVED, compilationUnit.getLineNumber(node.getName().getStartPosition()),
                    compilationUnit.getColumnNumber(node.getName().getStartPosition()), node.getName().getLength(), node.toString().trim());
    }
    else
    {
        String clzName = StringUtils.substringAfterLast(name, ".");
        state.getClassNameLookedUp().add(clzName);
        state.getClassNameToFQCN().put(clzName, name);
        ResolutionStatus status = node.resolveBinding() != null ? ResolutionStatus.RESOLVED : ResolutionStatus.RECOVERED;
        processImport(name, status, compilationUnit.getLineNumber(node.getName().getStartPosition()),
                    compilationUnit.getColumnNumber(node.getName().getStartPosition()), node.getName().getLength(), node.toString().trim());
    }
}
 
Example 9
Source File: ImportRewriteAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static String getFullName(ImportDeclaration decl) {
	String name= decl.getName().getFullyQualifiedName();
	return decl.isOnDemand() ? name + ".*": name; //$NON-NLS-1$
}