use of org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference in project xtext-xtend by eclipse.
the class XtendValidator method createExceptionMismatchError.
protected void createExceptionMismatchError(IResolvedOperation operation, EObject sourceElement, List<IResolvedOperation> exceptionMismatch) {
List<LightweightTypeReference> exceptions = operation.getIllegallyDeclaredExceptions();
StringBuilder message = new StringBuilder(100);
message.append("The declared exception");
if (exceptions.size() > 1) {
message.append('s');
}
message.append(' ');
for (int i = 0; i < exceptions.size(); i++) {
if (i != 0) {
if (i != exceptions.size() - 1)
message.append(", ");
else
message.append(" and ");
}
message.append(exceptions.get(i).getHumanReadableName());
}
if (exceptions.size() > 1) {
message.append(" are");
} else {
message.append(" is");
}
message.append(" not compatible with throws clause in ");
for (int i = 0; i < exceptionMismatch.size(); i++) {
if (i != 0) {
if (i != exceptionMismatch.size() - 1)
message.append(", ");
else
message.append(" and ");
}
IResolvedOperation resolvedOperation = exceptionMismatch.get(i);
message.append(getDeclaratorName(resolvedOperation));
message.append('.');
message.append(exceptionMismatch.get(i).getSimpleSignature());
}
error(message.toString(), sourceElement, exceptionsFeature(sourceElement), INCOMPATIBLE_THROWS_CLAUSE);
}
use of org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference in project xtext-xtend by eclipse.
the class XtendValidator method checkExceptions.
private void checkExceptions(EObject context, List<JvmTypeReference> exceptions, EReference reference) {
Set<String> declaredExceptionNames = Sets.newHashSet();
JvmTypeReference throwableType = getServices().getTypeReferences().getTypeForName(Throwable.class, context);
if (throwableType == null) {
return;
}
ITypeReferenceOwner owner = new StandardTypeReferenceOwner(getServices(), context);
LightweightTypeReference throwableReference = owner.toLightweightTypeReference(throwableType);
for (int i = 0; i < exceptions.size(); i++) {
JvmTypeReference exception = exceptions.get(i);
// throwables may not carry generics thus the raw comparison is sufficient
if (exception.getType() != null && !exception.getType().eIsProxy()) {
if (!throwableReference.isAssignableFrom(exception.getType()))
error("No exception of type " + exception.getSimpleName() + " can be thrown; an exception type must be a subclass of Throwable", reference, i, EXCEPTION_NOT_THROWABLE);
if (!declaredExceptionNames.add(exception.getQualifiedName()))
error("Exception " + exception.getSimpleName() + " is declared twice", reference, i, EXCEPTION_DECLARED_TWICE);
}
}
}
use of org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference in project xtext-xtend by eclipse.
the class CacheMethodCompileStrategy method apply.
@Override
public void apply(ITreeAppendable appendable) {
JvmOperation cacheMethod = (JvmOperation) logicalContainerProvider.getLogicalContainer(createExtensionInfo.getCreateExpression());
JvmDeclaredType containerType = cacheMethod.getDeclaringType();
IResolvedTypes resolvedTypes = typeResolver.resolveTypes(containerType);
final ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, containerType);
LightweightTypeReference listType = owner.newReferenceTo(ArrayList.class, new TypeReferenceInitializer<ParameterizedTypeReference>() {
@Override
public LightweightTypeReference enhance(ParameterizedTypeReference reference) {
reference.addTypeArgument(owner.newWildcardTypeReference());
return reference;
}
});
String cacheVarName = cacheField.getSimpleName();
String cacheKeyVarName = appendable.declareSyntheticVariable("CacheKey", "_cacheKey");
appendable.append("final ").append(listType).append(" ").append(cacheKeyVarName).append(" = ").append(CollectionLiterals.class).append(".newArrayList(");
List<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
LightweightTypeReference returnType = resolvedTypes.getActualType(initializerMethod.getParameters().get(0));
if (returnType != null) {
appendable.newLine().append("final ").append(returnType);
} else {
appendable.newLine().append("final Object");
}
String resultVarName = "_result";
appendable.append(" ").append(resultVarName).append(";");
// open synchronize block
appendable.newLine().append("synchronized (").append(cacheVarName).append(") {");
appendable.increaseIndentation();
// if the cache contains the key return the previously created object.
appendable.newLine().append("if (").append(cacheVarName).append(".containsKey(").append(cacheKeyVarName).append(")) {");
appendable.increaseIndentation();
appendable.newLine().append("return ").append(cacheVarName).append(".get(").append(cacheKeyVarName).append(");");
appendable.decreaseIndentation().newLine().append("}");
// execute the creation
compiler.toJavaStatement(createExtensionInfo.getCreateExpression(), appendable, true);
appendable.newLine();
appendable.append(resultVarName).append(" = ");
compiler.toJavaExpression(createExtensionInfo.getCreateExpression(), appendable);
appendable.append(";");
// store the newly created object in the cache
appendable.newLine().append(cacheVarName).append(".put(").append(cacheKeyVarName).append(", ");
LightweightTypeReference fieldType = resolvedTypes.getActualType(cacheField);
LightweightTypeReference declaredResultType = fieldType.getTypeArguments().get(1);
boolean castRequired = false;
if (!declaredResultType.isAssignableFrom(returnType)) {
castRequired = true;
appendable.append("(").append(declaredResultType).append(")");
}
appendable.append(resultVarName).append(");");
// close synchronize block
appendable.decreaseIndentation();
appendable.newLine().append("}");
appendable.newLine().append(initializerMethod.getSimpleName()).append("(").append(resultVarName);
for (JvmFormalParameter parameter : cacheMethod.getParameters()) {
appendable.append(", ").append(parameter.getName());
}
appendable.append(");");
// return the result
appendable.newLine().append("return ");
if (castRequired) {
appendable.append("(").append(declaredResultType).append(")");
}
appendable.append(resultVarName).append(";");
}
use of org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference in project xtext-xtend by eclipse.
the class DispatchMethodCompileStrategy method generateActualDispatchCall.
protected void generateActualDispatchCall(JvmOperation dispatchOperation, JvmOperation actualOperationToCall, ITreeAppendable a, ITypeReferenceOwner owner) {
a.append(actualOperationToCall.getSimpleName()).append("(");
Iterator<JvmFormalParameter> iter1 = dispatchOperation.getParameters().iterator();
for (Iterator<JvmFormalParameter> iter2 = actualOperationToCall.getParameters().iterator(); iter2.hasNext(); ) {
JvmFormalParameter p1 = iter1.next();
JvmFormalParameter p2 = iter2.next();
LightweightTypeReference type1 = owner.toLightweightTypeReference(p1.getParameterType());
LightweightTypeReference type2 = owner.toLightweightTypeReference(p2.getParameterType());
if (!type2.isAssignableFrom(type1, new TypeConformanceComputationArgument(true, false, true, true, false, false))) {
a.append("(").append(type2.getWrapperTypeIfPrimitive()).append(")");
}
if (typeReferences.is(p2.getParameterType(), Void.class)) {
a.append("null");
} else {
a.append(getVarName(p1, a));
}
if (iter2.hasNext()) {
a.append(", ");
}
}
a.append(")");
}
use of org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference in project xtext-xtend by eclipse.
the class ReorderedFeatureCallArgumentsTest method testBug457779_01.
@Test
public void testBug457779_01() {
final IFeatureCallArguments arguments = this.toArgumentsWithReceiver("String s, int i", "[], 1");
final XExpression first = arguments.getArgument(0);
Assert.assertNull(first);
final LightweightTypeReference firstType = arguments.getDeclaredTypeForLambda(0);
Assert.assertNull(firstType);
final XExpression third = arguments.getArgument(1);
Assert.assertTrue((third instanceof XNumberLiteral));
final LightweightTypeReference thirdType = arguments.getDeclaredTypeForLambda(1);
Assert.assertEquals("int", thirdType.getSimpleName());
final XExpression second = arguments.getArgument(2);
Assert.assertTrue((second instanceof XClosure));
final LightweightTypeReference secondType = arguments.getDeclaredTypeForLambda(2);
Assert.assertEquals("String", secondType.getSimpleName());
try {
arguments.getArgument(3);
Assert.fail("Expected exception");
} catch (final Throwable _t) {
if (_t instanceof IndexOutOfBoundsException) {
} else {
throw Exceptions.sneakyThrow(_t);
}
}
try {
arguments.getDeclaredTypeForLambda(3);
Assert.fail("Expected exception");
} catch (final Throwable _t_1) {
if (_t_1 instanceof IndexOutOfBoundsException) {
} else {
throw Exceptions.sneakyThrow(_t_1);
}
}
}
Aggregations