Java源码示例:org.eclipse.xtext.xtype.XFunctionTypeRef
示例1@Override
public JvmType getType(JvmTypeReference it) {
if (it instanceof XFunctionTypeRefImplCustom) {
return _getType((XFunctionTypeRefImplCustom) it);
} else if (it instanceof JvmGenericArrayTypeReferenceImplCustom) {
return _getType((JvmGenericArrayTypeReferenceImplCustom) it);
} else if (it instanceof XFunctionTypeRef) {
return _getType((XFunctionTypeRef) it);
} else if (it instanceof JvmParameterizedTypeReference) {
return _getType((JvmParameterizedTypeReference) it);
} else if (it != null) {
return _getType(it);
} else {
throw new IllegalArgumentException("Unhandled parameter types: " + Arrays.asList(it).toString());
}
}
@Check
public void checkFunctionTypeArgsNonVoid(XFunctionTypeRef typeRef) {
for (JvmTypeReference paramType : typeRef.getParamTypes()) {
JvmType type = paramType.getType();
if (type instanceof JvmVoid && !type.eIsProxy()) {
error("The primitive 'void' cannot be the type of a function parameter. ",
paramType, null, -1, INVALID_USE_OF_TYPE);
}
}
}
@Override
public StringBuilder doVisitFunctionTypeReference(XFunctionTypeRef reference, StringBuilder param) {
param.append("(");
List<JvmTypeReference> paramTypes = reference.getParamTypes();
for(int i = 0, size = paramTypes.size(); i < size; i++) {
if (i != 0) {
param.append(", ");
}
param = visit(paramTypes.get(i), param);
if (param == null)
return null;
}
param.append(")=>");
return visit(reference.getReturnType(), param);
}
@Override
public LightweightTypeReference doVisitFunctionTypeReference(XFunctionTypeRef reference) {
JvmTypeReference equivalent = reference.getEquivalent();
if (equivalent == null) {
return super.doVisitFunctionTypeReference(reference);
}
FunctionTypeReference result;
// constructors used below to avoid subsequent checks for isInner which was supposed to be done by
// the computation of the equivalent
if (equivalent.eClass() == TypesPackage.Literals.JVM_INNER_TYPE_REFERENCE) {
JvmParameterizedTypeReference outer = ((JvmInnerTypeReference) equivalent).getOuter();
LightweightTypeReference outerEquivalent = outer.accept(this);
result = new InnerFunctionTypeReference(owner, outerEquivalent, reference.getType());
} else {
result = new FunctionTypeReference(owner, reference.getType());
}
if (equivalent instanceof JvmParameterizedTypeReference) {
for(JvmTypeReference argument: ((JvmParameterizedTypeReference)equivalent).getArguments()) {
result.addTypeArgument(visit(argument));
}
}
for(JvmTypeReference parameter: reference.getParamTypes()) {
result.addParameterType(visit(parameter));
}
if (reference.getReturnType() != null) {
result.setReturnType(visit(reference.getReturnType()));
}
return result;
}
@Override
public JvmTypeReference toTypeReference() {
XFunctionTypeRef result = getOwner().getServices().getXtypeFactory().createXFunctionTypeRef();
result.setType(getType());
result.setEquivalent(getEquivalentTypeReference());
if (parameterTypes != null) {
for(LightweightTypeReference parameterType: parameterTypes) {
result.getParamTypes().add(parameterType.toTypeReference());
}
}
if (returnType != null) {
result.setReturnType(returnType.toTypeReference());
}
return result;
}
protected void acceptType(JvmTypeReference ref) {
if (ref instanceof XFunctionTypeRef
|| ref instanceof JvmWildcardTypeReference
|| ref instanceof JvmGenericArrayTypeReference
|| ref instanceof JvmCompoundTypeReference
|| (ref.eContainer() instanceof XFunctionTypeRef
&& ref.eContainmentFeature() == JVM_SPECIALIZED_TYPE_REFERENCE__EQUIVALENT)
|| NodeModelUtils.findActualNodeFor(ref) == null)
return;
else
acceptPreferredType(ref);
}
public void format(final Object ref, final IFormattableDocument document) {
if (ref instanceof JvmTypeParameter) {
_format((JvmTypeParameter)ref, document);
return;
} else if (ref instanceof XtextResource) {
_format((XtextResource)ref, document);
return;
} else if (ref instanceof XFunctionTypeRef) {
_format((XFunctionTypeRef)ref, document);
return;
} else if (ref instanceof JvmParameterizedTypeReference) {
_format((JvmParameterizedTypeReference)ref, document);
return;
} else if (ref instanceof JvmWildcardTypeReference) {
_format((JvmWildcardTypeReference)ref, document);
return;
} else if (ref instanceof XImportDeclaration) {
_format((XImportDeclaration)ref, document);
return;
} else if (ref instanceof XImportSection) {
_format((XImportSection)ref, document);
return;
} else if (ref instanceof EObject) {
_format((EObject)ref, document);
return;
} else if (ref == null) {
_format((Void)null, document);
return;
} else if (ref != null) {
_format(ref, document);
return;
} else {
throw new IllegalArgumentException("Unhandled parameter types: " +
Arrays.<Object>asList(ref, document).toString());
}
}
@Test public void testClosure_5() throws Exception {
XClosure closure = (XClosure) expression("[(String) => String mapper|mapper('something')]");
assertTrue(((XBlockExpression)closure.getExpression()).getExpressions().get(0) instanceof XFeatureCall);
JvmFormalParameter formalParameter = closure.getFormalParameters().get(0);
assertEquals("mapper", formalParameter.getName());
assertTrue(formalParameter.getParameterType() instanceof XFunctionTypeRef);
}
@Test public void testClosure_6() throws Exception {
XClosure closure = (XClosure) expression("([(String) => String mapper|mapper('something')])");
assertTrue(((XBlockExpression)closure.getExpression()).getExpressions().get(0) instanceof XFeatureCall);
JvmFormalParameter formalParameter = closure.getFormalParameters().get(0);
assertEquals("mapper", formalParameter.getName());
assertTrue(formalParameter.getParameterType() instanceof XFunctionTypeRef);
}
@Test
public void testBug427257() throws Exception {
XtendClass clazz = clazz("class C { def Iterable<=>String> m() { emptyList } }");
assertEquals(1, clazz.getMembers().size());
XtendFunction m = (XtendFunction) clazz.getMembers().get(0);
JvmParameterizedTypeReference returnType = (JvmParameterizedTypeReference) m.getReturnType();
JvmTypeReference typeArgument = returnType.getArguments().get(0);
assertTrue(typeArgument instanceof XFunctionTypeRef);
}
@Test public void testFunctionTypeRef_1() throws Exception {
XtendFunction func = function("def (String)=>Boolean foo() { [String s|s==null]}");
XFunctionTypeRef type = (XFunctionTypeRef) func.getReturnType();
assertNotNull(type.getReturnType());
assertEquals(1,type.getParamTypes().size());
assertNotNull(type.getParamTypes().get(0));
}
@Test public void testTypeReference_withImport() throws Exception {
XtendFunction func = (XtendFunction) clazz("import java.lang.* class X { def (String)=>Boolean foo() {[|true]} }").getMembers().get(0);
XFunctionTypeRef type = (XFunctionTypeRef) func.getReturnType();
JvmTypeReference returnType = type.getReturnType();
assertEquals("java.lang.Boolean", returnType.getIdentifier());
JvmTypeReference paramType = type.getParamTypes().get(0);
assertEquals("java.lang.String", paramType.getIdentifier());
}
@Override
public void sequence(ISerializationContext context, EObject semanticObject) {
EPackage epackage = semanticObject.eClass().getEPackage();
ParserRule rule = context.getParserRule();
Action action = context.getAssignedAction();
Set<Parameter> parameters = context.getEnabledBooleanParameters();
if (epackage == TypesPackage.eINSTANCE)
switch (semanticObject.eClass().getClassifierID()) {
case TypesPackage.JVM_GENERIC_ARRAY_TYPE_REFERENCE:
sequence_JvmTypeReference(context, (JvmGenericArrayTypeReference) semanticObject);
return;
case TypesPackage.JVM_INNER_TYPE_REFERENCE:
sequence_JvmParameterizedTypeReference(context, (JvmInnerTypeReference) semanticObject);
return;
case TypesPackage.JVM_LOWER_BOUND:
if (rule == grammarAccess.getJvmLowerBoundAndedRule()) {
sequence_JvmLowerBoundAnded(context, (JvmLowerBound) semanticObject);
return;
}
else if (rule == grammarAccess.getJvmLowerBoundRule()) {
sequence_JvmLowerBound(context, (JvmLowerBound) semanticObject);
return;
}
else break;
case TypesPackage.JVM_PARAMETERIZED_TYPE_REFERENCE:
if (action == grammarAccess.getJvmParameterizedTypeReferenceAccess().getJvmInnerTypeReferenceOuterAction_1_4_0_0_0()) {
sequence_JvmParameterizedTypeReference_JvmInnerTypeReference_1_4_0_0_0(context, (JvmParameterizedTypeReference) semanticObject);
return;
}
else if (rule == grammarAccess.getJvmTypeReferenceRule()
|| action == grammarAccess.getJvmTypeReferenceAccess().getJvmGenericArrayTypeReferenceComponentTypeAction_0_1_0_0()
|| rule == grammarAccess.getJvmParameterizedTypeReferenceRule()
|| rule == grammarAccess.getJvmArgumentTypeReferenceRule()) {
sequence_JvmParameterizedTypeReference(context, (JvmParameterizedTypeReference) semanticObject);
return;
}
else break;
case TypesPackage.JVM_TYPE_PARAMETER:
sequence_JvmTypeParameter(context, (JvmTypeParameter) semanticObject);
return;
case TypesPackage.JVM_UPPER_BOUND:
if (rule == grammarAccess.getJvmUpperBoundAndedRule()) {
sequence_JvmUpperBoundAnded(context, (JvmUpperBound) semanticObject);
return;
}
else if (rule == grammarAccess.getJvmUpperBoundRule()) {
sequence_JvmUpperBound(context, (JvmUpperBound) semanticObject);
return;
}
else break;
case TypesPackage.JVM_WILDCARD_TYPE_REFERENCE:
sequence_JvmWildcardTypeReference(context, (JvmWildcardTypeReference) semanticObject);
return;
}
else if (epackage == XtypePackage.eINSTANCE)
switch (semanticObject.eClass().getClassifierID()) {
case XtypePackage.XFUNCTION_TYPE_REF:
sequence_XFunctionTypeRef(context, (XFunctionTypeRef) semanticObject);
return;
case XtypePackage.XIMPORT_DECLARATION:
sequence_XImportDeclaration(context, (XImportDeclaration) semanticObject);
return;
case XtypePackage.XIMPORT_SECTION:
sequence_XImportSection(context, (XImportSection) semanticObject);
return;
}
if (errorAcceptor != null)
errorAcceptor.accept(diagnosticProvider.createInvalidContextOrTypeDiagnostic(semanticObject, context));
}
@Deprecated
protected void sequence_XFunctionTypeRef(EObject context, XFunctionTypeRef semanticObject) {
sequence_XFunctionTypeRef(createContext(context, semanticObject), semanticObject);
}
protected JvmType _getType(XFunctionTypeRef it) {
return getType(it, XtypePackage.Literals.XFUNCTION_TYPE_REF__TYPE);
}
@Override
public Result doVisitFunctionTypeReference(XFunctionTypeRef reference) {
return doVisitSpecializedTypeReference(reference);
}
@Override
public Result doVisitFunctionTypeReference(XFunctionTypeRef reference, Parameter param) {
return doVisitSpecializedTypeReference(reference, param);
}
/**
* Complete the initialization of the package and its meta-model. This
* method is guarded to have no affect on any invocation but its first.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
public void initializePackageContents()
{
if (isInitialized) return;
isInitialized = true;
// Initialize package
setName(eNAME);
setNsPrefix(eNS_PREFIX);
setNsURI(eNS_URI);
// Obtain other dependent packages
TypesPackage theTypesPackage = (TypesPackage)EPackage.Registry.INSTANCE.getEPackage(TypesPackage.eNS_URI);
// Create type parameters
// Set bounds for type parameters
// Add supertypes to classes
xFunctionTypeRefEClass.getESuperTypes().add(theTypesPackage.getJvmSpecializedTypeReference());
xComputedTypeReferenceEClass.getESuperTypes().add(theTypesPackage.getJvmSpecializedTypeReference());
// Initialize classes and features; add operations and parameters
initEClass(xFunctionTypeRefEClass, XFunctionTypeRef.class, "XFunctionTypeRef", !IS_ABSTRACT, !IS_INTERFACE, IS_GENERATED_INSTANCE_CLASS);
initEReference(getXFunctionTypeRef_ParamTypes(), theTypesPackage.getJvmTypeReference(), null, "paramTypes", null, 0, -1, XFunctionTypeRef.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, IS_COMPOSITE, !IS_RESOLVE_PROXIES, !IS_UNSETTABLE, IS_UNIQUE, !IS_DERIVED, IS_ORDERED);
initEReference(getXFunctionTypeRef_ReturnType(), theTypesPackage.getJvmTypeReference(), null, "returnType", null, 0, 1, XFunctionTypeRef.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, IS_COMPOSITE, !IS_RESOLVE_PROXIES, !IS_UNSETTABLE, IS_UNIQUE, !IS_DERIVED, IS_ORDERED);
initEReference(getXFunctionTypeRef_Type(), theTypesPackage.getJvmType(), null, "type", null, 0, 1, XFunctionTypeRef.class, IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, !IS_COMPOSITE, IS_RESOLVE_PROXIES, !IS_UNSETTABLE, IS_UNIQUE, IS_DERIVED, IS_ORDERED);
initEAttribute(getXFunctionTypeRef_InstanceContext(), ecorePackage.getEBoolean(), "instanceContext", null, 0, 1, XFunctionTypeRef.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, !IS_UNSETTABLE, !IS_ID, IS_UNIQUE, !IS_DERIVED, IS_ORDERED);
initEClass(xComputedTypeReferenceEClass, XComputedTypeReference.class, "XComputedTypeReference", !IS_ABSTRACT, !IS_INTERFACE, IS_GENERATED_INSTANCE_CLASS);
initEAttribute(getXComputedTypeReference_TypeProvider(), this.getIJvmTypeReferenceProvider(), "typeProvider", null, 0, 1, XComputedTypeReference.class, IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, !IS_UNSETTABLE, !IS_ID, IS_UNIQUE, !IS_DERIVED, IS_ORDERED);
initEClass(xImportSectionEClass, XImportSection.class, "XImportSection", !IS_ABSTRACT, !IS_INTERFACE, IS_GENERATED_INSTANCE_CLASS);
initEReference(getXImportSection_ImportDeclarations(), this.getXImportDeclaration(), null, "importDeclarations", null, 0, -1, XImportSection.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, IS_COMPOSITE, !IS_RESOLVE_PROXIES, !IS_UNSETTABLE, IS_UNIQUE, !IS_DERIVED, IS_ORDERED);
initEClass(xImportDeclarationEClass, XImportDeclaration.class, "XImportDeclaration", !IS_ABSTRACT, !IS_INTERFACE, IS_GENERATED_INSTANCE_CLASS);
initEAttribute(getXImportDeclaration_Wildcard(), ecorePackage.getEBoolean(), "wildcard", null, 0, 1, XImportDeclaration.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, !IS_UNSETTABLE, !IS_ID, IS_UNIQUE, !IS_DERIVED, IS_ORDERED);
initEAttribute(getXImportDeclaration_Extension(), ecorePackage.getEBoolean(), "extension", null, 0, 1, XImportDeclaration.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, !IS_UNSETTABLE, !IS_ID, IS_UNIQUE, !IS_DERIVED, IS_ORDERED);
initEAttribute(getXImportDeclaration_Static(), ecorePackage.getEBoolean(), "static", null, 0, 1, XImportDeclaration.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, !IS_UNSETTABLE, !IS_ID, IS_UNIQUE, !IS_DERIVED, IS_ORDERED);
initEReference(getXImportDeclaration_ImportedType(), theTypesPackage.getJvmDeclaredType(), null, "importedType", null, 0, 1, XImportDeclaration.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, !IS_COMPOSITE, IS_RESOLVE_PROXIES, !IS_UNSETTABLE, IS_UNIQUE, !IS_DERIVED, IS_ORDERED);
initEAttribute(getXImportDeclaration_MemberName(), ecorePackage.getEString(), "memberName", null, 0, 1, XImportDeclaration.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, !IS_UNSETTABLE, !IS_ID, IS_UNIQUE, !IS_DERIVED, IS_ORDERED);
initEAttribute(getXImportDeclaration_ImportedNamespace(), ecorePackage.getEString(), "importedNamespace", null, 0, 1, XImportDeclaration.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, !IS_UNSETTABLE, !IS_ID, IS_UNIQUE, !IS_DERIVED, IS_ORDERED);
addEOperation(xImportDeclarationEClass, ecorePackage.getEString(), "getImportedName", 0, 1, IS_UNIQUE, IS_ORDERED);
addEOperation(xImportDeclarationEClass, ecorePackage.getEString(), "getImportedTypeName", 0, 1, IS_UNIQUE, IS_ORDERED);
// Initialize data types
initEDataType(iJvmTypeReferenceProviderEDataType, IJvmTypeReferenceProvider.class, "IJvmTypeReferenceProvider", !IS_SERIALIZABLE, !IS_GENERATED_INSTANCE_CLASS);
// Create resource
createResource(eNS_URI);
}
@Test public void testFunctionTypeRef_0() throws Exception {
XtendFunction func = function("def =>Boolean foo() { [|true]}");
XFunctionTypeRef type = (XFunctionTypeRef) func.getReturnType();
assertNotNull(type.getReturnType());
assertEquals(0,type.getParamTypes().size());
}
@Test public void testTypeReference_0() throws Exception {
XtendFunction func = function("def =>java.lang.Boolean foo() {[|true]}");
XFunctionTypeRef type = (XFunctionTypeRef) func.getReturnType();
JvmTypeReference returnType = type.getReturnType();
assertEquals("java.lang.Boolean", returnType.getIdentifier());
}
/**
* Contexts:
* JvmTypeReference returns XFunctionTypeRef
* XFunctionTypeRef returns XFunctionTypeRef
* JvmArgumentTypeReference returns XFunctionTypeRef
*
* Constraint:
* ((paramTypes+=JvmTypeReference paramTypes+=JvmTypeReference*)? returnType=JvmTypeReference)
*/
protected void sequence_XFunctionTypeRef(ISerializationContext context, XFunctionTypeRef semanticObject) {
genericSequencer.createSequence(context, semanticObject);
}
/**
* Contexts:
* JvmTypeReference returns XFunctionTypeRef
* XFunctionTypeRef returns XFunctionTypeRef
* JvmArgumentTypeReference returns XFunctionTypeRef
*
* Constraint:
* ((paramTypes+=JvmTypeReference paramTypes+=JvmTypeReference*)? returnType=JvmTypeReference)
*/
protected void sequence_XFunctionTypeRef(ISerializationContext context, XFunctionTypeRef semanticObject) {
genericSequencer.createSequence(context, semanticObject);
}
/**
* Contexts:
* JvmTypeReference returns XFunctionTypeRef
* XFunctionTypeRef returns XFunctionTypeRef
* JvmArgumentTypeReference returns XFunctionTypeRef
*
* Constraint:
* ((paramTypes+=JvmTypeReference paramTypes+=JvmTypeReference*)? returnType=JvmTypeReference)
*/
protected void sequence_XFunctionTypeRef(ISerializationContext context, XFunctionTypeRef semanticObject) {
genericSequencer.createSequence(context, semanticObject);
}
/**
* Contexts:
* JvmTypeReference returns XFunctionTypeRef
* XFunctionTypeRef returns XFunctionTypeRef
* JvmArgumentTypeReference returns XFunctionTypeRef
*
* Constraint:
* ((paramTypes+=JvmTypeReference paramTypes+=JvmTypeReference*)? returnType=JvmTypeReference)
*/
protected void sequence_XFunctionTypeRef(ISerializationContext context, XFunctionTypeRef semanticObject) {
genericSequencer.createSequence(context, semanticObject);
}
/**
* Contexts:
* JvmTypeReference returns XFunctionTypeRef
* XFunctionTypeRef returns XFunctionTypeRef
* JvmArgumentTypeReference returns XFunctionTypeRef
*
* Constraint:
* ((paramTypes+=JvmTypeReference paramTypes+=JvmTypeReference*)? returnType=JvmTypeReference)
*/
protected void sequence_XFunctionTypeRef(ISerializationContext context, XFunctionTypeRef semanticObject) {
genericSequencer.createSequence(context, semanticObject);
}
/**
* Contexts:
* JvmSuperTypeReference returns XFunctionTypeRef
* XFunctionSuperTypeRef returns XFunctionTypeRef
*
* Constraint:
* ((instanceContext?='(' (paramTypes+=JvmTypeReference paramTypes+=JvmTypeReference*)?)? returnType=JvmTypeReference)
*/
protected void sequence_XFunctionSuperTypeRef(ISerializationContext context, XFunctionTypeRef semanticObject) {
genericSequencer.createSequence(context, semanticObject);
}
Result doVisitFunctionTypeReference(XFunctionTypeRef reference, Parameter param);
Result doVisitFunctionTypeReference(XFunctionTypeRef reference);