Ejemplo n.º 1
0
  private void extractIType(IType type) {
    try {
      String fqn = type.getFullyQualifiedName();

      // Write the entity
      if (type.isClass()) {
        entityWriter.writeClass(fqn, type.getFlags(), path);

        // Write the superclass
        String superSig = type.getSuperclassTypeSignature();
        if (superSig != null) {
          relationWriter.writeExtends(fqn, typeSignatureToFqn(superSig), path);
        }
      } else if (type.isAnnotation()) {
        entityWriter.writeAnnotation(fqn, type.getFlags(), path);
      } else if (type.isInterface()) {
        entityWriter.writeInterface(fqn, type.getFlags(), path);
      } else if (type.isEnum()) {
        entityWriter.writeEnum(fqn, type.getFlags(), path);
      }

      // Write the superinterfaces
      for (String superIntSig : type.getSuperInterfaceTypeSignatures()) {
        relationWriter.writeImplements(fqn, typeSignatureToFqn(superIntSig), path);
      }

      if (!fqnStack.isEmpty()) {
        relationWriter.writeInside(type.getFullyQualifiedName(), fqnStack.peek(), path);
      }

      fqnStack.push(type.getFullyQualifiedName());

      for (IType child : type.getTypes()) {
        extractIType(child);
      }

      for (IField field : type.getFields()) {
        if (!Flags.isSynthetic(field.getFlags())) {
          extractIField(field);
        }
      }

      for (IMethod method : type.getMethods()) {
        if (!Flags.isSynthetic(method.getFlags())
            || (Flags.isSynthetic(method.getFlags())
                && method.isConstructor()
                && method.getParameterTypes().length == 0)) {
          extractIMethod(method, type.isAnnotation());
        }
      }

      int pos = 0;
      for (ITypeParameter param : type.getTypeParameters()) {
        relationWriter.writeParametrizedBy(fqn, getTypeParam(param), pos++, path);
      }

      fqnStack.pop();
    } catch (Exception e) {
      logger.log(Level.SEVERE, "Error in extracting class file", e);
    }
  }
  /**
   * Creates a qualified class name from a class name which doesn't contain package name.
   *
   * @param parent a full qualified class name of the class which uses this variable
   * @param type a class name which doesn't contain package name
   * @return full a created qualified class name
   */
  public static String getFullQName(IType parent, String type) {
    if (type.indexOf('.') >= 0) {
      return type;
    }
    if (isPrimitive(type)) {
      return type;
    }
    IJavaProject project = parent.getJavaProject();
    try {
      IType javaType = project.findType("java.lang." + type);
      if (javaType != null && javaType.exists()) {
        return javaType.getFullyQualifiedName();
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    while (true) {
      try {
        IType javaType = project.findType(parent.getFullyQualifiedName() + "." + type);
        if (javaType != null && javaType.exists()) {
          return parent.getFullyQualifiedName() + "." + type;
        }
      } catch (Exception ex) {
        ex.printStackTrace();
      }
      try {
        IType javaType =
            project.findType(parent.getPackageFragment().getElementName() + "." + type);
        if (javaType != null && javaType.exists()) {
          return javaType.getFullyQualifiedName();
        }
      } catch (Exception ex) {
        ex.printStackTrace();
      }
      try {
        IImportDeclaration[] imports = parent.getCompilationUnit().getImports();
        for (int i = 0; i < imports.length; i++) {
          String importName = imports[i].getElementName();
          if (importName.endsWith("." + type)) {
            return importName;
          }
          if (importName.endsWith(".*")) {
            try {
              IType javaType = project.findType(importName.replaceFirst("\\*$", type));
              if (javaType != null && javaType.exists()) {
                return javaType.getFullyQualifiedName();
              }
            } catch (Exception ex) {
            }
          }
        }
      } catch (Exception ex) {
        ex.printStackTrace();
      }

      try {
        // スーパークラス
        if (parent.getSuperclassTypeSignature() == null) {
          break;
        }
        String superClass =
            JavaUtil.getFullQName(parent, Signature.toString(parent.getSuperclassTypeSignature()));

        if (superClass.startsWith("java.lang.")) {
          break;
        }

        parent = parent.getJavaProject().findType(superClass);
      } catch (JavaModelException ex) {
      }
    }
    return type;
  }