private JavaElement getJavaElement(LocalVariableBinding binding) {
    LocalDeclaration local = binding.declaration;

    JavaElement parent = null;
    ReferenceContext referenceContext =
        binding.declaringScope.isLambdaSubscope()
            ? binding.declaringScope.namedMethodScope().referenceContext()
            : binding.declaringScope.referenceContext();
    if (referenceContext instanceof AbstractMethodDeclaration) {
      AbstractMethodDeclaration methodDeclaration = (AbstractMethodDeclaration) referenceContext;
      parent = this.getJavaElementOfCompilationUnit(methodDeclaration, methodDeclaration.binding);
    } else if (referenceContext instanceof TypeDeclaration) {
      // Local variable is declared inside an initializer
      TypeDeclaration typeDeclaration = (TypeDeclaration) referenceContext;

      JavaElement type =
          this.getJavaElementOfCompilationUnit(typeDeclaration, typeDeclaration.binding);
      parent = Util.getUnresolvedJavaElement(local.sourceStart, local.sourceEnd, type);
    }
    if (parent == null) return null;

    return new LocalVariable(
        parent,
        new String(local.name),
        local.declarationSourceStart,
        local.declarationSourceEnd,
        local.sourceStart,
        local.sourceEnd,
        local.type == null
            ? Signature.createTypeSignature(binding.type.signableName(), true)
            : Util.typeSignature(local.type),
        binding.declaration.annotations,
        local.modifiers,
        local.getKind() == AbstractVariableDeclaration.PARAMETER);
  }
Esempio n. 2
0
  private JavaElement getUnresolvedJavaElement() {
    if (JavaCore.getPlugin() == null) {
      return null;
    }
    if (!(this.resolver instanceof DefaultBindingResolver)) return null;

    DefaultBindingResolver defaultBindingResolver = (DefaultBindingResolver) this.resolver;
    if (!defaultBindingResolver.fromJavaProject) return null;
    return Util.getUnresolvedJavaElement(
        this.binding,
        defaultBindingResolver.workingCopyOwner,
        defaultBindingResolver.getBindingsToNodesMap());
  }
 private JavaElement getUnresolvedJavaElement(
     org.eclipse.jdt.internal.compiler.lookup.TypeBinding typeBinding) {
   if (JavaCore.getPlugin() == null) {
     return null;
   }
   if (this.resolver instanceof DefaultBindingResolver) {
     DefaultBindingResolver defaultBindingResolver = (DefaultBindingResolver) this.resolver;
     if (!defaultBindingResolver.fromJavaProject) return null;
     return org.eclipse.jdt.internal.core.util.Util.getUnresolvedJavaElement(
         typeBinding,
         defaultBindingResolver.workingCopyOwner,
         defaultBindingResolver.getBindingsToNodesMap());
   }
   return null;
 }
  public IJavaElement[] getVisibleElements(String typeSignature) {
    if (this.assistScope == null) return new IJavaElement[0];

    if (!this.hasComputedVisibleElementBindings) {
      computeVisibleElementBindings();
    }

    TypeBinding assignableTypeBinding = null;
    if (typeSignature != null) {
      assignableTypeBinding = getTypeFromSignature(typeSignature, this.assistScope);
      if (assignableTypeBinding == null) return new IJavaElement[0];
    }

    int length =
        this.visibleLocalVariables.size() + this.visibleFields.size() + this.visibleMethods.size();
    if (length == 0) return new IJavaElement[0];

    IJavaElement[] result = new IJavaElement[length];

    int elementCount = 0;

    int size = this.visibleLocalVariables.size();
    if (size > 0) {
      next:
      for (int i = 0; i < size; i++) {
        try {
          LocalVariableBinding binding =
              (LocalVariableBinding) this.visibleLocalVariables.elementAt(i);
          if (binding.type == null
              || (assignableTypeBinding != null
                  && !binding.type.isCompatibleWith(assignableTypeBinding))) continue next;
          JavaElement localVariable = getJavaElement(binding);
          if (localVariable != null) result[elementCount++] = localVariable;
        } catch (AbortCompilation e) {
          // log the exception and proceed
          Util.logRepeatedMessage(e.getKey(), e);
        }
      }
    }
    size = this.visibleFields.size();
    if (size > 0) {
      next:
      for (int i = 0; i < size; i++) {
        try {
          FieldBinding binding = (FieldBinding) this.visibleFields.elementAt(i);
          if (assignableTypeBinding != null
              && !binding.type.isCompatibleWith(assignableTypeBinding)) continue next;
          if (this.assistScope.isDefinedInSameUnit(binding.declaringClass)) {
            JavaElement field = getJavaElementOfCompilationUnit(binding);
            if (field != null) result[elementCount++] = field;
          } else {
            JavaElement field = Util.getUnresolvedJavaElement(binding, this.owner, EmptyNodeMap);
            if (field != null) result[elementCount++] = field.resolved(binding);
          }
        } catch (AbortCompilation e) {
          // log the exception and proceed
          Util.logRepeatedMessage(e.getKey(), e);
        }
      }
    }
    size = this.visibleMethods.size();
    if (size > 0) {
      next:
      for (int i = 0; i < size; i++) {
        try {
          MethodBinding binding = (MethodBinding) this.visibleMethods.elementAt(i);
          if (assignableTypeBinding != null
              && !binding.returnType.isCompatibleWith(assignableTypeBinding)) continue next;
          if (this.assistScope.isDefinedInSameUnit(binding.declaringClass)) {
            JavaElement method = getJavaElementOfCompilationUnit(binding);
            if (method != null) result[elementCount++] = method;
          } else {
            JavaElement method = Util.getUnresolvedJavaElement(binding, this.owner, EmptyNodeMap);
            if (method != null) result[elementCount++] = method.resolved(binding);
          }
        } catch (AbortCompilation e) {
          // log the exception and proceed
          Util.logRepeatedMessage(e.getKey(), e);
        }
      }
    }

    if (elementCount != result.length) {
      System.arraycopy(result, 0, result = new IJavaElement[elementCount], 0, elementCount);
    }

    return result;
  }