Exemplo n.º 1
0
 public String toJavaCode(final TypeReference typeref) {
   StringBuilderBasedAppendable _xifexpression = null;
   boolean _notEquals = (!Objects.equal(this.importManager, null));
   if (_notEquals) {
     StringBuilderBasedAppendable _stringBuilderBasedAppendable =
         new StringBuilderBasedAppendable(this.importManager);
     _xifexpression = _stringBuilderBasedAppendable;
   } else {
     StringBuilderBasedAppendable _stringBuilderBasedAppendable_1 =
         new StringBuilderBasedAppendable();
     _xifexpression = _stringBuilderBasedAppendable_1;
   }
   final StringBuilderBasedAppendable appendable = _xifexpression;
   final LightweightTypeReference typeRef =
       this.compilationUnit.toLightweightTypeReference(typeref);
   appendable.append(typeRef);
   return appendable.toString();
 }
  public CharSequence apply(ImportManager importManager) {
    JvmOperation cacheMethod =
        (JvmOperation)
            logicalContainerProvider.getLogicalContainer(createExtensionInfo.getCreateExpression());
    StringBuilderBasedAppendable appendable = new StringBuilderBasedAppendable(importManager);
    JvmDeclaredType containerType = cacheMethod.getDeclaringType();
    JvmTypeReference listType = typeReferences.getTypeForName(ArrayList.class, containerType);
    JvmTypeReference collectonLiterals =
        typeReferences.getTypeForName(CollectionLiterals.class, containerType);
    String cacheVarName = cacheField.getSimpleName();
    String cacheKeyVarName = appendable.declareVariable("CacheKey", "_cacheKey");
    appendable.append("final ");
    typeReferenceSerializer.serialize(listType, containerType, appendable);
    appendable.append(cacheKeyVarName).append(" = ");
    typeReferenceSerializer.serialize(collectonLiterals, containerType, appendable);
    appendable.append(".newArrayList(");
    EList<JvmFormalParameter> list = cacheMethod.getParameters();
    for (Iterator<JvmFormalParameter> iterator = list.iterator(); iterator.hasNext(); ) {
      JvmFormalParameter jvmFormalParameter = iterator.next();
      appendable.append(getVarName(jvmFormalParameter));
      if (iterator.hasNext()) {
        appendable.append(", ");
      }
    }
    appendable.append(");");
    // declare result variable
    JvmTypeReference returnType = typeProvider.getType(createExtensionInfo.getCreateExpression());
    appendable.append("\nfinal ");
    typeReferenceSerializer.serialize(returnType, containerType, appendable);
    String resultVarName = "_result";
    appendable.append(" ").append(resultVarName).append(";");
    // open synchronize block
    appendable.append("\nsynchronized (").append(cacheVarName).append(") {");
    appendable.increaseIndentation();
    // if the cache contains the key return the previously created object.
    appendable
        .append("\nif (")
        .append(cacheVarName)
        .append(".containsKey(")
        .append(cacheKeyVarName)
        .append(")) {");
    appendable.increaseIndentation();
    appendable
        .append("\nreturn ")
        .append(cacheVarName)
        .append(".get(")
        .append(cacheKeyVarName)
        .append(");");
    appendable.decreaseIndentation().append("\n}");
    // execute the creation
    compiler.toJavaStatement(createExtensionInfo.getCreateExpression(), appendable, true);
    appendable.append("\n");
    appendable.append(resultVarName).append(" = ");
    compiler.toJavaExpression(createExtensionInfo.getCreateExpression(), appendable);
    appendable.append(";");

    // store the newly created object in the cache
    appendable
        .append("\n")
        .append(cacheVarName)
        .append(".put(")
        .append(cacheKeyVarName)
        .append(", ")
        .append(resultVarName)
        .append(");");

    // close synchronize block
    appendable.decreaseIndentation();
    appendable.append("\n}");
    appendable
        .append("\n")
        .append(initializerMethod.getSimpleName())
        .append("(")
        .append(resultVarName);
    for (JvmFormalParameter parameter : cacheMethod.getParameters()) {
      appendable.append(", ").append(parameter.getName());
    }
    appendable.append(");");
    // return the result
    appendable.append("\nreturn ");
    appendable.append(resultVarName).append(";");
    return appendable.toString();
  }