Example #1
0
  /** Returns <code>true</code> if the rhs array type can be assigned to the lhs array type. */
  private static boolean areArraysAssignable(JArrayType lhsType, JArrayType rhsType) {
    // areClassTypesAssignable should prevent us from getting here if the types
    // are referentially equal.
    assert (lhsType != rhsType);

    JType lhsComponentType = lhsType.getComponentType();
    JType rhsComponentType = rhsType.getComponentType();

    if (lhsComponentType.isPrimitive() != null || rhsComponentType.isPrimitive() != null) {
      /*
       * Arrays are referentially stable so there will only be one int[] no
       * matter how many times it is referenced in the code. So, if either
       * component type is a primitive then we know that we are not assignable.
       */
      return false;
    }

    assert (lhsComponentType instanceof JClassType);
    assert (rhsComponentType instanceof JClassType);

    JClassType thisComponentClass = (JClassType) lhsComponentType;
    JClassType subtypeComponentClass = (JClassType) rhsComponentType;

    return areClassTypesAssignable(thisComponentClass, subtypeComponentClass);
  }
  protected String toFormStringExpression(JParameter argument, Style classStyle)
      throws UnableToCompleteException {
    JType type = argument.getType();
    String expr = argument.getName();

    if (type.isPrimitive() != null) {
      return "\"\"+" + expr;
    }
    if (STRING_TYPE == type) {
      return expr;
    }
    if (type.isClass() != null && isOverlayArrayType(type.isClass())) {
      return "(new " + JSON_ARRAY_CLASS + "(" + expr + ")).toString()";
    }
    if (type.isClass() != null && OVERLAY_VALUE_TYPE.isAssignableFrom(type.isClass())) {
      return "(new " + JSON_OBJECT_CLASS + "(" + expr + ")).toString()";
    }
    if (type.getQualifiedBinaryName().startsWith("java.lang.")) {
      return String.format("(%s != null ? %s.toString() : null)", expr, expr);
    }

    Json jsonAnnotation = argument.getAnnotation(Json.class);
    final Style style = jsonAnnotation != null ? jsonAnnotation.style() : classStyle;

    return locator.encodeExpression(type, expr, style) + ".toString()";
  }
Example #3
0
  /**
   * Adds the given parameter to this JConstructor's list of parameters.
   *
   * @param parameter The parameter to add to the this JConstructor's list of parameters.
   */
  public void addParameter(final JParameter parameter) {
    if (parameter == null) {
      return;
    }

    // -- check current params
    if (_params.get(parameter.getName()) != null) {
      StringBuilder err = new StringBuilder(64);
      err.append("A parameter already exists for the constructor, ");
      err.append(this._declaringClass.getName());
      err.append(", with the name: ");
      err.append(parameter.getName());
      throw new IllegalArgumentException(err.toString());
    }

    _params.put(parameter.getName(), parameter);

    // -- be considerate and add the class name to the
    // -- declaring class's list of imports
    if (_declaringClass != null) {
      JType jType = parameter.getType();
      if (!jType.isPrimitive()) {
        _declaringClass.addImport(jType.getName());
      }
    }
  }
