示例#1
0
 /*
  * (non-Javadoc)
  *
  * @see
  * org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom
  * .CompilationUnit)
  */
 @Override
 public boolean visit(final CompilationUnit node) {
   if (node.getPackage() != null) {
     currentPackageName = node.getPackage().getName().getFullyQualifiedName();
   }
   return super.visit(node);
 }
示例#2
0
 @Override
 public O setPackage(final String name) {
   if (unit.getPackage() == null) {
     unit.setPackage(unit.getAST().newPackageDeclaration());
   }
   unit.getPackage().setName(unit.getAST().newName(name));
   return (O) this;
 }
 /*
  * @see ASTVisitor#visit(CompilationUnit)
  */
 public boolean visit(CompilationUnit node) {
   if (node.getPackage() != null) {
     node.getPackage().accept(this);
   }
   for (Iterator it = node.imports().iterator(); it.hasNext(); ) {
     ImportDeclaration d = (ImportDeclaration) it.next();
     d.accept(this);
   }
   for (Iterator it = node.types().iterator(); it.hasNext(); ) {
     AbstractTypeDeclaration d = (AbstractTypeDeclaration) it.next();
     d.accept(this);
   }
   return false;
 }
示例#4
0
 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;
 }
示例#5
0
 /*
  * Package modifiers
  */
 @Override
 public String getPackage() {
   PackageDeclaration pkg = unit.getPackage();
   if (pkg != null) {
     return pkg.getName().getFullyQualifiedName();
   } else {
     return null;
   }
 }
 private void updatePackageStatement(
     CompilationUnit astCU, String[] pkgName, ASTRewrite rewriter, ICompilationUnit cu)
     throws JavaModelException {
   boolean defaultPackage = pkgName.length == 0;
   AST ast = astCU.getAST();
   if (defaultPackage) {
     // remove existing package statement
     PackageDeclaration pkg = astCU.getPackage();
     if (pkg != null) {
       int pkgStart;
       Javadoc javadoc = pkg.getJavadoc();
       if (javadoc != null) {
         pkgStart = javadoc.getStartPosition() + javadoc.getLength() + 1;
       } else {
         pkgStart = pkg.getStartPosition();
       }
       int extendedStart = astCU.getExtendedStartPosition(pkg);
       if (pkgStart != extendedStart) {
         // keep the comments associated with package declaration
         // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=247757
         String commentSource = cu.getSource().substring(extendedStart, pkgStart);
         ASTNode comment =
             rewriter.createStringPlaceholder(commentSource, ASTNode.PACKAGE_DECLARATION);
         rewriter.set(astCU, CompilationUnit.PACKAGE_PROPERTY, comment, null);
       } else {
         rewriter.set(astCU, CompilationUnit.PACKAGE_PROPERTY, null, null);
       }
     }
   } else {
     org.eclipse.jdt.core.dom.PackageDeclaration pkg = astCU.getPackage();
     if (pkg != null) {
       // rename package statement
       Name name = ast.newName(pkgName);
       rewriter.set(pkg, PackageDeclaration.NAME_PROPERTY, name, null);
     } else {
       // create new package statement
       pkg = ast.newPackageDeclaration();
       pkg.setName(ast.newName(pkgName));
       rewriter.set(astCU, CompilationUnit.PACKAGE_PROPERTY, pkg, null);
     }
   }
 }
  private ClipboardData getClipboardData(ITypeRoot inputElement, int offset, int length) {
    CompilationUnit astRoot =
        SharedASTProvider.getAST(inputElement, SharedASTProvider.WAIT_ACTIVE_ONLY, null);
    if (astRoot == null) {
      return null;
    }

    // do process import if selection spans over import declaration or package
    List<ImportDeclaration> list = astRoot.imports();
    if (!list.isEmpty()) {
      if (offset < ((ASTNode) list.get(list.size() - 1)).getStartPosition()) {
        return null;
      }
    } else if (astRoot.getPackage() != null) {
      if (offset < ((ASTNode) astRoot.getPackage()).getStartPosition()) {
        return null;
      }
    }

    ArrayList<SimpleName> typeImportsRefs = new ArrayList<SimpleName>();
    ArrayList<SimpleName> staticImportsRefs = new ArrayList<SimpleName>();

    ImportReferencesCollector.collect(
        astRoot,
        inputElement.getJavaProject(),
        new Region(offset, length),
        typeImportsRefs,
        staticImportsRefs);

    if (typeImportsRefs.isEmpty() && staticImportsRefs.isEmpty()) {
      return null;
    }

    HashSet<String> namesToImport = new HashSet<String>(typeImportsRefs.size());
    for (int i = 0; i < typeImportsRefs.size(); i++) {
      Name curr = typeImportsRefs.get(i);
      IBinding binding = curr.resolveBinding();
      if (binding != null && binding.getKind() == IBinding.TYPE) {
        ITypeBinding typeBinding = (ITypeBinding) binding;
        if (typeBinding.isArray()) {
          typeBinding = typeBinding.getElementType();
        }
        if (typeBinding.isTypeVariable()
            || typeBinding.isCapture()
            || typeBinding.isWildcardType()) { // can be removed when bug 98473 is fixed
          continue;
        }

        if (typeBinding.isMember() || typeBinding.isTopLevel()) {
          String name = Bindings.getRawQualifiedName(typeBinding);
          if (name.length() > 0) {
            namesToImport.add(name);
          }
        }
      }
    }

    HashSet<String> staticsToImport = new HashSet<String>(staticImportsRefs.size());
    for (int i = 0; i < staticImportsRefs.size(); i++) {
      Name curr = staticImportsRefs.get(i);
      IBinding binding = curr.resolveBinding();
      if (binding != null) {
        StringBuffer buf = new StringBuffer(Bindings.getImportName(binding));
        if (binding.getKind() == IBinding.METHOD) {
          buf.append("()"); // $NON-NLS-1$
        }
        staticsToImport.add(buf.toString());
      }
    }

    if (namesToImport.isEmpty() && staticsToImport.isEmpty()) {
      return null;
    }

    String[] typeImports = namesToImport.toArray(new String[namesToImport.size()]);
    String[] staticImports = staticsToImport.toArray(new String[staticsToImport.size()]);
    return new ClipboardData(inputElement, typeImports, staticImports);
  }
示例#8
0
 @Override
 public boolean isDefaultPackage() {
   return unit.getPackage() == null;
 }