Exemplo n.º 1
0
  private static VariableDeclaration getVariableDeclaration(Name node) {
    IBinding binding = node.resolveBinding();
    if (binding == null && node.getParent() instanceof VariableDeclaration)
      return (VariableDeclaration) node.getParent();

    if (binding != null && binding.getKind() == IBinding.VARIABLE) {
      CompilationUnit cu = (CompilationUnit) ASTNodes.getParent(node, CompilationUnit.class);
      return ASTNodes.findVariableDeclaration(((IVariableBinding) binding), cu);
    }
    return null;
  }
 private void possibleTypeRefFound(Name node) {
   while (node.isQualifiedName()) {
     node = ((QualifiedName) node).getQualifier();
   }
   IBinding binding = node.resolveBinding();
   if (binding == null || binding.getKind() == IBinding.TYPE) {
     // if the binding is null, we cannot determine if
     // we have a type binding or not, so we will assume
     // we do.
     addReference((SimpleName) node);
   }
 }
  @Override
  protected ASTRewrite getRewrite() throws CoreException {
    ASTNode boundNode = fAstRoot.findDeclaringNode(fBinding);
    ASTNode declNode = null;
    CompilationUnit newRoot = fAstRoot;
    if (boundNode != null) {
      declNode = boundNode; // is same CU
    } else {
      newRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
      declNode = newRoot.findDeclaringNode(fBinding.getKey());
    }
    ImportRewrite imports = createImportRewrite(newRoot);

    if (declNode instanceof TypeDeclaration) {
      AST ast = declNode.getAST();
      ASTRewrite rewrite = ASTRewrite.create(ast);

      ImportRewriteContext importRewriteContext =
          new ContextSensitiveImportRewriteContext(declNode, imports);
      Type newInterface = imports.addImport(fNewInterface, ast, importRewriteContext);
      ListRewrite listRewrite =
          rewrite.getListRewrite(declNode, TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY);
      listRewrite.insertLast(newInterface, null);

      // set up linked mode
      final String KEY_TYPE = "type"; // $NON-NLS-1$
      addLinkedPosition(rewrite.track(newInterface), true, KEY_TYPE);
      return rewrite;
    }
    return null;
  }
  public void registerAddedStaticImport(IBinding binding) {
    if (binding instanceof IVariableBinding) {
      ITypeBinding declaringType = ((IVariableBinding) binding).getDeclaringClass();
      fAddedStaticImports.add(
          new StaticImportData(
              Bindings.getRawQualifiedName(declaringType), binding.getName(), true));

    } else if (binding instanceof IMethodBinding) {
      ITypeBinding declaringType = ((IMethodBinding) binding).getDeclaringClass();
      fAddedStaticImports.add(
          new StaticImportData(
              Bindings.getRawQualifiedName(declaringType), binding.getName(), false));

    } else {
      throw new IllegalArgumentException(binding.toString());
    }
  }
  private void possibleStaticImportFound(Name name) {
    if (fStaticImports == null || fASTRoot == null) {
      return;
    }

    while (name.isQualifiedName()) {
      name = ((QualifiedName) name).getQualifier();
    }
    if (!isAffected(name)) {
      return;
    }

    IBinding binding = name.resolveBinding();
    SimpleName simpleName = (SimpleName) name;
    if (binding == null
        || binding instanceof ITypeBinding
        || !Modifier.isStatic(binding.getModifiers())
        || simpleName.isDeclaration()) {
      return;
    }

    if (binding instanceof IVariableBinding) {
      IVariableBinding varBinding = (IVariableBinding) binding;
      if (varBinding.isField()) {
        varBinding = varBinding.getVariableDeclaration();
        ITypeBinding declaringClass = varBinding.getDeclaringClass();
        if (declaringClass != null && !declaringClass.isLocal()) {
          if (new ScopeAnalyzer(fASTRoot)
              .isDeclaredInScope(
                  varBinding, simpleName, ScopeAnalyzer.VARIABLES | ScopeAnalyzer.CHECK_VISIBILITY))
            return;
          fStaticImports.add(simpleName);
        }
      }
    } else if (binding instanceof IMethodBinding) {
      IMethodBinding methodBinding = ((IMethodBinding) binding).getMethodDeclaration();
      ITypeBinding declaringClass = methodBinding.getDeclaringClass();
      if (declaringClass != null && !declaringClass.isLocal()) {
        if (new ScopeAnalyzer(fASTRoot)
            .isDeclaredInScope(
                methodBinding, simpleName, ScopeAnalyzer.METHODS | ScopeAnalyzer.CHECK_VISIBILITY))
          return;
        fStaticImports.add(simpleName);
      }
    }
  }
