public boolean initialize(EObject xtendMethod, IRenameElementContext context) {
   Assert.isLegal(xtendMethod instanceof XtendFunction);
   Assert.isLegal(((XtendFunction) xtendMethod).isDispatch());
   Assert.isLegal(context instanceof DispatchMethodRenameContext);
   ResourceSet resourceSet = xtendMethod.eResource().getResourceSet();
   Map<URI, IJavaElement> jvm2JavaElements =
       ((DispatchMethodRenameContext) context).getJvm2JavaElements();
   for (URI dispatchOperationURI : jvm2JavaElements.keySet()) {
     JvmOperation dispatchOperation =
         (JvmOperation) resourceSet.getEObject(dispatchOperationURI, true);
     XtendFunction xtendDispatchMethod = associations.getXtendFunction(dispatchOperation);
     if (xtendDispatchMethod != null) {
       if (equal(xtendDispatchMethod.getName(), dispatchOperation.getSimpleName())) {
         // synthetic dispatcher
         dispatchers.add(dispatchOperation);
       } else {
         // xtend dispatch method
         XtendDispatchMethodChildStrategy xtendChildStrategy = childStrategyProvider.get();
         xtendChildStrategy.initialize(xtendDispatchMethod, context);
         children.add(xtendChildStrategy);
       }
     } else {
       // a dispatch method form a Java class
       JavaDispatchMethodChildStrategy jvmChildStrategy = javaStrategyProvider.get();
       jvmChildStrategy.initialize(dispatchOperation, context);
       children.add(jvmChildStrategy);
     }
   }
   return !children.isEmpty();
 }
Exemplo n.º 2
0
 protected IScope createLocalVarScopeForJvmOperation(JvmOperation context, IScope parentScope) {
   EList<JvmFormalParameter> parameters = context.getParameters();
   List<LocalVarDescription> descriptions = newArrayList();
   for (JvmFormalParameter p : parameters) {
     if (p.getName() != null)
       descriptions.add(new LocalVarDescription(QualifiedName.create(p.getName()), p));
   }
   return new JvmFeatureScope(parentScope, "operation " + context.getSimpleName(), descriptions);
 }
Exemplo n.º 3
0
 public Method getMethod(JvmOperation operation) {
   Class<?> rawType = getRawType(operation.getDeclaringType());
   if (rawType == null) return null;
   Class<?>[] paramTypes = getParamTypes(operation);
   try {
     return rawType.getDeclaredMethod(operation.getSimpleName(), paramTypes);
   } catch (Exception e) {
     LOG.error("Can't find " + operation.getIdentifier(), e);
     return null;
   }
 }
Exemplo n.º 4
0
 @Override
 protected JvmOperation getMethodFromType(EObject context, Class<?> type, String method) {
   JvmOperation result = super.getMethodFromType(context, type, method);
   if (result != null) {
     IJavaElement found = elementFinder.findElementFor(result);
     assertEquals(IJavaElement.METHOD, found.getElementType());
     assertEquals(result.getSimpleName(), found.getElementName());
     IMethod foundMethod = (IMethod) found;
     assertEquals(result.getParameters().size(), foundMethod.getNumberOfParameters());
   }
   return result;
 }
 private static JvmOperation mockJvmOperation(String name, boolean isAbstract) {
   JvmOperation operation = mock(JvmOperation.class);
   when(operation.isAbstract()).thenReturn(isAbstract);
   when(operation.isStatic()).thenReturn(false);
   when(operation.isDefault()).thenReturn(false);
   when(operation.isDeprecated()).thenReturn(false);
   when(operation.isFinal()).thenReturn(false);
   when(operation.isSynchronized()).thenReturn(false);
   when(operation.isNative()).thenReturn(false);
   when(operation.getSimpleName()).thenReturn(name);
   when(operation.getAnnotations()).thenReturn(new BasicEList<JvmAnnotationReference>());
   return operation;
 }
 protected SignatureHashBuilder appendSignature(JvmOperation operation) {
   appendVisibility(operation.getVisibility()).append(" ");
   if (operation.isAbstract()) append("abstract ");
   if (operation.isStatic()) append("static ");
   if (operation.isFinal()) append("final ");
   appendType(operation.getReturnType())
       .appendTypeParameters(operation)
       .append(" ")
       .append(operation.getSimpleName())
       .append("(");
   for (JvmFormalParameter p : operation.getParameters()) {
     appendType(p.getParameterType());
     append(" ");
   }
   append(") ");
   for (JvmTypeReference ex : operation.getExceptions()) {
     appendType(ex).append(" ");
   }
   return this;
 }
  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();
  }
