Esempio n. 1
0
 private char[][][] getQualifiedNames(ObjectVector types) {
   final int size = types.size;
   char[][][] focusQualifiedNames = null;
   IJavaElement javaElement = this.pattern.focus;
   int index = 0;
   while (javaElement != null && !(javaElement instanceof ITypeRoot)) {
     javaElement = javaElement.getParent();
   }
   if (javaElement != null) {
     IType primaryType = ((ITypeRoot) javaElement).findPrimaryType();
     if (primaryType != null) {
       focusQualifiedNames = new char[size + 1][][];
       focusQualifiedNames[index++] =
           CharOperation.splitOn('.', primaryType.getFullyQualifiedName().toCharArray());
     }
   }
   if (focusQualifiedNames == null) {
     focusQualifiedNames = new char[size][][];
   }
   for (int i = 0; i < size; i++) {
     focusQualifiedNames[index++] =
         CharOperation.splitOn(
             '.', ((IType) (types.elementAt(i))).getFullyQualifiedName().toCharArray());
   }
   return focusQualifiedNames.length == 0
       ? null
       : ReferenceCollection.internQualifiedNames(focusQualifiedNames, true);
 }
  private void searchVisibleFields(
      FieldBinding[] fields,
      ReferenceBinding receiverType,
      Scope scope,
      InvocationSite invocationSite,
      Scope invocationScope,
      boolean onlyStaticFields,
      ObjectVector localsFound,
      ObjectVector fieldsFound) {
    ObjectVector newFieldsFound = new ObjectVector();
    // Inherited fields which are hidden by subclasses are filtered out
    // No visibility checks can be performed without the scope & invocationSite

    next:
    for (int f = fields.length; --f >= 0; ) {
      FieldBinding field = fields[f];

      if (field.isSynthetic()) continue next;

      if (onlyStaticFields && !field.isStatic()) continue next;

      if (!field.canBeSeenBy(receiverType, invocationSite, scope)) continue next;

      for (int i = fieldsFound.size; --i >= 0; ) {
        FieldBinding otherField = (FieldBinding) fieldsFound.elementAt(i);
        if (CharOperation.equals(field.name, otherField.name, true)) {
          continue next;
        }
      }

      for (int l = localsFound.size; --l >= 0; ) {
        LocalVariableBinding local = (LocalVariableBinding) localsFound.elementAt(l);

        if (CharOperation.equals(field.name, local.name, true)) {
          continue next;
        }
      }

      newFieldsFound.add(field);
    }

    fieldsFound.addAll(newFieldsFound);
  }
  private void searchVisibleLocalMethods(
      MethodBinding[] methods,
      ReferenceBinding receiverType,
      Scope scope,
      InvocationSite invocationSite,
      Scope invocationScope,
      boolean onlyStaticMethods,
      ObjectVector methodsFound) {
    ObjectVector newMethodsFound = new ObjectVector();
    // Inherited methods which are hidden by subclasses are filtered out
    // No visibility checks can be performed without the scope & invocationSite

    next:
    for (int f = methods.length; --f >= 0; ) {
      MethodBinding method = methods[f];

      if (method.isSynthetic()) continue next;

      if (method.isDefaultAbstract()) continue next;

      if (method.isConstructor()) continue next;

      if (onlyStaticMethods && !method.isStatic()) continue next;

      if (!method.canBeSeenBy(receiverType, invocationSite, scope)) continue next;

      for (int i = methodsFound.size; --i >= 0; ) {
        MethodBinding otherMethod = (MethodBinding) methodsFound.elementAt(i);
        if (method == otherMethod) continue next;

        if (CharOperation.equals(method.selector, otherMethod.selector, true)) {
          if (this.lookupEnvironment.methodVerifier().isMethodSubsignature(otherMethod, method)) {
            continue next;
          }
        }
      }

      newMethodsFound.add(method);
    }

    methodsFound.addAll(newMethodsFound);
  }
