Example #1
0
  private IMethodInfo getPropertyGetMethod(IPropertyInfo pi, IJavaType ownerType) {
    JavaTypeInfo ti;
    if (!(pi.getOwnersType() instanceof IJavaType)) {
      return null;
    }
    if (ownerType.getTypeInfo() instanceof JavaTypeInfo) {
      ti = (JavaTypeInfo) ownerType.getTypeInfo();
    } else {
      throw new IllegalArgumentException(ownerType + " is not a recognized Java type");
    }

    IType propType = pi.getFeatureType();

    String strAccessor = "get" + pi.getDisplayName();
    IMethodInfo mi = ti.getMethod(ownerType, strAccessor);
    if (mi == null || mi.getReturnType() != propType) {
      strAccessor = "is" + pi.getDisplayName();
      mi = ti.getMethod(ownerType, strAccessor);
    }
    if (mi != null && mi.getReturnType() == propType) {
      return mi;
    }

    return null;
  }
Example #2
0
  private void genMemberMethod(StringBuilder sb, IMethodInfo mi, IJavaType type) {
    if (!canExtendMethod(mi)) {
      return;
    }

    StringBuilder sbModifiers = buildModifiers(mi);
    if (mi.getDescription() != null) {
      sb.append("/** ").append(mi.getDescription()).append(" */");
    }
    sb.append("  ")
        .append(sbModifiers)
        .append("function ")
        .append(mi.getDisplayName())
        .append(TypeInfoUtil.getTypeVarList(mi))
        .append("(");
    IParameterInfo[] params = getGenericParameters(mi);
    for (int i = 0; i < params.length; i++) {
      IParameterInfo pi = params[i];
      sb.append(' ')
          .append("p")
          .append(i)
          .append(" : ")
          .append(pi.getFeatureType().getName())
          .append(i < params.length - 1 ? ',' : ' ');
    }
    sb.append(") : ").append(getGenericReturnType(mi).getName()).append("\n");
    if (!mi.isAbstract()) {
      generateStub(sb, mi.getReturnType());
    }
  }
Example #3
0
  private void genMethodImpl(StringBuilder sb, IMethodInfo mi, IJavaType type) {
    if (mi.isPrivate() || mi.isInternal()) {
      return;
    }

    if (mi.isStatic() && mi.getDisplayName().indexOf('$') < 0) {
      genStaticMethod(sb, mi, type);
    } else {
      genMemberMethod(sb, mi, type);
    }
  }
Example #4
0
  private void genStaticMethod(StringBuilder sb, IMethodInfo mi, IJavaType type) {
    if (!(mi instanceof JavaMethodInfo)) {
      // It is possible that a methodinfo on a java type originates outside of java.
      // E.g., enhancement methods. Only static JAVA members should be reflected in
      // the proxy.
      return;
    }

    if (isPropertyMethod(mi)) {
      // We favor properties over methods -- gotta pick one
      return;
    }

    StringBuilder sbModifiers = appendVisibilityModifier(mi);
    if (mi.getDescription() != null) {
      sb.append("/** ").append(mi.getDescription()).append(" */");
    }
    sb.append("  ")
        .append(sbModifiers)
        .append("static function ")
        .append(mi.getDisplayName())
        .append(TypeInfoUtil.getTypeVarList(mi))
        .append("(");
    IParameterInfo[] params = getGenericParameters(mi);
    for (int i = 0; i < params.length; i++) {
      IParameterInfo pi = params[i];
      sb.append(' ')
          .append("p")
          .append(i)
          .append(" : ")
          .append(pi.getFeatureType().getName())
          .append(i < params.length - 1 ? ',' : ' ');
    }
    sb.append(") : ")
        .append(getGenericReturnType(mi).getName())
        .append("\n")
        .append("{\n")
        .append((mi.getReturnType() == GosuParserTypes.NULL_TYPE() ? "" : "    return "))
        .append(type.getName())
        .append('.')
        .append(mi.getDisplayName())
        .append(TypeInfoUtil.getTypeVarListNoBounds(mi))
        .append("(");
    for (int i = 0; i < params.length; i++) {
      sb.append(' ').append("p").append(i).append(i < params.length - 1 ? ',' : ' ');
    }
    sb.append(");\n");
    sb.append("}\n");
  }
  public void setMethodDescriptor(IMethodInfo md) {
    _md = md;

    if (md != null) {
      IType type = JavaTypes.IGOSU_OBJECT();
      if (_md.getOwnersType() == IGosuClassInternal.Util.getGosuClassFrom(type)) {
        _md =
            type.getTypeInfo()
                .getMethod(
                    _md.getDisplayName(),
                    ((FunctionType) ((IGosuMethodInfo) _md).getDfs().getType())
                        .getParameterTypes());
      }
    }
  }
  public void testInternalAPIHidesMembers() {
    IType scriptableTestExampleType = TypeSystem.get(ScriptableTestExample.class);

    IPropertyInfo exampleProperty =
        scriptableTestExampleType.getTypeInfo().getProperty("ExampleProperty");
    assertTrue(exampleProperty.isVisible(ScriptabilityModifiers.SCRIPTABLE));
    IPropertyInfo internalProperty =
        scriptableTestExampleType.getTypeInfo().getProperty("InternalProperty");
    assertFalse(internalProperty.isVisible(ScriptabilityModifiers.SCRIPTABLE));

    IMethodInfo exampleMethod = scriptableTestExampleType.getTypeInfo().getMethod("exampleMethod");
    assertTrue(exampleMethod.isVisible(ScriptabilityModifiers.SCRIPTABLE));
    IMethodInfo internalMethod =
        scriptableTestExampleType.getTypeInfo().getMethod("internalMethod");
    assertFalse(internalMethod.isVisible(ScriptabilityModifiers.SCRIPTABLE));
  }
