boolean matchSuperTypeReference(
      SuperTypeReferencePattern pattern, Object binaryInfo, IBinaryType enclosingBinaryType) {
    if (!(binaryInfo instanceof IBinaryType)) return false;

    IBinaryType type = (IBinaryType) binaryInfo;
    if (pattern.superRefKind != SuperTypeReferencePattern.ONLY_SUPER_INTERFACES) {
      char[] vmName = type.getSuperclassName();
      if (vmName != null) {
        char[] superclassName = convertClassFileFormat(vmName);
        if (checkTypeName(
            pattern.superSimpleName,
            pattern.superQualification,
            superclassName,
            pattern.isCaseSensitive(),
            pattern.isCamelCase())) return true;
      }
    }

    if (pattern.superRefKind != SuperTypeReferencePattern.ONLY_SUPER_CLASSES) {
      char[][] superInterfaces = type.getInterfaceNames();
      if (superInterfaces != null) {
        for (int i = 0, max = superInterfaces.length; i < max; i++) {
          char[] superInterfaceName = convertClassFileFormat(superInterfaces[i]);
          if (checkTypeName(
              pattern.superSimpleName,
              pattern.superQualification,
              superInterfaceName,
              pattern.isCaseSensitive(),
              pattern.isCamelCase())) return true;
        }
      }
    }
    return false;
  }
  boolean matchTypeDeclaration(
      TypeDeclarationPattern pattern, Object binaryInfo, IBinaryType enclosingBinaryType) {
    if (!(binaryInfo instanceof IBinaryType)) return false;

    IBinaryType type = (IBinaryType) binaryInfo;
    char[] fullyQualifiedTypeName = convertClassFileFormat(type.getName());
    boolean qualifiedPattern = pattern instanceof QualifiedTypeDeclarationPattern;
    if (pattern.enclosingTypeNames == null || qualifiedPattern) {
      char[] simpleName =
          (pattern.getMatchMode() == SearchPattern.R_PREFIX_MATCH)
              ? CharOperation.concat(pattern.simpleName, IIndexConstants.ONE_STAR)
              : pattern.simpleName;
      char[] pkg =
          qualifiedPattern
              ? ((QualifiedTypeDeclarationPattern) pattern).qualification
              : pattern.pkg;
      if (!checkTypeName(
          simpleName,
          pkg,
          fullyQualifiedTypeName,
          pattern.isCaseSensitive(),
          pattern.isCamelCase())) return false;
    } else {
      char[] enclosingTypeName = CharOperation.concatWith(pattern.enclosingTypeNames, '.');
      char[] patternString =
          pattern.pkg == null
              ? enclosingTypeName
              : CharOperation.concat(pattern.pkg, enclosingTypeName, '.');
      if (!checkTypeName(
          pattern.simpleName,
          patternString,
          fullyQualifiedTypeName,
          pattern.isCaseSensitive(),
          pattern.isCamelCase())) return false;
    }

    int kind = TypeDeclaration.kind(type.getModifiers());
    switch (pattern.typeSuffix) {
      case CLASS_SUFFIX:
        return kind == TypeDeclaration.CLASS_DECL;
      case INTERFACE_SUFFIX:
        return kind == TypeDeclaration.INTERFACE_DECL;
      case ENUM_SUFFIX:
        return kind == TypeDeclaration.ENUM_DECL;
      case ANNOTATION_TYPE_SUFFIX:
        return kind == TypeDeclaration.ANNOTATION_TYPE_DECL;
      case CLASS_AND_INTERFACE_SUFFIX:
        return kind == TypeDeclaration.CLASS_DECL || kind == TypeDeclaration.INTERFACE_DECL;
      case CLASS_AND_ENUM_SUFFIX:
        return kind == TypeDeclaration.CLASS_DECL || kind == TypeDeclaration.ENUM_DECL;
      case INTERFACE_AND_ANNOTATION_SUFFIX:
        return kind == TypeDeclaration.INTERFACE_DECL
            || kind == TypeDeclaration.ANNOTATION_TYPE_DECL;
      case TYPE_SUFFIX: // nothing
    }
    return true;
  }