Example #4
0
  public void addMethod(JMethod jMethod) throws IllegalArgumentException {
    if (jMethod == null) {
      throw new IllegalArgumentException("Class methods cannot be null");
    }

    // -- check method name and signatures *add later*

    // -- keep method list sorted for esthetics when printing
    // -- START SORT :-)
    boolean added = false;
    short modifierVal = 0;
    JModifiers modifiers = jMethod.getModifiers();

    for (int i = 0; i < methods.size(); i++) {
      JMethod tmp = (JMethod) methods.elementAt(i);
      // -- first compare modifiers
      if (tmp.getModifiers().isPrivate()) {
        if (!modifiers.isPrivate()) {
          methods.insertElementAt(jMethod, i);
          added = true;
          break;
        }
      }
      // -- compare names
      if (jMethod.getName().compareTo(tmp.getName()) < 0) {
        methods.insertElementAt(jMethod, i);
        added = true;
        break;
      }
    }
    // -- END SORT
    if (!added) methods.addElement(jMethod);

    // -- check parameter packages to make sure we have them
    // -- in our import list

    String[] pkgNames = jMethod.getParameterClassNames();
    for (int i = 0; i < pkgNames.length; i++) {
      addImport(pkgNames[i]);
    }
    // -- check return type to make sure it's included in the
    // -- import list
    JType jType = jMethod.getReturnType();
    if (jType != null) {
      while (jType.isArray()) jType = jType.getComponentType();

      if (!jType.isPrimitive()) addImport(((JClass) jType).getName());
    }
    // -- check exceptions
    JClass[] exceptions = jMethod.getExceptions();
    for (int i = 0; i < exceptions.length; i++) {
      addImport(exceptions[i].getName());
    }
  } // -- addMethod
  protected String toStringExpression(JType type, String expr) {
    if (type.isPrimitive() != null) {
      return "\"\"+" + expr;
    }
    if (STRING_TYPE == type) {
      return expr;
    }
    if (type.isClass() != null && isOverlayArrayType(type.isClass())) {
      return "(new " + JSON_ARRAY_CLASS + "(" + expr + ")).toString()";
    }
    if (type.isClass() != null && OVERLAY_VALUE_TYPE.isAssignableFrom(type.isClass())) {
      return "(new " + JSON_OBJECT_CLASS + "(" + expr + ")).toString()";
    }

    return String.format("(%s != null ? %s.toString() : null)", expr, expr);
  }
Example #6
0
  /**
   * Adds the given JMethodSignature to this JClass
   *
   * @param jMethodSig the JMethodSignature to add.
   * @throws java.lang.IllegalArgumentException when the given JMethodSignature conflicts with an
   *     existing method signature.
   */
  public void addMethod(JMethodSignature jMethodSig) throws IllegalArgumentException {
    if (jMethodSig == null) {
      String err = "The JMethodSignature cannot be null.";
      throw new IllegalArgumentException(err);
    }

    // -- check method name and signatures *add later*

    // -- keep method list sorted for esthetics when printing
    // -- START SORT :-)
    boolean added = false;
    //        short modifierVal = 0;
    JModifiers modifiers = jMethodSig.getModifiers();
    for (int i = 0; i < methods.size(); i++) {
      JMethodSignature tmp = (JMethodSignature) methods.elementAt(i);
      // -- first compare modifiers
      if (tmp.getModifiers().isProtected()) {
        if (!modifiers.isProtected()) {
          methods.insertElementAt(jMethodSig, i);
          added = true;
          break;
        }
      }
      // -- compare names
      if (jMethodSig.getName().compareTo(tmp.getName()) < 0) {
        methods.insertElementAt(jMethodSig, i);
        added = true;
        break;
      }
    }
    // -- END SORT
    if (!added) methods.addElement(jMethodSig);

    // -- check return type to make sure it's included in the
    // -- import list
    JType jType = jMethodSig.getReturnType();
    if (jType != null) {
      while (jType.isArray()) jType = jType.getComponentType();

      if (!jType.isPrimitive()) addImport(jType.getName());
    }
    // -- check exceptions
    JClass[] exceptions = jMethodSig.getExceptions();
    for (JClass exception : exceptions) {
      addImport(exception.getName());
    }
  } // -- addMethod
Example #7
0
  public void addMember(JMember jMember) throws IllegalArgumentException {
    if (jMember == null) {
      throw new IllegalArgumentException("Class members cannot be null");
    }
    if (members.get(jMember.getName()) != null) {
      String err = "duplicate name found: " + jMember.getName();
      throw new IllegalArgumentException(err);
    }
    members.put(jMember.getName(), jMember);

    // if member is of a type not imported by this class
    // then add import
    JType type = jMember.getType();
    while (type.isArray()) type = type.getComponentType();
    if (!type.isPrimitive()) {
      addImport(((JClass) type).getName());
    }
  } // -- addMember