Exemplo n.º 6
0
 public static IAnnotationBinding getAnnotation(IBinding binding, Class<?> annotationClass) {
   for (IAnnotationBinding annotation : binding.getAnnotations()) {
     if (typeEqualsClass(annotation.getAnnotationType(), annotationClass)) {
       return annotation;
     }
   }
   return null;
 }
Exemplo n.º 7
0
 /** Less strict version of the above where we don't care about the annotation's package. */
 public static boolean hasNamedAnnotation(IBinding binding, String annotationName) {
   for (IAnnotationBinding annotation : binding.getAnnotations()) {
     if (annotation.getName().equals(annotationName)) {
       return true;
     }
   }
   return false;
 }
Exemplo n.º 8
0
 /**
  * Return true if a binding has a named "Nonnull" annotation. Package names aren't checked because
  * different nonnull annotations are defined in several Java frameworks, with varying but similar
  * names.
  */
 public static boolean hasNonnullAnnotation(IBinding binding) {
   Pattern p = Pattern.compile("No[nt][Nn]ull");
   for (IAnnotationBinding annotation : binding.getAnnotations()) {
     if (p.matcher(annotation.getName()).matches()) {
       return true;
     }
   }
   return false;
 }
  private String[] getVariableNameProposals(ITypeBinding arrayTypeBinding, IJavaProject project) {
    String[] variableNames = getUsedVariableNames();
    String baseName = FOR_LOOP_ELEMENT_IDENTIFIER;
    String name = fArrayBinding.getName();
    if (name.length() > 2 && name.charAt(name.length() - 1) == 's') {
      baseName = name.substring(0, name.length() - 1);
    }
    String[] elementSuggestions =
        StubUtility.getLocalNameSuggestions(project, baseName, 0, variableNames);

    String type = arrayTypeBinding.getElementType().getName();
    String[] typeSuggestions =
        StubUtility.getLocalNameSuggestions(
            project, type, arrayTypeBinding.getDimensions() - 1, variableNames);

    String[] result = new String[elementSuggestions.length + typeSuggestions.length];
    System.arraycopy(elementSuggestions, 0, result, 0, elementSuggestions.length);
    System.arraycopy(typeSuggestions, 0, result, elementSuggestions.length, typeSuggestions.length);
    return result;
  }
Exemplo n.º 10
0
  String getSourceLocation(IBinding methodBinding, ITypeBinding declaringClass) {
    if (methodBinding == null) {
      throw new IllegalStateException("Cannot find methodbinding, running without workspace?");
    }

    IJavaElement javaElement = methodBinding.getJavaElement();
    if (javaElement == null) {
      return null;
    }
    IPath path = javaElement.getPath();
    if ("jar".equals(path.getFileExtension())) {
      return path.lastSegment();
    } else if (declaringClass != null) {
      return declaringClass
          .getJavaElement()
          .getResource()
          .getProjectRelativePath()
          .toPortableString();
    } else {
      return javaElement.getPath().toPortableString();
    }
  }
Exemplo n.º 11
0
 public static boolean isAbstract(IBinding binding) {
   return Modifier.isAbstract(binding.getModifiers());
 }
Exemplo n.º 12
0
 protected ISourceLocation resolveBinding(IBinding binding) {
   ISourceLocation resolvedBinding = bindingsResolver.resolveBinding(binding);
   if (binding != null) locationCache.put(binding.getKey(), resolvedBinding);
   return resolvedBinding;
 }
Exemplo n.º 13
0
 public static boolean isPrivate(IBinding binding) {
   return Modifier.isPrivate(binding.getModifiers());
 }
Exemplo n.º 14
0
 public static boolean isFinal(IBinding binding) {
   return Modifier.isFinal(binding.getModifiers());
 }
Exemplo n.º 15
0
 @Override
 public boolean visit(MethodDeclaration node) {
   IBinding binding = node.resolveBinding();
   if (binding.getJavaElement().equals(method)) this.declaration = node;
   return super.visit(node);
 }
  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);
  }
Exemplo n.º 17
0
 public static boolean isSynchronized(IBinding binding) {
   return Modifier.isSynchronized(binding.getModifiers());
 }