org.eclipse.xtext.common.types.JvmSynonymTypeReference Java Examples

The following examples show how to use org.eclipse.xtext.common.types.JvmSynonymTypeReference. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XbaseInterpreter.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected boolean isInstanceoOf(Object value, JvmTypeReference type) {
	if (type instanceof JvmSynonymTypeReference) {
		for(JvmTypeReference synonym: ((JvmSynonymTypeReference) type).getReferences()) {
			if (isInstanceoOf(value, synonym)) {
				return true;
			}
		}
		return false;
	}
	String exceptionTypeName = type.getType().getQualifiedName();
	try {
		Class<?> exceptionType = classFinder.forName(exceptionTypeName);
		if (exceptionType.isInstance(value))
			return true;
	} catch (ClassNotFoundException e) {
		throw new EvaluationException(new NoClassDefFoundError(exceptionTypeName));
	}
	return false;
}
 
Example #2
Source File: ParserTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testMultiCatch_01() throws Exception {
	XtendClass clazz = clazz("class Foo { def void m() { try {} catch(NullPointerException | IllegalArgumentException e) {} } }");
	assertEquals(1, clazz.getMembers().size());
	XtendFunction m = (XtendFunction) clazz.getMembers().get(0);
	XBlockExpression body = (XBlockExpression) m.getExpression();
	assertEquals(1, body.getExpressions().size());
	XTryCatchFinallyExpression tryCatch = (XTryCatchFinallyExpression) body.getExpressions().get(0);
	XCatchClause singleCatchClause = tryCatch.getCatchClauses().get(0);
	XtendFormalParameter parameter = (XtendFormalParameter) singleCatchClause.getDeclaredParam();
	assertFalse(parameter.isExtension());
	JvmSynonymTypeReference parameterType = (JvmSynonymTypeReference) parameter.getParameterType();
	assertEquals(2, parameterType.getReferences().size());
}
 
Example #3
Source File: ParserTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testMultiCatch_02() throws Exception {
	XtendClass clazz = clazz("class Foo { def void m() { try {} catch(extension NullPointerException | IllegalArgumentException | IllegalStateException e) {} } }");
	assertEquals(1, clazz.getMembers().size());
	XtendFunction m = (XtendFunction) clazz.getMembers().get(0);
	XBlockExpression body = (XBlockExpression) m.getExpression();
	assertEquals(1, body.getExpressions().size());
	XTryCatchFinallyExpression tryCatch = (XTryCatchFinallyExpression) body.getExpressions().get(0);
	XCatchClause singleCatchClause = tryCatch.getCatchClauses().get(0);
	XtendFormalParameter parameter = (XtendFormalParameter) singleCatchClause.getDeclaredParam();
	assertTrue(parameter.isExtension());
	JvmSynonymTypeReference parameterType = (JvmSynonymTypeReference) parameter.getParameterType();
	assertEquals(3, parameterType.getReferences().size());
}
 
Example #4
Source File: ParserTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testMultiGuard_01() throws Exception {
	XtendClass clazz = clazz("class Foo { def void m(Object x) { switch(x) { NullPointerException | IllegalArgumentException : x } } }");
	assertEquals(1, clazz.getMembers().size());
	XtendFunction m = (XtendFunction) clazz.getMembers().get(0);
	XBlockExpression body = (XBlockExpression) m.getExpression();
	assertEquals(1, body.getExpressions().size());
	XSwitchExpression switchExpr = (XSwitchExpression) body.getExpressions().get(0);
	XCasePart singleCase = switchExpr.getCases().get(0);
	JvmSynonymTypeReference parameterType = (JvmSynonymTypeReference) singleCase.getTypeGuard();
	assertEquals(2, parameterType.getReferences().size());
}
 
Example #5
Source File: ProxyAwareUIStrings.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public StringBuilder doVisitSynonymTypeReference(JvmSynonymTypeReference reference, StringBuilder param) {
	return doVisitCompoundTypeReference(reference, param, " | ");
}
 
Example #6
Source File: LightweightTypeReferenceFactory.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public LightweightTypeReference doVisitSynonymTypeReference(JvmSynonymTypeReference reference) {
	return doVisitCompoundReference(reference, true);
}
 
Example #7
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected void appendCatchClause(XCatchClause catchClause, boolean parentIsReferenced, String parentVariable,
		ITreeAppendable appendable) {
	JvmTypeReference type = catchClause.getDeclaredParam().getParameterType();
	final String declaredParamName = makeJavaIdentifier(catchClause.getDeclaredParam().getName());
	final String name = appendable.declareVariable(catchClause.getDeclaredParam(), declaredParamName);
	appendable.append("if (");
	JvmTypeReference typeToUse = type;
	if (type instanceof JvmSynonymTypeReference) {
		List<JvmTypeReference> references = ((JvmSynonymTypeReference) type).getReferences();
		Iterator<JvmTypeReference> iter = references.iterator();
		while(iter.hasNext()) {
			appendable.append(parentVariable).append(" instanceof ");
			serialize(iter.next(), catchClause, appendable);
			if (iter.hasNext()) {
				appendable.append(" || ");
			}
		}
		typeToUse = resolveSynonymType((JvmSynonymTypeReference) type, catchClause);
	} else {
		appendable.append(parentVariable).append(" instanceof ");
		serialize(type, catchClause, appendable);			
	}
	appendable.append(") ").append("{");
	appendable.increaseIndentation();
	ITreeAppendable withDebugging = appendable.trace(catchClause, true);
	if (!XbaseUsageCrossReferencer.find(catchClause.getDeclaredParam(), catchClause.getExpression()).isEmpty()) {
		ITreeAppendable parameterAppendable = withDebugging.trace(catchClause.getDeclaredParam());
		appendCatchClauseParameter(catchClause, typeToUse, name, parameterAppendable.newLine());
		withDebugging.append(" = (");
		serialize(typeToUse, catchClause, withDebugging);
		withDebugging.append(")").append(parentVariable).append(";");
	}
	final boolean canBeReferenced = parentIsReferenced && ! isPrimitiveVoid(catchClause.getExpression());
	internalToJavaStatement(catchClause.getExpression(), withDebugging, canBeReferenced);
	if (canBeReferenced) {
		appendable.newLine().append(getVarName(catchClause.eContainer(), appendable)).append(" = ");
		internalToConvertedExpression(catchClause.getExpression(), appendable, getLightweightType((XExpression) catchClause.eContainer()));
		appendable.append(";");
	}
	closeBlock(appendable);
}
 