Ejemplo n.º 3
0
  public static void main(String[] args) throws UnsupportedEncodingException {
    IObject memObj = new MemObject();
    memObj.set("key1", Types.INT, 33);
    System.out.println("key1=" + memObj.as("key1", Types.INT));

    JsonObject jsonObject = new JsonObject();
    jsonObject.set("welt", Types.INT, 44);
    System.out.println("key1(json)=" + jsonObject.as("welt", Types.INT));

    JsonObject innerObj = new JsonObject();
    innerObj.set("innerKey", Types.INT, 32);

    JsonArray jsa = new JsonArray();
    jsa.add(Types.STRING, "Ich bin ein String im Array");
    jsa.add(Types.INT, 666);
    jsa.add(Types.OBJECT, innerObj);
    jsonObject.set("keyZumArray", Types.ARRAY, jsa);

    final byte[] binarySrc =
        (new String("Ich bin binäre daten, encodiert in UTF-8")).getBytes("UTF-8");
    final IBinaryType binary = new JsonBinary();
    binary.write(
        new IBinaryWriter() {
          @Override
          public void write(OutputStream writer) {
            try {
              writer.write(binarySrc);
            } catch (IOException e) {
              e.printStackTrace(); // To change body of catch statement use File | Settings |
              // File Templates.
            }
          }
        });
    jsonObject.set("binaryKey", Types.BINARY, binary);

    System.out.println("json=" + jsonObject.toJsonString());

    final Optional<IBinaryType> binOutput = jsonObject.as("binaryKey", Types.BINARY);
    binOutput
        .get()
        .read(
            new IBinaryReader() {
              @Override
              public void read(InputStream reader) {}
            });
  }