Exemplo n.º 8
0
  protected void convertFunctionType(
      final JvmTypeReference expectedType,
      final JvmTypeReference functionType,
      final ITreeAppendable appendable,
      final Later expression,
      XExpression context) {
    //		JvmTypeReference resolvedLeft = closures.getResolvedExpectedType(expectedType,
    // functionType);
    if (expectedType.getIdentifier().equals(Object.class.getName())
        || EcoreUtil.equals(expectedType.getType(), functionType.getType())
        || ((expectedType instanceof JvmSynonymTypeReference)
            && Iterables.any(
                ((JvmSynonymTypeReference) expectedType).getReferences(),
                new Predicate<JvmTypeReference>() {
                  public boolean apply(@Nullable JvmTypeReference ref) {
                    return EcoreUtil.equals(ref.getType(), functionType.getType());
                  }
                }))) {
      // same raw type but different type parameters
      // at this point we know that we are compatible so we have to convince the Java compiler about
      // that ;-)
      if (!getTypeConformanceComputer().isConformant(expectedType, functionType)) {
        // insert a cast
        appendable.append("(");
        serialize(expectedType, context, appendable);
        appendable.append(")");
      }
      expression.exec(appendable);
      return;
    }
    JvmOperation operation = closures.findImplementingOperation(expectedType, context.eResource());
    if (operation == null) {
      throw new IllegalStateException(
          "expected type " + expectedType + " not mappable from " + functionType);
    }
    JvmType declaringType =
        (expectedType instanceof JvmParameterizedTypeReference)
            ? expectedType.getType()
            : operation.getDeclaringType();
    final JvmTypeReference typeReferenceWithPlaceHolder =
        getTypeReferences().createTypeRef(declaringType);
    ITypeArgumentContext typeArgumentContext =
        contextProvider.getTypeArgumentContext(
            new TypeArgumentContextProvider.AbstractRequest() {
              @Override
              public JvmTypeReference getExpectedType() {
                return functionType;
              }

              @Override
              public JvmTypeReference getDeclaredType() {
                return typeReferenceWithPlaceHolder;
              }

              @Override
              public String toString() {
                return "TypeConvertingCompiler.convertFunctionType [expected="
                    + functionType
                    + ",declared="
                    + typeReferenceWithPlaceHolder
                    + "]";
              }
            });
    JvmTypeReference resolvedExpectedType =
        typeArgumentContext.resolve(typeReferenceWithPlaceHolder);
    appendable.append("new ");
    serialize(resolvedExpectedType, context, appendable, true, false);
    appendable.append("() {");
    appendable.increaseIndentation().increaseIndentation();
    appendable.newLine().append("public ");
    serialize(
        typeArgumentContext.resolve(operation.getReturnType()), context, appendable, true, false);
    appendable.append(" ").append(operation.getSimpleName()).append("(");
    EList<JvmFormalParameter> params = operation.getParameters();
    for (Iterator<JvmFormalParameter> iterator = params.iterator(); iterator.hasNext(); ) {
      JvmFormalParameter p = iterator.next();
      final String name = p.getName();
      serialize(
          typeArgumentContext.resolve(p.getParameterType()), context, appendable, false, false);
      appendable.append(" ").append(name);
      if (iterator.hasNext()) appendable.append(",");
    }
    appendable.append(") {");
    appendable.increaseIndentation();
    if (!getTypeReferences().is(operation.getReturnType(), Void.TYPE))
      appendable.newLine().append("return ");
    else appendable.newLine();
    expression.exec(appendable);
    appendable.append(".");
    JvmOperation actualOperation =
        closures.findImplementingOperation(functionType, context.eResource());
    appendable.append(actualOperation.getSimpleName());
    appendable.append("(");
    for (Iterator<JvmFormalParameter> iterator = params.iterator(); iterator.hasNext(); ) {
      JvmFormalParameter p = iterator.next();
      final String name = p.getName();
      appendable.append(name);
      if (iterator.hasNext()) appendable.append(",");
    }
    appendable.append(");");
    appendable.decreaseIndentation();
    appendable.newLine().append("}");
    appendable.decreaseIndentation().decreaseIndentation();
    appendable.newLine().append("}");
  }