Example #8
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected JvmTypeReference resolveSynonymType(JvmSynonymTypeReference reference, EObject context) {
	LightweightTypeReference lightweight = toLightweight(reference, context);
	LightweightTypeReference superType = lightweight.getOwner().getServices().getTypeConformanceComputer().getCommonSuperType(lightweight.getMultiTypeComponents(), lightweight.getOwner());
	return superType.toJavaCompliantTypeReference();
}
 
Example #9
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected ITreeAppendable appendOpenIfStatement(XCasePart casePart, ITreeAppendable b, String matchedVariable, String variableName, XSwitchExpressionCompilationState state) {
	ITreeAppendable caseAppendable = b.trace(casePart, true);
	if (state.caseNeedsIfNotMatchedCheck()) {
		caseAppendable.newLine().append("if (!").append(matchedVariable).append(") {");
		caseAppendable.increaseIndentation();
	}
	JvmTypeReference typeGuard = casePart.getTypeGuard();
	if (typeGuard != null) {
		ITreeAppendable typeGuardAppendable = caseAppendable.trace(typeGuard, true);
		typeGuardAppendable.newLine().append("if (");
		if (typeGuard instanceof JvmSynonymTypeReference) {
			Iterator<JvmTypeReference> iter = ((JvmSynonymTypeReference) typeGuard).getReferences().iterator();
			while(iter.hasNext()) {
				typeGuardAppendable.append(variableName);
				typeGuardAppendable.append(" instanceof ");
				typeGuardAppendable.trace(typeGuard).append(iter.next().getType());
				if (iter.hasNext()) {
					typeGuardAppendable.append(" || ");
				}
			}
		} else {
			typeGuardAppendable.append(variableName);
			typeGuardAppendable.append(" instanceof ");
			typeGuardAppendable.trace(typeGuard).append(typeGuard.getType());
		}
		typeGuardAppendable.append(") {");
		typeGuardAppendable.increaseIndentation();
		typeGuardAppendable.openPseudoScope();
	}
	if (casePart.getCase() != null) {
		ITreeAppendable conditionAppendable = caseAppendable.trace(casePart.getCase(), true);
		internalToJavaStatement(casePart.getCase(), conditionAppendable, true);
		conditionAppendable.newLine().append("if (");
		LightweightTypeReference convertedType = getLightweightType(casePart.getCase());
		if (convertedType.isType(Boolean.TYPE) || convertedType.isType(Boolean.class)) {
			internalToJavaExpression(casePart.getCase(), conditionAppendable);
		} else {
			JvmType objectsType = findKnownType(Objects.class, casePart);
			if (objectsType != null) {
				conditionAppendable.append(objectsType);
				conditionAppendable.append(".equal(").append(variableName).append(", ");
				internalToJavaExpression(casePart.getCase(), conditionAppendable);
				conditionAppendable.append(")");
			} else {
				// use ObjectExtensions rather than a == b || a != null && a.equals(b) since
				// that won't work with primitive types and other incompatible conditional operands
				conditionAppendable.append(ObjectExtensions.class);
				conditionAppendable.append(".operator_equals(").append(variableName).append(", ");
				internalToJavaExpression(casePart.getCase(), conditionAppendable);
				conditionAppendable.append(")");
			}
		}
		conditionAppendable.append(")");
		caseAppendable.append(" {");
		caseAppendable.increaseIndentation();
	}
	// set matched to true
	return caseAppendable.newLine().append(matchedVariable).append("=true;");
}
 
Example #10
Source File: AbstractTypeReferenceVisitor.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Result doVisitSynonymTypeReference(JvmSynonymTypeReference reference) {
	return doVisitCompoundTypeReference(reference);
}
 
Example #11
Source File: AbstractTypeReferenceVisitorWithParameter.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Result doVisitSynonymTypeReference(JvmSynonymTypeReference reference, Parameter param) {
	return doVisitCompoundTypeReference(reference, param);
}
 
Example #12
Source File: AbstractXtendSemanticSequencer.java    From xtext-xtend with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * Contexts:
 *     MultiTypeReference returns JvmSynonymTypeReference
 *
 * Constraint:
 *     (references+=MultiTypeReference_JvmSynonymTypeReference_1_0 references+=JvmTypeReference+)
 */
protected void sequence_MultiTypeReference(ISerializationContext context, JvmSynonymTypeReference semanticObject) {
	genericSequencer.createSequence(context, semanticObject);
}
 
Example #13
Source File: ITypeReferenceVisitorWithParameter.java    From xtext-extras with Eclipse Public License 2.0 votes vote down vote up
Result doVisitSynonymTypeReference(JvmSynonymTypeReference reference, Parameter param); 
Example #14
Source File: ITypeReferenceVisitor.java    From xtext-extras with Eclipse Public License 2.0 votes vote down vote up
Result doVisitSynonymTypeReference(JvmSynonymTypeReference reference);