/** @param primitive unused in this context but useful for inheritors */ protected void convertPrimitiveToWrapper( final JvmTypeReference primitive, final JvmTypeReference wrapper, XExpression context, final ITreeAppendable appendable, final Later expression) { serialize(wrapper, context, appendable); appendable.append("."); appendable.append("valueOf("); expression.exec(appendable); appendable.append(")"); }
/** @param wrapper unused in this context but useful for inheritors */ protected void convertWrapperToPrimitive( final JvmTypeReference wrapper, final JvmTypeReference primitive, XExpression context, final ITreeAppendable appendable, final Later expression) { appendable.append("("); expression.exec(appendable); appendable.append(")"); appendable.append("."); serialize(primitive, context, appendable); appendable.append("Value("); appendable.append(")"); }
protected void convertArrayToList( final JvmTypeReference left, final ITreeAppendable appendable, XExpression context, final Later expression) { appendable.append("(("); serialize(left, context, appendable); appendable.append(")"); JvmTypeReference conversions = getTypeReferences().getTypeForName(Conversions.class, context); serialize(conversions, context, appendable); appendable.append(".doWrapArray("); expression.exec(appendable); appendable.append("))"); }
@Override protected final void internalToConvertedExpression( final XExpression obj, ITreeAppendable appendable, @Nullable JvmTypeReference toBeConvertedTo) { if (toBeConvertedTo != null) { JvmTypeReference actualType = getTypeProvider().getType(obj); if (!EcoreUtil.equals(toBeConvertedTo, actualType)) { doConversion( toBeConvertedTo, actualType, appendable, obj, new Later() { public void exec(ITreeAppendable appendable) { appendable = appendable.trace(obj, true); internalToConvertedExpression(obj, appendable); } }); return; } } final ITreeAppendable trace = appendable.trace(obj, true); internalToConvertedExpression(obj, trace); }
protected void convertListToArray( final JvmTypeReference arrayTypeReference, final ITreeAppendable appendable, XExpression context, final Later expression) { appendable.append("(("); serialize(arrayTypeReference, context, appendable); appendable.append(")"); JvmTypeReference conversions = getTypeReferences().getTypeForName(Conversions.class, context); serialize(conversions, context, appendable); appendable.append(".unwrapArray("); expression.exec(appendable); JvmGenericArrayTypeReference rawTypeArrayReference = (JvmGenericArrayTypeReference) rawTypeHelper.getRawTypeReference(arrayTypeReference, context.eResource()); appendable.append(", "); serialize(rawTypeArrayReference.getComponentType(), context, appendable); appendable.append(".class))"); }
protected void convertMultiType( JvmTypeReference expectation, JvmMultiTypeReference multiType, XExpression context, ITreeAppendable b, Later expression) { JvmTypeReference castTo = null; for (JvmTypeReference candidate : multiType.getReferences()) { if (getTypeConformanceComputer().isConformant(expectation, candidate, true)) { castTo = candidate; break; } } if (castTo != null) { b.append("(("); serialize(castTo, context, b, true, false); b.append(")"); } expression.exec(b); if (castTo != null) { b.append(")"); } }
public void serialize( final JvmTypeReference type, EObject context, IAppendable appendable, boolean withoutConstraints, boolean paramsToWildcard, boolean paramsToObject, boolean allowPrimitives) { IAppendable tracedAppendable = appendable; boolean tracing = false; if (appendable instanceof ITreeAppendable && type.eResource() == context.eResource()) { tracedAppendable = ((ITreeAppendable) appendable).trace(type); tracing = true; } if (type instanceof JvmWildcardTypeReference) { JvmWildcardTypeReference wildcard = (JvmWildcardTypeReference) type; if (!withoutConstraints) { tracedAppendable.append("?"); } if (!wildcard.getConstraints().isEmpty()) { for (JvmTypeConstraint constraint : wildcard.getConstraints()) { if (constraint instanceof JvmLowerBound) { if (!withoutConstraints) tracedAppendable.append(" super "); serialize( constraint.getTypeReference(), context, tracedAppendable, withoutConstraints, paramsToWildcard, paramsToObject, false); return; } } boolean first = true; for (JvmTypeConstraint constraint : wildcard.getConstraints()) { if (constraint instanceof JvmUpperBound) { if (first) { if (!withoutConstraints) tracedAppendable.append(" extends "); first = false; } else { if (withoutConstraints) throw new IllegalStateException( "cannot have two upperbounds if type should be printed without constraints"); tracedAppendable.append(" & "); } serialize( constraint.getTypeReference(), context, tracedAppendable, withoutConstraints, paramsToWildcard, paramsToObject, false); } } } else if (withoutConstraints) { tracedAppendable.append("Object"); } } else if (type instanceof JvmGenericArrayTypeReference) { serialize( ((JvmGenericArrayTypeReference) type).getComponentType(), context, tracedAppendable, withoutConstraints, paramsToWildcard, paramsToObject, true); tracedAppendable.append("[]"); } else if (type instanceof JvmParameterizedTypeReference) { JvmParameterizedTypeReference parameterized = (JvmParameterizedTypeReference) type; if ((paramsToWildcard || paramsToObject) && parameterized.getType() instanceof JvmTypeParameter) { JvmTypeParameter parameter = (JvmTypeParameter) parameterized.getType(); if (!isLocalTypeParameter(context, parameter)) { if (paramsToWildcard) tracedAppendable.append("?"); else tracedAppendable.append("Object"); return; } } JvmType jvmType = allowPrimitives ? type.getType() : primitives.asWrapperTypeIfPrimitive(type).getType(); if (tracing) { ITextRegion region = locationProvider.getFullTextRegion( type, TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE, 0); if (region instanceof ITextRegionWithLineInformation) { ((ITreeAppendable) tracedAppendable) .trace(new LocationData((ITextRegionWithLineInformation) region, null)) .append(jvmType); } else { tracedAppendable.append(jvmType); } } else { tracedAppendable.append(jvmType); } if (!parameterized.getArguments().isEmpty()) { tracedAppendable.append("<"); for (int i = 0; i < parameterized.getArguments().size(); i++) { if (i != 0) { tracedAppendable.append(","); } serialize( parameterized.getArguments().get(i), context, tracedAppendable, withoutConstraints, paramsToWildcard, paramsToObject, false); } tracedAppendable.append(">"); } } else if (type instanceof JvmAnyTypeReference) { tracedAppendable.append("Object"); } else if (type instanceof JvmMultiTypeReference) { serialize( resolveMultiType(type, context), context, tracedAppendable, withoutConstraints, paramsToWildcard, paramsToObject, allowPrimitives); } else if (type instanceof JvmDelegateTypeReference) { JvmTypeReference delegate = ((JvmDelegateTypeReference) type).getDelegate(); if (delegate != null) serialize( delegate, context, tracedAppendable, withoutConstraints, paramsToWildcard, paramsToObject, allowPrimitives); else tracedAppendable.append("Object"); } else if (type instanceof JvmSpecializedTypeReference) { serialize( ((JvmSpecializedTypeReference) type).getEquivalent(), context, tracedAppendable, withoutConstraints, paramsToWildcard, paramsToObject, allowPrimitives); } else if (type instanceof JvmUnknownTypeReference) { if (type.eIsSet(TypesPackage.Literals.JVM_UNKNOWN_TYPE_REFERENCE__QUALIFIED_NAME)) { tracedAppendable.append(type.getQualifiedName()); } else { tracedAppendable.append("Object"); } } else { throw new IllegalArgumentException(String.valueOf(type)); } }
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("}"); }