Esempio n. 4
0
 /*
  * Create the list of focused jars or projects.
  */
 private static IJavaElement[] getFocusedElementsAndTypes(
     SearchPattern pattern, IJavaElement focusElement, ObjectVector superTypes)
     throws JavaModelException {
   if (pattern instanceof MethodPattern) {
     // For method pattern, it needs to walk along the focus type super hierarchy
     // and add jars/projects of all the encountered types.
     IType type = (IType) pattern.focus.getAncestor(IJavaElement.TYPE);
     MethodPattern methodPattern = (MethodPattern) pattern;
     String selector = new String(methodPattern.selector);
     int parameterCount = methodPattern.parameterCount;
     ITypeHierarchy superHierarchy = type.newSupertypeHierarchy(null);
     IType[] allTypes = superHierarchy.getAllSupertypes(type);
     int length = allTypes.length;
     SimpleSet focusSet = new SimpleSet(length + 1);
     if (focusElement != null) focusSet.add(focusElement);
     for (int i = 0; i < length; i++) {
       IMethod[] methods = allTypes[i].getMethods();
       int mLength = methods.length;
       for (int m = 0; m < mLength; m++) {
         if (parameterCount == methods[m].getNumberOfParameters()
             && methods[m].getElementName().equals(selector)) {
           IPackageFragmentRoot root =
               (IPackageFragmentRoot) allTypes[i].getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
           IJavaElement element = root.isArchive() ? root : root.getParent();
           focusSet.add(element);
           if (superTypes != null) superTypes.add(allTypes[i]);
           break;
         }
       }
     }
     // Rebuilt a contiguous array
     IJavaElement[] focuses = new IJavaElement[focusSet.elementSize];
     Object[] values = focusSet.values;
     int count = 0;
     for (int i = values.length; --i >= 0; ) {
       if (values[i] != null) {
         focuses[count++] = (IJavaElement) values[i];
       }
     }
     return focuses;
   }
   if (focusElement == null) return new IJavaElement[0];
   return new IJavaElement[] {focusElement};
 }
  private void searchVisibleVariablesAndMethods(
      Scope scope,
      ObjectVector localsFound,
      ObjectVector fieldsFound,
      ObjectVector methodsFound,
      boolean notInJavadoc) {

    InvocationSite invocationSite = CompletionEngine.FakeInvocationSite;

    boolean staticsOnly = false;
    // need to know if we're in a static context (or inside a constructor)

    Scope currentScope = scope;

    done1:
    while (true) { // done when a COMPILATION_UNIT_SCOPE is found

      switch (currentScope.kind) {
        case Scope.METHOD_SCOPE:
          // handle the error case inside an explicit constructor call (see MethodScope>>findField)
          MethodScope methodScope = (MethodScope) currentScope;
          staticsOnly |= methodScope.isStatic | methodScope.isConstructorCall;
          // $FALL-THROUGH$
        case Scope.BLOCK_SCOPE:
          BlockScope blockScope = (BlockScope) currentScope;

          next:
          for (int i = 0, length = blockScope.locals.length; i < length; i++) {
            LocalVariableBinding local = blockScope.locals[i];

            if (local == null) break next;

            if (local.isSecret()) continue next;
            // If the local variable declaration's initialization statement itself has the
            // completion,
            // then don't propose the local variable
            if (local.declaration.initialization != null) {
              /*(use this if-else block if it is found that local.declaration.initialization != null is not sufficient to
                guarantee that proposal is being asked inside a local variable declaration's initializer)
               if(local.declaration.initialization.sourceEnd > 0) {
              	if (this.assistNode.sourceEnd <= local.declaration.initialization.sourceEnd
              			&& this.assistNode.sourceStart >= local.declaration.initialization.sourceStart) {
              		continue next;
              	}
              } else {
              	CompletionNodeDetector detector = new CompletionNodeDetector(
              			this.assistNode,
              			local.declaration.initialization);
              	if (detector.containsCompletionNode()) {
              		continue next;
              	}
              }*/
              continue next;
            }
            for (int f = 0; f < localsFound.size; f++) {
              LocalVariableBinding otherLocal = (LocalVariableBinding) localsFound.elementAt(f);
              if (CharOperation.equals(otherLocal.name, local.name, true)) continue next;
            }

            localsFound.add(local);
          }
          break;

        case Scope.COMPILATION_UNIT_SCOPE:
          break done1;
      }
      currentScope = currentScope.parent;
    }

    staticsOnly = false;
    currentScope = scope;

    done2:
    while (true) { // done when a COMPILATION_UNIT_SCOPE is found

      switch (currentScope.kind) {
        case Scope.METHOD_SCOPE:
          // handle the error case inside an explicit constructor call (see MethodScope>>findField)
          MethodScope methodScope = (MethodScope) currentScope;
          staticsOnly |= methodScope.isStatic | methodScope.isConstructorCall;
          break;
        case Scope.CLASS_SCOPE:
          ClassScope classScope = (ClassScope) currentScope;
          SourceTypeBinding enclosingType = classScope.referenceContext.binding;

          searchVisibleFields(
              enclosingType,
              classScope,
              invocationSite,
              scope,
              staticsOnly,
              notInJavadoc,
              localsFound,
              fieldsFound);

          searchVisibleMethods(
              enclosingType,
              classScope,
              invocationSite,
              scope,
              staticsOnly,
              notInJavadoc,
              methodsFound);

          staticsOnly |= enclosingType.isStatic();
          break;

        case Scope.COMPILATION_UNIT_SCOPE:
          break done2;
      }
      currentScope = currentScope.parent;
    }

    // search in static import
    ImportBinding[] importBindings = scope.compilationUnitScope().imports;
    for (int i = 0; i < importBindings.length; i++) {
      ImportBinding importBinding = importBindings[i];
      if (importBinding.isValidBinding() && importBinding.isStatic()) {
        Binding binding = importBinding.resolvedImport;
        if (binding != null && binding.isValidBinding()) {
          if (importBinding.onDemand) {
            if ((binding.kind() & Binding.TYPE) != 0) {
              searchVisibleFields(
                  (ReferenceBinding) binding,
                  scope,
                  invocationSite,
                  scope,
                  staticsOnly,
                  notInJavadoc,
                  localsFound,
                  fieldsFound);

              searchVisibleMethods(
                  (ReferenceBinding) binding,
                  scope,
                  invocationSite,
                  scope,
                  staticsOnly,
                  notInJavadoc,
                  methodsFound);
            }
          } else {
            if ((binding.kind() & Binding.FIELD) != 0) {
              searchVisibleFields(
                  new FieldBinding[] {(FieldBinding) binding},
                  ((FieldBinding) binding).declaringClass,
                  scope,
                  invocationSite,
                  scope,
                  staticsOnly,
                  localsFound,
                  fieldsFound);
            } else if ((binding.kind() & Binding.METHOD) != 0) {
              MethodBinding methodBinding = (MethodBinding) binding;

              searchVisibleLocalMethods(
                  methodBinding.declaringClass.getMethods(methodBinding.selector),
                  methodBinding.declaringClass,
                  scope,
                  invocationSite,
                  scope,
                  true,
                  methodsFound);
            }
          }
        }
      }
    }
  }