Beispiel #1
0
  //
  // API
  //
  public List<JstType> translate() {

    ITranslateTracer tracer = getTracer();
    tracer.startGroup(DECLARATION_PHASE);

    try {
      TranslateCtx ctx = getCtx();
      TranslationMode mode = getMode();

      String srcName;
      CompilationUnit cu;
      BaseTypeVisitor visitor;
      TranslateInfo tInfo;

      List<TypeVisitTask> tasks = new ArrayList<TypeVisitTask>();

      // Create tasks
      for (JstType jstType : getStartingTypes()) {
        if (jstType == null) {
          addError(new TranslateError(TranslateMsgId.NULL_INPUT, "jstType is null"));
          continue;
        }
        // Exclude visited types
        tInfo = ctx.getTranslateInfo(jstType);
        if (mode.hasDeclaration() && tInfo.getMode().hasDeclaration()
            || mode.hasDependency() && tInfo.getMode().hasDependency()) {
          continue;
        }
        cu = AstBindingHelper.getCompilationUnit(jstType);
        srcName = AstBindingHelper.getSourceName(jstType);
        ctx.getTranslateInfo(jstType).addMode(mode);
        visitor = createVisitor(jstType);
        tasks.add(new TypeVisitTask(srcName, cu, visitor, getTracer(jstType), ctx));
      }

      // Execute tasks
      if (tasks.size() == 1 || !ctx.isParallelEnabled()) {
        for (TypeVisitTask task : tasks) {
          task.execute();
          addDependency(task.getVisitor());
          setExceptions(task.getType(), task.getExceptions());
        }
      } else if (tasks.size() > 0) {
        TranslationParallelRunner.getInstance().execute(tasks);
        for (TypeVisitTask task : tasks) {
          addDependency(task.getVisitor());
          setExceptions(task.getType(), task.getExceptions());
        }
      }

      mergeTraces();

      return getDependentTypes();
    } finally {
      tracer.endGroup(DECLARATION_PHASE, getErrors());
    }
  }
  private String getValue(QualifiedName name, ASTNode astNode, final JstType jstType) {

    TranslateInfo tInfo = TranslateCtx.ctx().getTranslateInfo(jstType);

    String typeName = name.getQualifier().getFullyQualifiedName();
    String fullName = tInfo.getImported(typeName);
    try {
      Class<?> toClass = Class.forName(fullName);
      Field field = toClass.getField(name.getName().toString());
      return (String) field.get(toClass);
    } catch (SecurityException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (NoSuchFieldException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IllegalArgumentException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    return null;
  }
  //
  // Satisfy IAnnoProcessor
  //
  @Override
  public CustomInfo process(final ASTNode astNode, final JstType jstType) {

    List<Annotation> annos = getAnnotations(astNode);
    if (annos.isEmpty()) {
      return null;
    }

    CustomInfo cInfo = new CustomInfo();
    boolean forceFullyQualified = false;

    String annoName;
    for (Annotation anno : annos) {

      annoName = anno.getTypeName().toString();

      if (annoName.equals(AExclude.class.getSimpleName())) {
        cInfo.setAttr(CustomAttr.EXCLUDED);
      } else if (annoName.equals(AJavaOnly.class.getSimpleName())) {
        cInfo.setAttr(CustomAttr.JAVA_ONLY);
      } else if (annoName.equals(AJsProxy.class.getSimpleName())) {
        cInfo.setAttr(CustomAttr.JS_PROXY);
      } else if (annoName.equals(AMappedToJS.class.getSimpleName())) {
        cInfo.setAttr(CustomAttr.MAPPED_TO_JS);
        process(astNode, jstType, anno, cInfo);
      } else if (annoName.equals(AMappedToVJO.class.getSimpleName())) {
        cInfo.setAttr(CustomAttr.MAPPED_TO_VJO);
        process(astNode, jstType, anno, cInfo);
      } else if (annoName.equals(ACustomizedAs.class.getSimpleName())) {
        MemberValuePair mvPair;
        for (Object pair : ((NormalAnnotation) anno).values()) {
          if (pair instanceof MemberValuePair) {
            mvPair = (MemberValuePair) pair;
            if (mvPair.getName().toString().equals("type")) {
              Type asAstType = ((TypeLiteral) mvPair.getValue()).getType();
              IJstType asJstType =
                  TranslateCtx.ctx()
                      .getProvider()
                      .getDataTypeTranslator()
                      .processType(asAstType, jstType);
              cInfo.setAsType(asJstType);
            } else if (mvPair.getName().toString().equals("name")) {
              cInfo.setAsName(((StringLiteral) mvPair.getValue()).getLiteralValue());
            }
          }
        }
      } else if (annoName.equals(ARename.class.getSimpleName())) {
        process(astNode, jstType, anno, cInfo);
      } else if (annoName.equals(AProperty.class.getSimpleName())) {
        if (astNode instanceof MethodDeclaration) {
          genMetaForPty((MethodDeclaration) astNode, jstType, anno);
        }
      } else if (annoName.equals(AForceFullyQualified.class.getSimpleName())) {
        if (astNode instanceof MethodDeclaration) {
          forceFullyQualified = TranslateHelper.isStatic(((MethodDeclaration) astNode).modifiers());
        }
      }
    }

    if (forceFullyQualified) {
      cInfo.setForceFullyQualify(true);
    }

    return cInfo;
  }