Ejemplo n.º 1
0
 public void action(Visitor visitor) {
   Iterator it = Elements.iterator();
   Element element = null;
   while (it.hasNext()) {
     element = (Element) it.next();
     element.accept(visitor);
   }
 }
  public void computeVariablesDependencies(Element e, final Set<Identifier> names) {
    e.accept(
        new Scanner() {

          @Override
          public void visitVariableRef(VariableRef variableRef) {
            names.add(variableRef.getName());
          }
        });
  }
Ejemplo n.º 3
0
  private String asString(Element e) {
    if (e == null) return "[elt:null]";
    return e.accept(
        new SimpleElementVisitor<String, Void>() {
          @Override
          public String defaultAction(Element e, Void ignore) {
            return "[elt:" + e.getKind() + " " + e.toString() + "]";
          }

          @Override
          public String visitPackage(PackageElement e, Void ignore) {
            return "pkg " + e.getQualifiedName();
          }

          @Override
          public String visitType(TypeElement e, Void ignore) {
            StringBuilder sb = new StringBuilder();
            if (e.getEnclosedElements().isEmpty()) sb.append("empty ");
            ElementKind ek = e.getKind();
            switch (ek) {
              case CLASS:
                sb.append("clss");
                break;
              case INTERFACE:
                sb.append("intf");
                break;
              default:
                sb.append(ek);
                break;
            }
            sb.append(" ");
            Element encl = e.getEnclosingElement();
            if (!isUnnamedPackage(encl) && encl.asType().getKind() != TypeKind.NONE) {
              sb.append("(");
              sb.append(asString(encl));
              sb.append(")");
              sb.append(".");
            }
            sb.append(e.getSimpleName());
            if (e.asType().getKind() == TypeKind.ERROR) sb.append("!");
            return sb.toString();
          }
        },
        null);
  }
  public void convertConstants(
      String library,
      List<Define> defines,
      Element sourcesRoot,
      final Signatures signatures,
      final DeclarationsHolder out,
      final Identifier libraryClassName) {
    // final List<Define> defines = new ArrayList<Define>();
    final Map<String, String> constants = Result.getMap(result.stringConstants, library);
    //
    sourcesRoot.accept(
        new Scanner() {
          //			@Override
          //			public void visitDefine(Define define) {
          //				super.visitDefine(define);
          //				if (elementsFilter.accept(define))
          //					defines.add(define);
          //			}
          @Override
          public void visitVariablesDeclaration(VariablesDeclaration v) {
            super.visitVariablesDeclaration(v);
            // if (!elementsFilter.accept(v))
            //	return;

            if (v.findParentOfType(Struct.class) != null) return;

            if (v.getValueType() instanceof FunctionSignature) return;

            for (Declarator decl : v.getDeclarators()) {
              if (!(decl instanceof DirectDeclarator))
                continue; // TODO provide a mapping of exported values

              TypeRef mutatedType = (TypeRef) decl.mutateTypeKeepingParent(v.getValueType());
              if (mutatedType == null
                  || !mutatedType.getModifiers().contains(ModifierType.Const)
                  || mutatedType.getModifiers().contains(ModifierType.Extern)
                  || decl.getDefaultValue() == null) continue;

              // TypeRef type = v.getValueType();
              String name = decl.resolveName();

              JavaPrim prim = result.typeConverter.getPrimitive(mutatedType, libraryClassName);
              if (prim == null) {
                if (mutatedType.toString().contains("NSString")) {
                  String value = constants.get(name);
                  if (value != null) outputNSString(name, value, out, signatures, v, decl);
                }
                continue;
              }

              try {

                // DirectDeclarator dd = (DirectDeclarator)decl;
                Pair<Expression, TypeRef> val =
                    result.typeConverter.convertExpressionToJava(
                        decl.getDefaultValue(), libraryClassName, true);

                if (!signatures.addVariable(name)) continue;

                // TODO
                TypeRef tr =
                    prim == JavaPrim.NativeLong || prim == JavaPrim.NativeSize
                        ? typeRef("long")
                        : primRef(prim)
                    // result.typeConverter.convertTypeToJNA(mutatedType,
                    // TypeConversion.TypeConversionMode.FieldType, libraryClassName)
                    ;
                VariablesDeclaration vd =
                    new VariablesDeclaration(tr, new DirectDeclarator(name, val.getFirst()));
                if (!result.config.noComments) {
                  vd.setCommentBefore(v.getCommentBefore());
                  vd.addToCommentBefore(decl.getCommentBefore());
                  vd.addToCommentBefore(decl.getCommentAfter());
                  vd.addToCommentBefore(v.getCommentAfter());
                }

                //                        if (result.config.runtime ==
                // JNAeratorConfig.Runtime.BridJ)
                vd.addModifiers(ModifierType.Public, ModifierType.Static, ModifierType.Final);

                out.addDeclaration(vd);
              } catch (UnsupportedConversionException e) {
                out.addDeclaration(skipDeclaration(v, e.toString()));
              }
            }
          }
        });

    if (defines != null) {
      for (Define define : reorderDefines(defines)) {
        if (define.getValue() == null) continue;

        try {
          // System.out.println("Define " + define.getName() + " = " + define.getValue());
          out.addDeclaration(
              outputConstant(
                  define.getName(),
                  define.getValue(),
                  signatures,
                  define.getValue(),
                  "define",
                  libraryClassName,
                  true,
                  false,
                  false));
        } catch (UnsupportedConversionException ex) {
          out.addDeclaration(skipDeclaration(define, ex.toString()));
        }
      }
    }
    for (Map.Entry<String, String> e : constants.entrySet()) {
      outputNSString(e.getKey(), e.getValue(), out, signatures);
    }
  }