Example #7
0
 private boolean isPropertyGetter(IMethodInfo mi, String strPrefix) {
   String strMethod = mi.getDisplayName();
   if (strMethod.startsWith(strPrefix) && mi.getParameters().length == 0) {
     String strProp = strMethod.substring(strPrefix.length());
     if (strProp.length() > 0 && Character.isUpperCase(strProp.charAt(0))) {
       ITypeInfo ti = (ITypeInfo) mi.getContainer();
       IPropertyInfo pi =
           ti instanceof IRelativeTypeInfo
               ? ((IRelativeTypeInfo) ti).getProperty(mi.getOwnersType(), strProp)
               : ti.getProperty(strProp);
       if (pi != null && getGenericType(pi).getName().equals(getGenericReturnType(mi).getName())) {
         return !Keyword.isReserved(pi.getName()) || Keyword.isReservedValue(pi.getName());
       }
     }
   }
   return false;
 }
Example #8
0
  private boolean canExtendMethod(IMethodInfo mi) {
    if (!(mi instanceof JavaMethodInfo)) {
      // It is possible that a methodinfo on a java type originates outside of java.
      // E.g., enhancement methods. Gosu does not support extending these.
      return false;
    }

    if (isPropertyMethod(mi)) {
      // We favor properties over methods -- gotta pick one
      return false;
    }

    int iMethodModifiers = ((IJavaMethodInfo) mi).getModifiers();
    return // !java.lang.reflect.Modifier.isFinal( iMethodModifiers ) &&
    !java.lang.reflect.Modifier.isNative(iMethodModifiers)
        &&
        // See GosuClassInstanceFactory.genSuperClassMembers() (we don't allow finalizers)
        !mi.getDisplayName().equals("finalize")
        && mi.getDisplayName().indexOf('$') < 0;
  }
Example #9
0
 private void genInterfaceMethodDecl(StringBuilder sb, IMethodInfo mi) {
   if (!(mi instanceof JavaMethodInfo)) {
     // It is possible that a methodinfo on a java type originates outside of java.
     // E.g., enhancement methods. Gosu does not support extending these.
     return;
   }
   if (isPropertyMethod(mi)) {
     return;
   }
   if (mi.getDisplayName().equals("hashCode")
       || mi.getDisplayName().equals("equals")
       || mi.getDisplayName().equals("toString")) {
     if (!mi.getOwnersType().getName().equals(IGosuObject.class.getName())) {
       return;
     }
   }
   if (mi.getDescription() != null) {
     sb.append("/** ").append(mi.getDescription()).append(" */");
   }
   sb.append("  function ")
       .append(mi.getDisplayName())
       .append(TypeInfoUtil.getTypeVarList(mi))
       .append("(");
   IParameterInfo[] params = getGenericParameters(mi);
   for (int i = 0; i < params.length; i++) {
     IParameterInfo pi = params[i];
     sb.append(' ').append("p").append(i).append(" : ").append(pi.getFeatureType().getName());
     sb.append(i < params.length - 1 ? ',' : ' ');
   }
   sb.append(") : ").append(getGenericReturnType(mi).getName()).append(";\n");
 }
Example #10
0
 private boolean isPropertySetter(IMethodInfo mi) {
   String strMethod = mi.getDisplayName();
   if (strMethod.startsWith("set")
       && strMethod.length() > 3
       && mi.getParameters().length == 1
       && mi.getReturnType() == JavaTypes.pVOID()) {
     String strProp = strMethod.substring("set".length());
     if (Character.isUpperCase(strProp.charAt(0))) {
       ITypeInfo ti = (ITypeInfo) mi.getContainer();
       IPropertyInfo pi =
           ti instanceof IRelativeTypeInfo
               ? ((IRelativeTypeInfo) ti).getProperty(mi.getOwnersType(), strProp)
               : ti.getProperty(strProp);
       if (pi != null
           && pi.isReadable()
           && getGenericType(pi)
               .getName()
               .equals(getGenericParameters(mi)[0].getFeatureType().getName())) {
         return !Keyword.isReserved(pi.getName()) || Keyword.isReservedValue(pi.getName());
       }
     }
   }
   return false;
 }