Ejemplo n.º 4
0
 /** Add an additional binary type */
 public void accept(
     IBinaryType binaryType, PackageBinding packageBinding, AccessRestriction accessRestriction) {
   if (this.options.verbose) {
     this.out.println(
         Messages.bind(Messages.compilation_loadBinary, new String(binaryType.getName())));
     //			new Exception("TRACE BINARY").printStackTrace(System.out);
     //		    System.out.println();
   }
   this.lookupEnvironment.createBinaryTypeFrom(binaryType, packageBinding, accessRestriction);
 }
  private boolean checkDeclaringType(
      IBinaryType enclosingBinaryType,
      char[] simpleName,
      char[] qualification,
      boolean isCaseSensitive,
      boolean isCamelCase) {
    if (simpleName == null && qualification == null) return true;
    if (enclosingBinaryType == null) return true;

    char[] declaringTypeName = convertClassFileFormat(enclosingBinaryType.getName());
    return checkTypeName(
        simpleName, qualification, declaringTypeName, isCaseSensitive, isCamelCase);
  }
  /*
   * Look for annotations references
   */
  private void matchAnnotations(
      SearchPattern pattern, MatchLocator locator, ClassFile classFile, IBinaryType binaryType)
      throws CoreException {
    // Only process TypeReference patterns
    switch (pattern.kind) {
      case TYPE_REF_PATTERN:
        break;
      case OR_PATTERN:
        SearchPattern[] patterns = ((OrPattern) pattern).patterns;
        for (int i = 0, length = patterns.length; i < length; i++) {
          matchAnnotations(patterns[i], locator, classFile, binaryType);
        }
        // $FALL-THROUGH$ - fall through default to return
      default:
        return;
    }
    TypeReferencePattern typeReferencePattern = (TypeReferencePattern) pattern;

    // Look for references in class annotations
    IBinaryAnnotation[] annotations = binaryType.getAnnotations();
    BinaryType classFileBinaryType = (BinaryType) classFile.getType();
    BinaryTypeBinding binaryTypeBinding = null;
    if (checkAnnotations(typeReferencePattern, annotations, binaryType.getTagBits())) {
      classFileBinaryType =
          new ResolvedBinaryType(
              (JavaElement) classFileBinaryType.getParent(),
              classFileBinaryType.getElementName(),
              classFileBinaryType.getKey());
      TypeReferenceMatch match =
          new TypeReferenceMatch(
              classFileBinaryType,
              SearchMatch.A_ACCURATE,
              -1,
              0,
              false,
              locator.getParticipant(),
              locator.currentPossibleMatch.resource);
      // TODO 3.4 M7 (frederic) - bug 209996: see how create the annotation handle from the binary
      // and put it in the local element
      match.setLocalElement(null);
      locator.report(match);
    }

    // Look for references in methods annotations
    MethodInfo[] methods = (MethodInfo[]) binaryType.getMethods();
    if (methods != null) {
      for (int i = 0, max = methods.length; i < max; i++) {
        MethodInfo method = methods[i];
        if (checkAnnotations(typeReferencePattern, method.getAnnotations(), method.getTagBits())) {
          binaryTypeBinding = locator.cacheBinaryType(classFileBinaryType, binaryType);
          IMethod methodHandle =
              classFileBinaryType.getMethod(
                  new String(
                      method.isConstructor()
                          ? binaryTypeBinding
                              .compoundName[binaryTypeBinding.compoundName.length - 1]
                          : method.getSelector()),
                  CharOperation.toStrings(
                      Signature.getParameterTypes(
                          convertClassFileFormat(method.getMethodDescriptor()))));
          TypeReferenceMatch match =
              new TypeReferenceMatch(
                  methodHandle,
                  SearchMatch.A_ACCURATE,
                  -1,
                  0,
                  false,
                  locator.getParticipant(),
                  locator.currentPossibleMatch.resource);
          // TODO 3.4 M7 (frederic) - bug 209996: see how create the annotation handle from the
          // binary and put it in the local element
          match.setLocalElement(null);
          locator.report(match);
        }
      }
    }

    // Look for references in fields annotations
    FieldInfo[] fields = (FieldInfo[]) binaryType.getFields();
    if (fields != null) {
      for (int i = 0, max = fields.length; i < max; i++) {
        FieldInfo field = fields[i];
        if (checkAnnotations(typeReferencePattern, field.getAnnotations(), field.getTagBits())) {
          IField fieldHandle = classFileBinaryType.getField(new String(field.getName()));
          TypeReferenceMatch match =
              new TypeReferenceMatch(
                  fieldHandle,
                  SearchMatch.A_ACCURATE,
                  -1,
                  0,
                  false,
                  locator.getParticipant(),
                  locator.currentPossibleMatch.resource);
          // TODO 3.4 M7 (frederic) - bug 209996: see how create the annotation handle from the
          // binary and put it in the local element
          match.setLocalElement(null);
          locator.report(match);
        }
      }
    }
  }
  /** Locate declaration in the current class file. This class file is always in a jar. */
  public void locateMatches(MatchLocator locator, ClassFile classFile, IBinaryType info)
      throws CoreException {
    SearchPattern pattern = locator.pattern;

    // check annotations references
    matchAnnotations(pattern, locator, classFile, info);

    // check class definition
    BinaryType binaryType = (BinaryType) classFile.getType();
    if (matchBinary(pattern, info, null)) {
      binaryType =
          new ResolvedBinaryType(
              (JavaElement) binaryType.getParent(),
              binaryType.getElementName(),
              binaryType.getKey());
      locator.reportBinaryMemberDeclaration(null, binaryType, null, info, SearchMatch.A_ACCURATE);
      return;
    }

    // Define arrays to store methods/fields from binary type if necessary
    IBinaryMethod[] binaryMethods = info.getMethods();
    int bMethodsLength = binaryMethods == null ? 0 : binaryMethods.length;
    IBinaryMethod[] unresolvedMethods = null;
    char[][] binaryMethodSignatures = null;
    boolean hasUnresolvedMethods = false;

    // Get fields from binary type info
    IBinaryField[] binaryFields = info.getFields();
    int bFieldsLength = binaryFields == null ? 0 : binaryFields.length;
    IBinaryField[] unresolvedFields = null;
    boolean hasUnresolvedFields = false;

    // Report as many accurate matches as possible
    int accuracy = SearchMatch.A_ACCURATE;
    boolean mustResolve = pattern.mustResolve;
    if (mustResolve) {
      BinaryTypeBinding binding = locator.cacheBinaryType(binaryType, info);
      if (binding != null) {
        // filter out element not in hierarchy scope
        if (!locator.typeInHierarchy(binding)) return;

        // Search matches on resolved methods
        MethodBinding[] availableMethods = binding.availableMethods();
        int aMethodsLength = availableMethods == null ? 0 : availableMethods.length;
        hasUnresolvedMethods = bMethodsLength != aMethodsLength;
        for (int i = 0; i < aMethodsLength; i++) {
          MethodBinding method = availableMethods[i];
          char[] methodSignature = method.genericSignature();
          if (methodSignature == null) methodSignature = method.signature();

          // Report the match if possible
          int level = locator.patternLocator.resolveLevel(method);
          if (level != PatternLocator.IMPOSSIBLE_MATCH) {
            IMethod methodHandle =
                binaryType.getMethod(
                    new String(
                        method.isConstructor()
                            ? binding.compoundName[binding.compoundName.length - 1]
                            : method.selector),
                    CharOperation.toStrings(
                        Signature.getParameterTypes(convertClassFileFormat(methodSignature))));
            accuracy =
                level == PatternLocator.ACCURATE_MATCH
                    ? SearchMatch.A_ACCURATE
                    : SearchMatch.A_INACCURATE;
            locator.reportBinaryMemberDeclaration(null, methodHandle, method, info, accuracy);
          }

          // Remove method from unresolved list
          if (hasUnresolvedMethods) {
            if (binaryMethodSignatures
                == null) { // Store binary method signatures to avoid multiple computation
              binaryMethodSignatures = new char[bMethodsLength][];
              for (int j = 0; j < bMethodsLength; j++) {
                IBinaryMethod binaryMethod = binaryMethods[j];
                char[] signature = binaryMethod.getGenericSignature();
                if (signature == null) signature = binaryMethod.getMethodDescriptor();
                binaryMethodSignatures[j] = signature;
              }
            }
            for (int j = 0; j < bMethodsLength; j++) {
              if (CharOperation.equals(binaryMethods[j].getSelector(), method.selector)
                  && CharOperation.equals(binaryMethodSignatures[j], methodSignature)) {
                if (unresolvedMethods == null) {
                  System.arraycopy(
                      binaryMethods,
                      0,
                      unresolvedMethods = new IBinaryMethod[bMethodsLength],
                      0,
                      bMethodsLength);
                }
                unresolvedMethods[j] = null;
                break;
              }
            }
          }
        }

        // Search matches on resolved fields
        FieldBinding[] availableFields = binding.availableFields();
        int aFieldsLength = availableFields == null ? 0 : availableFields.length;
        hasUnresolvedFields = bFieldsLength != aFieldsLength;
        for (int i = 0; i < aFieldsLength; i++) {
          FieldBinding field = availableFields[i];

          // Report the match if possible
          int level = locator.patternLocator.resolveLevel(field);
          if (level != PatternLocator.IMPOSSIBLE_MATCH) {
            IField fieldHandle = binaryType.getField(new String(field.name));
            accuracy =
                level == PatternLocator.ACCURATE_MATCH
                    ? SearchMatch.A_ACCURATE
                    : SearchMatch.A_INACCURATE;
            locator.reportBinaryMemberDeclaration(null, fieldHandle, field, info, accuracy);
          }

          // Remove the field from unresolved list
          if (hasUnresolvedFields) {
            for (int j = 0; j < bFieldsLength; j++) {
              if (CharOperation.equals(binaryFields[j].getName(), field.name)) {
                if (unresolvedFields == null) {
                  System.arraycopy(
                      binaryFields,
                      0,
                      unresolvedFields = new IBinaryField[bFieldsLength],
                      0,
                      bFieldsLength);
                }
                unresolvedFields[j] = null;
                break;
              }
            }
          }
        }

        // If all methods/fields were accurate then returns now
        if (!hasUnresolvedMethods && !hasUnresolvedFields) {
          return;
        }
      }
      accuracy = SearchMatch.A_INACCURATE;
    }

    // Report inaccurate methods
    if (mustResolve) binaryMethods = unresolvedMethods;
    bMethodsLength = binaryMethods == null ? 0 : binaryMethods.length;
    for (int i = 0; i < bMethodsLength; i++) {
      IBinaryMethod method = binaryMethods[i];
      if (method == null) continue; // impossible match or already reported as accurate
      if (matchBinary(pattern, method, info)) {
        char[] name;
        if (method.isConstructor()) {
          name = info.getName();
          int lastSlash = CharOperation.lastIndexOf('/', name);
          if (lastSlash != -1) {
            name = CharOperation.subarray(name, lastSlash + 1, name.length);
          }
        } else {
          name = method.getSelector();
        }
        String selector = new String(name);
        char[] methodSignature = binaryMethodSignatures == null ? null : binaryMethodSignatures[i];
        if (methodSignature == null) {
          methodSignature = method.getGenericSignature();
          if (methodSignature == null) methodSignature = method.getMethodDescriptor();
        }
        String[] parameterTypes =
            CharOperation.toStrings(
                Signature.getParameterTypes(convertClassFileFormat(methodSignature)));
        IMethod methodHandle = binaryType.getMethod(selector, parameterTypes);
        methodHandle =
            new ResolvedBinaryMethod(binaryType, selector, parameterTypes, methodHandle.getKey());
        locator.reportBinaryMemberDeclaration(null, methodHandle, null, info, accuracy);
      }
    }

    // Report inaccurate fields
    if (mustResolve) binaryFields = unresolvedFields;
    bFieldsLength = binaryFields == null ? 0 : binaryFields.length;
    for (int i = 0; i < bFieldsLength; i++) {
      IBinaryField field = binaryFields[i];
      if (field == null) continue; // impossible match or already reported as accurate
      if (matchBinary(pattern, field, info)) {
        String fieldName = new String(field.getName());
        IField fieldHandle = binaryType.getField(fieldName);
        fieldHandle = new ResolvedBinaryField(binaryType, fieldName, fieldHandle.getKey());
        locator.reportBinaryMemberDeclaration(null, fieldHandle, null, info, accuracy);
      }
    }
  }