예제 #1
0
 public static org.lgna.project.ast.JavaField getEnumConstantFieldIfOneAndOnly(
     org.lgna.project.ast.AbstractType<?, ?, ?> type) {
   org.lgna.project.ast.JavaField rv = null;
   if (type instanceof org.lgna.project.ast.JavaType) {
     org.lgna.project.ast.JavaType javaType = (org.lgna.project.ast.JavaType) type;
     if (type.isAssignableTo(Enum.class)) {
       Class<Enum<?>> cls = (Class<Enum<?>>) javaType.getClassReflectionProxy().getReification();
       Enum<?>[] constants = cls.getEnumConstants();
       if (constants.length == 1) {
         Enum<?> constant = constants[0];
         rv = org.lgna.project.ast.JavaField.getInstance(constant.getClass(), constant.name());
       }
     }
   }
   return rv;
 }
예제 #2
0
  private static org.lgna.project.ast.NamedUserType getNamedUserTypeFor(
      org.lgna.project.ast.AbstractType<?, ?, ?> ancestorType,
      org.lgna.project.ast.AbstractType<?, ?, ?>[] argumentTypes,
      int i,
      org.lgna.project.ast.AbstractField argumentField) {
    org.lgna.project.ast.AbstractType<?, ?, ?> superType;
    final int LAST_INDEX = argumentTypes.length - 1;
    if (i < LAST_INDEX) {
      superType = getNamedUserTypeFor(ancestorType, argumentTypes, i + 1, null);
    } else {
      superType = ancestorType;
    }
    edu.cmu.cs.dennisc.pattern.Criterion<org.lgna.project.ast.NamedUserType> criterion;
    if (argumentField != null) {
      criterion = new ExtendsTypeWithSuperArgumentFieldCriterion(superType, argumentField);
    } else {
      criterion = new ExtendsTypeWithConstructorParameterTypeCriterion(superType, argumentTypes[i]);
    }

    //		org.alice.ide.IDE ide = org.alice.ide.IDE.getActiveInstance();
    //		if( ide != null ) {
    //			org.alice.ide.ProjectDocument projectDocument = ide.getDocument();
    //			if( projectDocument != null ) {
    //				org.alice.ide.type.TypeCache typeCache = projectDocument.getTypeCache();
    //				typeCache.getNamedUserTypeFor( ancestorType, argumentTypes, i, argumentField );
    //			}
    //		}

    org.lgna.project.Project project = org.alice.ide.ProjectStack.peekProject();
    if (project != null) {
      java.util.Set<org.lgna.project.ast.NamedUserType> existingTypes = project.getNamedUserTypes();
      for (org.lgna.project.ast.NamedUserType existingType : existingTypes) {
        if (criterion.accept(existingType)) {
          return existingType;
        }
      }
    }
    org.lgna.project.ast.Expression[] expressions;
    if (argumentField != null) {
      expressions =
          new org.lgna.project.ast.Expression[] {
            new org.lgna.project.ast.FieldAccess(
                new org.lgna.project.ast.TypeExpression(argumentField.getDeclaringType()),
                argumentField)
          };
    } else {
      expressions = USE_PARAMETER_ACCESSES_AS_ARGUMENTS_TO_SUPER;
    }
    String name = createClassNameFromResourceType(argumentTypes[i]);

    org.lgna.project.ast.NamedUserType rv =
        createTypeFor(
            superType,
            name,
            new org.lgna.project.ast.AbstractType[] {argumentTypes[i]},
            expressions);
    if (argumentTypes[i] instanceof org.lgna.project.ast.JavaType) {
      org.lgna.project.ast.JavaType javaArgumentTypeI =
          (org.lgna.project.ast.JavaType) argumentTypes[i];
      Class<?> cls = javaArgumentTypeI.getClassReflectionProxy().getReification();
      if (edu.cmu.cs.dennisc.java.lang.reflect.ReflectionUtilities.isFinal(cls)) {
        boolean isSetResourceMethodDesired;
        if (cls.isEnum()) {
          isSetResourceMethodDesired = cls.getEnumConstants().length > 1;
        } else {
          isSetResourceMethodDesired = true;
        }
        if (isSetResourceMethodDesired) {
          String simpleClsName = cls.getSimpleName();
          org.lgna.project.ast.UserMethod setResourceMethod = new org.lgna.project.ast.UserMethod();
          setResourceMethod.managementLevel.setValue(
              org.lgna.project.ast.ManagementLevel.GENERATED);
          setResourceMethod.name.setValue("set" + simpleClsName);
          setResourceMethod.returnType.setValue(org.lgna.project.ast.JavaType.VOID_TYPE);
          org.lgna.project.ast.BlockStatement body = new org.lgna.project.ast.BlockStatement();
          setResourceMethod.body.setValue(body);

          StringBuilder parameterNameSB = new StringBuilder();
          parameterNameSB.append(Character.toLowerCase(simpleClsName.charAt(0)));
          parameterNameSB.append(simpleClsName.substring(1));
          org.lgna.project.ast.UserParameter parameter = new org.lgna.project.ast.UserParameter();
          parameter.name.setValue(parameterNameSB.toString());
          parameter.valueType.setValue(javaArgumentTypeI);

          setResourceMethod.requiredParameters.add(parameter);

          body.statements.add(
              org.lgna.project.ast.AstUtilities.createMethodInvocationStatement(
                  new org.lgna.project.ast.ThisExpression(),
                  SET_JOINTED_MODEL_RESOURCE_METHOD,
                  new org.lgna.project.ast.ParameterAccess(parameter)));

          setResourceMethod.isSignatureLocked.setValue(true);
          rv.methods.add(setResourceMethod);
        }
      }
    }
    return rv;
  }