Example #11
0
 private IParameterInfo[] getGenericParameters(IMethodInfo mi) {
   return (mi instanceof JavaMethodInfo)
       ? ((JavaMethodInfo) mi).getGenericParameters()
       : mi.getParameters();
 }
Example #12
0
 private IType getGenericReturnType(IMethodInfo mi) {
   return (mi instanceof JavaMethodInfo)
       ? ((JavaMethodInfo) mi).getGenericReturnType()
       : mi.getReturnType();
 }
Example #13
0
  private void genMemberProperty(IPropertyInfo pi, StringBuilder sb, IJavaType type) {
    if (pi.isStatic()) {
      return;
    }

    if (!(pi instanceof JavaBaseFeatureInfo)) {
      // It is possible that a propertyinfo on a java type originates outside of java.
      // E.g., enhancement properties. Gosu does not support extending these.
      return;
    }

    if (Keyword.isReserved(pi.getName()) && !Keyword.isReservedValue(pi.getName())) {
      // Sorry these won't compile
      // ## todo: handle them reflectively?
      return;
    }

    if (pi instanceof JavaFieldPropertyInfo) {
      StringBuilder sbModifiers = appendFieldVisibilityModifier(pi);
      sb.append("  ")
          .append(sbModifiers)
          .append(" var ")
          .append(pi.getName())
          .append(" : ")
          .append(getGenericType(pi).getName())
          .append("\n");
    } else {
      IMethodInfo mi = getPropertyGetMethod(pi, type);
      boolean bFinal = false;
      if (mi != null) {
        int iMethodModifiers = ((IJavaMethodInfo) mi).getModifiers();
        bFinal = java.lang.reflect.Modifier.isFinal(iMethodModifiers);
      }

      if (mi != null && !bFinal) {
        if (mi.getDescription() != null) {
          sb.append("/** ").append(mi.getDescription()).append(" */");
        }
        StringBuilder sbModifiers = buildModifiers(mi);
        sb.append("  ")
            .append(sbModifiers)
            .append("property get ")
            .append(pi.getName())
            .append("() : ")
            .append(getGenericType(pi).getName())
            .append("\n");
        if (!mi.isAbstract()) {
          generateStub(sb, mi.getReturnType());
        }
      } else {
        String strJavaClassName = type.getName();
        StringBuilder sbModifiers;
        boolean bAbstact = false;
        if (bFinal) {
          bAbstact = mi.isAbstract();
          sbModifiers = buildModifiers(mi);
        } else {
          sbModifiers = appendVisibilityModifier(pi);
        }
        sb.append("  ")
            .append(sbModifiers)
            .append("property get ")
            .append(pi.getName())
            .append("() : ")
            .append(getGenericType(pi).getName())
            .append("\n");
        if (!bAbstact) {
          generateStub(sb, getGenericType(pi));
        }
      }

      mi = getPropertySetMethod(pi, type);
      bFinal = false;
      if (mi != null) {
        int iMethodModifiers = ((IJavaMethodInfo) mi).getModifiers();
        bFinal = java.lang.reflect.Modifier.isFinal(iMethodModifiers);
      }

      if (mi != null && !bFinal) {
        StringBuilder sbModifiers = buildModifiers(mi);
        if (pi.isWritable(pi.getOwnersType())) {
          sb.append("  ")
              .append(sbModifiers)
              .append("property set ")
              .append(pi.getName())
              .append("( _proxy_arg_value : ")
              .append(getGenericType(pi).getName())
              .append(" )\n");
          if (!mi.isAbstract()) {
            generateStub(sb, JavaTypes.pVOID());
          }
        }
      } else {
        if (pi.isWritable(type.getEnclosingType() != null ? null : pi.getOwnersType())) {
          String strJavaClassName = type.getName();
          StringBuilder sbModifiers;
          boolean bAbstact = false;
          if (bFinal) {
            bAbstact = mi.isAbstract();
            sbModifiers = buildModifiers(mi);
          } else {
            sbModifiers = appendVisibilityModifier(pi);
          }
          sb.append("  ")
              .append(sbModifiers)
              .append("property set ")
              .append(pi.getName())
              .append("( _proxy_arg_value : ")
              .append(getGenericType(pi).getName())
              .append(" )\n");
          if (!bAbstact) {
            generateStub(sb, JavaTypes.pVOID());
          }
        }
      }
    }
  }
Example #14
0
 /**
  * A convenience method for running this suite from a main method. Subclasses of Suite can create
  * a main method like so:
  *
  * <pre>
  *   public static void main(String[] args) {
  *     System.exit( new GosuSuite().runSuite() ? 0 : 1 );
  *   }
  * </pre>
  *
  * @return a boolean saying if all tests passed
  */
 public final boolean runViaStaticSuiteMethod() {
   IType iType = TypeSystem.getTypeFromObject(this);
   IMethodInfo method = iType.getTypeInfo().getMethod("suite");
   Test test = (Test) method.getCallHandler().handleCall(null);
   return runImpl(test).wasSuccessful();
 }