Java Code Examples for org.eclipse.xtext.common.types.JvmOperation

The following examples show how to use org.eclipse.xtext.common.types.JvmOperation. These examples are extracted from open source projects. 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 Project: xtext-eclipse   Author: eclipse   File: AbstractTypeProviderTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testFindTypeByName_javaLangNumber_02() {
	String typeName = Number[][].class.getName();
	JvmArrayType type = (JvmArrayType) getTypeProvider().findTypeByName(typeName);
	JvmOperation longValue = (JvmOperation) type.eResource().getEObject("java.lang.Number.longValue()");
	assertNotNull(longValue);
	JvmDeclaredType number = longValue.getDeclaringType();
	assertNotNull(number.getArrayType());
	assertSame(type, number.getArrayType().getArrayType());
	assertNull(type.eGet(TypesPackage.Literals.JVM_COMPONENT_TYPE__ARRAY_TYPE));
	// array will created on the fly
	assertNotNull(type.getArrayType());
	diagnose(type);
	Resource resource = type.eResource();
	getAndResolveAllFragments(resource);
	recomputeAndCheckIdentifiers(resource);
}
 
Example #2
Source Project: xtext-extras   Author: eclipse   File: FeatureKinds.java    License: Eclipse Public License 2.0 6 votes vote down vote up
public static String getTypeName(JvmIdentifiableElement feature) {
	if (feature instanceof JvmFormalParameter) {
		return "parameter";
	}
	if (feature instanceof XVariableDeclaration) {
		return "local variable";
	}
	if (feature instanceof JvmEnumerationLiteral) {
		return "enum literal";
	}
	if (feature instanceof JvmField) {
		return "field";
	}
	if (feature instanceof JvmOperation) {
		return "method";
	}
	if (feature instanceof JvmConstructor) {
		return "constructor";
	}
	if (feature instanceof JvmType) {
		return "type";
	}
	throw new IllegalStateException();
}
 
Example #3
Source Project: xtext-xtend   Author: eclipse   File: LinkingTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testImplicitFirstArgument_03() throws Exception {
	XtendClass clazz = clazz(
			"import static extension test.ImplicitFirstArgumentStatics.*\n" +
			"class MyXtendClass {\n" + 
			"  def testExtensionMethods(CharSequence it) {\n" + 
			"    toLowerCase\n" + 
			"  }\n" +
			"  extension String" +
			"}");
	XtendFunction func= (XtendFunction) clazz.getMembers().get(0);
	
	XFeatureCall third = (XFeatureCall) ((XBlockExpression)func.getExpression()).getExpressions().get(0);
	JvmOperation thirdFeature = (JvmOperation) third.getFeature();
	assertEquals("java.lang.String.toLowerCase()", thirdFeature.getIdentifier());
	assertNull(third.getImplicitFirstArgument());
	XMemberFeatureCall thirdReceiver = (XMemberFeatureCall) third.getImplicitReceiver();
	assertTrue(thirdReceiver.getFeature() instanceof JvmField);
	assertNull(third.getInvalidFeatureIssueCode(), third.getInvalidFeatureIssueCode());
}
 
Example #4
Source Project: xtext-xtend   Author: eclipse   File: XtendHoverDocumentationProvider.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected void handleSuperMethodReferences(EObject context) {
	if (context instanceof XtendFunction) {
		XtendFunction function = (XtendFunction) context;
		if (function.isOverride()) {
			JvmOperation inferredOperation = associations.getDirectlyInferredOperation(function);
			if (inferredOperation != null) {
				JvmOperation overridden = overrideHelper.findOverriddenOperation(inferredOperation);
				if (overridden != null) {
					buffer.append("<div>"); //$NON-NLS-1$
					buffer.append("<b>"); //$NON-NLS-1$
					buffer.append("Overrides:"); //$NON-NLS-1$
					buffer.append("</b> "); //$NON-NLS-1$
					buffer.append(createMethodInTypeLinks(overridden));
					buffer.append("</div>"); //$NON-NLS-1$
				}
			}
		}
	}
}
 
Example #5
Source Project: sarl   Author: sarl   File: Utils.java    License: Apache License 2.0 6 votes vote down vote up
/** Analyzing the type hierarchy of the given interface and
 * extract hierarchy information.
 *
 * @param jvmElement - the element to analyze
 * @param operations - filled with the operations inside and inherited by the element.
 * @param fields - filled with the fields inside and inherited by the element.
 * @param sarlSignatureProvider - provider of tools related to action signatures.
 * @see OverrideHelper
 */
public static void populateInterfaceElements(
		JvmDeclaredType jvmElement,
		Map<ActionPrototype, JvmOperation> operations,
		Map<String, JvmField> fields,
		IActionPrototypeProvider sarlSignatureProvider) {
	for (final JvmFeature feature : jvmElement.getAllFeatures()) {
		if (!"java.lang.Object".equals(feature.getDeclaringType().getQualifiedName())) { //$NON-NLS-1$
			if (operations != null && feature instanceof JvmOperation) {
				final JvmOperation operation = (JvmOperation) feature;
				final ActionParameterTypes sig = sarlSignatureProvider.createParameterTypesFromJvmModel(
						operation.isVarArgs(), operation.getParameters());
				final ActionPrototype actionKey = sarlSignatureProvider.createActionPrototype(
						operation.getSimpleName(), sig);
				operations.put(actionKey, operation);
			} else if (fields != null && feature instanceof JvmField) {
				fields.put(feature.getSimpleName(), (JvmField) feature);
			}
		}
	}
}
 
Example #6
Source Project: xtext-extras   Author: eclipse   File: JvmModelGeneratorTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testAnnotation_1() {
  try {
    final XExpression expression = this.expression("42", false);
    final Procedure1<JvmAnnotationType> _function = (JvmAnnotationType it) -> {
      EList<JvmMember> _members = it.getMembers();
      final Procedure1<JvmOperation> _function_1 = (JvmOperation it_1) -> {
        this.builder.setBody(it_1, expression);
      };
      JvmOperation _method = this.builder.toMethod(expression, "theTruth", this.references.getTypeForName(int.class, expression), _function_1);
      this.builder.<JvmOperation>operator_add(_members, _method);
    };
    final JvmAnnotationType clazz = this.builder.toAnnotationType(expression, "my.test.Foo", _function);
    final Class<?> compiledClass = this.compile(expression.eResource(), clazz);
    Assert.assertTrue(compiledClass.isAnnotation());
    final Method method = IterableExtensions.<Method>head(((Iterable<Method>)Conversions.doWrapArray(compiledClass.getMethods())));
    Assert.assertEquals("theTruth", method.getName());
    Assert.assertEquals(Integer.valueOf(42), method.getDefaultValue());
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #7
Source Project: xtext-eclipse   Author: eclipse   File: AbstractTypeProviderTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test_ParameterizedTypes_Inner_07() {
	String typeName = ParameterizedTypes.Inner.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmOperation methodV = getMethodFromType(type, ParameterizedTypes.Inner.class, "methodZArray_01()");
	JvmTypeReference listZ = methodV.getReturnType();
	assertEquals("java.util.List<Z[][]>", listZ.getIdentifier());
	JvmParameterizedTypeReference listType = (JvmParameterizedTypeReference) listZ;
	assertEquals(1, listType.getArguments().size());
	JvmTypeReference typeArgument = listType.getArguments().get(0);
	JvmType argumentType = typeArgument.getType();
	assertTrue(argumentType instanceof JvmArrayType);
	assertTrue(((JvmArrayType) argumentType).getComponentType() instanceof JvmArrayType);
	JvmComponentType componentType = ((JvmArrayType) ((JvmArrayType) argumentType).getComponentType())
			.getComponentType();
	JvmTypeParameter z = type.getTypeParameters().get(2);
	assertSame(z, componentType);
}
 
Example #8
Source Project: xtext-extras   Author: eclipse   File: JvmModelGenerator.java    License: Eclipse Public License 2.0 6 votes vote down vote up
public ITreeAppendable generateModifier(final JvmMember it, final ITreeAppendable appendable, final GeneratorConfig config) {
  if (it instanceof JvmConstructor) {
    return _generateModifier((JvmConstructor)it, appendable, config);
  } else if (it instanceof JvmOperation) {
    return _generateModifier((JvmOperation)it, appendable, config);
  } else if (it instanceof JvmField) {
    return _generateModifier((JvmField)it, appendable, config);
  } else if (it instanceof JvmGenericType) {
    return _generateModifier((JvmGenericType)it, appendable, config);
  } else if (it instanceof JvmDeclaredType) {
    return _generateModifier((JvmDeclaredType)it, appendable, config);
  } else {
    throw new IllegalArgumentException("Unhandled parameter types: " +
      Arrays.<Object>asList(it, appendable, config).toString());
  }
}
 
Example #9
Source Project: xtext-xtend   Author: eclipse   File: DispatchHelper.java    License: Eclipse Public License 2.0 6 votes vote down vote up
public JvmOperation getDispatcherOperation(JvmOperation dispatchCase) {
	EObject sourceElement = associations.getPrimarySourceElement(dispatchCase);
	if (sourceElement instanceof XtendFunction) {
		XtendFunction function = (XtendFunction) sourceElement;
		if (function.isDispatch()) {
			Iterable<JvmOperation> operations = filter(associations.getJvmElements(sourceElement), JvmOperation.class);
			for(JvmOperation operation: operations) {
				if (Strings.equal(operation.getSimpleName(), function.getName())) {
					return operation;
				}
			}
		}
	} else {
		DispatchSignature signature = new DispatchSignature(dispatchCase.getSimpleName().substring(1), dispatchCase.getParameters().size());
		JvmOperation result = getDispatcherOperation(dispatchCase.getDeclaringType(), signature);
		return result;
	}
	return null;
}
 
Example #10
Source Project: xtext-extras   Author: eclipse   File: AbstractTypeProviderTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test_twoListWildcardsListResult_03() {
	JvmOperation twoListWildcardsListResult = getMethodFromParameterizedMethods(
			"twoListWildcardsListResult(java.util.List,java.util.List)");
	JvmTypeReference returnType = twoListWildcardsListResult.getReturnType();
	JvmParameterizedTypeReference parameterized = (JvmParameterizedTypeReference) returnType;
	assertEquals(1, parameterized.getArguments().size());
	JvmTypeReference typeParameter = parameterized.getArguments().get(0);
	assertTrue(typeParameter instanceof JvmWildcardTypeReference);
	JvmWildcardTypeReference wildcard = (JvmWildcardTypeReference) typeParameter;
	assertEquals(1, wildcard.getConstraints().size());
	assertTrue(wildcard.getConstraints().get(0) instanceof JvmUpperBound);
	JvmUpperBound upperBound = (JvmUpperBound) wildcard.getConstraints().get(0);
	assertNotNull(upperBound.getTypeReference());
	JvmType upperBoundType = upperBound.getTypeReference().getType();
	assertFalse(upperBoundType.eIsProxy());
	assertEquals("java.lang.Object", upperBoundType.getIdentifier());
}
 
Example #11
Source Project: xtext-extras   Author: eclipse   File: AbstractTypeProviderTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test_ParameterizedTypes_02() {
	String typeName = ParameterizedTypes.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmOperation methodS = getMethodFromType(type, ParameterizedTypes.class, "methodS(S)");
	JvmTypeReference listS = methodS.getReturnType();
	assertFalse(listS.toString(), listS.eIsProxy());
	assertEquals("java.util.List<? extends S>", listS.getIdentifier());
	JvmParameterizedTypeReference listType = (JvmParameterizedTypeReference) listS;
	assertEquals(1, listType.getArguments().size());
	JvmTypeReference typeArgument = listType.getArguments().get(0);
	assertTrue(typeArgument instanceof JvmWildcardTypeReference);
	JvmWildcardTypeReference wildcardTypeArgument = (JvmWildcardTypeReference) typeArgument;
	assertEquals("? extends S", wildcardTypeArgument.getIdentifier());
	assertEquals(1, wildcardTypeArgument.getConstraints().size());
	JvmUpperBound upperBound = (JvmUpperBound) wildcardTypeArgument.getConstraints().get(0);
	JvmTypeParameter s = type.getTypeParameters().get(0);
	assertSame(s, upperBound.getTypeReference().getType());
}
 
Example #12
Source Project: sarl   Author: sarl   File: Utils.java    License: Apache License 2.0 6 votes vote down vote up
/** Analyzing the type hierarchy of the given element, and
 * extract any type-related information.
 *
 * @param jvmElement - the element to analyze
 * @param finalOperations - filled with the final operations inherited by the element.
 * @param overridableOperations - filled with the oervrideable operations inherited by the element.
 * @param inheritedFields - filled with the fields inherited by the element.
 * @param operationsToImplement - filled with the abstract operations inherited by the element.
 * @param superConstructors - filled with the construstors of the super type.
 * @param sarlSignatureProvider - provider of tools related to action signatures.
 * @see OverrideHelper
 */
public static void populateInheritanceContext(
		JvmDeclaredType jvmElement,
		Map<ActionPrototype, JvmOperation> finalOperations,
		Map<ActionPrototype, JvmOperation> overridableOperations,
		Map<String, JvmField> inheritedFields,
		Map<ActionPrototype, JvmOperation> operationsToImplement,
		Map<ActionParameterTypes, JvmConstructor> superConstructors,
		IActionPrototypeProvider sarlSignatureProvider) {
	populateInheritanceContext(
			jvmElement,
			jvmElement.getExtendedClass(),
			jvmElement.getExtendedInterfaces(),
			finalOperations, overridableOperations, inheritedFields,
			operationsToImplement, superConstructors, sarlSignatureProvider);
}
 
Example #13
Source Project: sarl   Author: sarl   File: SarlJvmModelAssociations.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public XtendFunction getXtendFunction(JvmOperation jvmOperation) {
	XtendFunction fct = null;
	try {
		fct = super.getXtendFunction(jvmOperation);
	} catch (Throwable exception) {
		fct = null;
	}
	if (fct == null) {
		for (final EObject obj : getSourceElements(jvmOperation)) {
			if (obj instanceof XtendFunction) {
				fct = (XtendFunction) obj;
				break;
			}
		}
	}
	return fct;
}
 
Example #14
Source Project: xtext-xtend   Author: eclipse   File: JvmAnnotationReferenceImpl.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Override
public Expression getExpression(final String property) {
  final JvmOperation op = this.findOperation(property);
  final Function1<JvmAnnotationValue, Boolean> _function = (JvmAnnotationValue it) -> {
    return Boolean.valueOf((Objects.equal(it.getOperation(), op) || ((it.getOperation() == null) && Objects.equal(op.getSimpleName(), "value"))));
  };
  final JvmAnnotationValue annotationValue = IterableExtensions.<JvmAnnotationValue>findFirst(this.getDelegate().getValues(), _function);
  boolean _matched = false;
  if (annotationValue instanceof JvmCustomAnnotationValue) {
    _matched=true;
    EObject _head = IterableExtensions.<EObject>head(((JvmCustomAnnotationValue)annotationValue).getValues());
    final XExpression expression = ((XExpression) _head);
    if (((expression != null) && this.getCompilationUnit().isBelongedToCompilationUnit(expression))) {
      return this.getCompilationUnit().toExpression(expression);
    }
  }
  return null;
}
 
Example #15
Source Project: xtext-eclipse   Author: eclipse   File: AbstractTypeProviderTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test_twoListParamsNoResult_03() {
	JvmOperation twoListParamsNoResult = getMethodFromParameterizedMethods(
			"twoListParamsNoResult(java.util.List,java.util.List)");
	JvmFormalParameter firstParam = twoListParamsNoResult.getParameters().get(0);
	JvmTypeReference paramType = firstParam.getParameterType();
	JvmParameterizedTypeReference parameterized = (JvmParameterizedTypeReference) paramType;
	assertEquals(1, parameterized.getArguments().size());
	JvmTypeReference typeParameter = parameterized.getArguments().get(0);
	JvmType referencedType = typeParameter.getType();
	assertFalse(referencedType.eIsProxy());
	assertTrue(referencedType instanceof JvmTypeParameter);
	JvmTypeParameter typeVar = (JvmTypeParameter) referencedType;
	assertEquals("T", typeVar.getName());
	assertSame(twoListParamsNoResult, typeVar.getDeclarator());
}
 
Example #16
Source Project: xtext-eclipse   Author: eclipse   File: AbstractTypeProviderTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testMethods_defaultStaticMethod_01() {
	String typeName = Methods.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmOperation method = getMethodFromType(type, Methods.class, "defaultStaticMethod()");
	assertSame(type, method.getDeclaringType());
	assertFalse(method.isAbstract());
	assertFalse(method.isFinal());
	assertTrue(method.isStatic());
	assertFalse(method.isSynchronized());
	assertFalse(method.isStrictFloatingPoint());
	assertFalse(method.isNative());
	assertEquals(JvmVisibility.DEFAULT, method.getVisibility());
	JvmType methodType = method.getReturnType().getType();
	assertEquals("void", methodType.getIdentifier());
}
 
Example #17
Source Project: xtext-eclipse   Author: eclipse   File: AbstractTypeProviderTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test_twoListWildcardsNoResult_03() {
	JvmOperation twoListWildcardsNoResult = getMethodFromParameterizedMethods(
			"twoListWildcardsNoResult(java.util.List,java.util.List)");
	JvmFormalParameter firstParam = twoListWildcardsNoResult.getParameters().get(0);
	JvmTypeReference paramType = firstParam.getParameterType();
	JvmParameterizedTypeReference parameterized = (JvmParameterizedTypeReference) paramType;
	assertEquals(1, parameterized.getArguments().size());
	JvmTypeReference typeParameter = parameterized.getArguments().get(0);
	assertTrue(typeParameter instanceof JvmWildcardTypeReference);
	JvmWildcardTypeReference wildcard = (JvmWildcardTypeReference) typeParameter;
	assertEquals(1, wildcard.getConstraints().size());
	assertTrue(wildcard.getConstraints().get(0) instanceof JvmUpperBound);
	JvmUpperBound upperBound = (JvmUpperBound) wildcard.getConstraints().get(0);
	assertNotNull(upperBound.getTypeReference());
	JvmType upperBoundType = upperBound.getTypeReference().getType();
	assertFalse(upperBoundType.eIsProxy());
	assertEquals("java.lang.Object", upperBoundType.getIdentifier());
}
 
Example #18
Source Project: xtext-extras   Author: eclipse   File: ReferencedInvalidTypeFinder.java    License: Eclipse Public License 2.0 6 votes vote down vote up
protected LightweightTypeReference internalFindReferencedInvalidType(final JvmIdentifiableElement operation) {
  if (operation instanceof JvmOperation) {
    return _internalFindReferencedInvalidType((JvmOperation)operation);
  } else if (operation instanceof JvmExecutable) {
    return _internalFindReferencedInvalidType((JvmExecutable)operation);
  } else if (operation instanceof JvmField) {
    return _internalFindReferencedInvalidType((JvmField)operation);
  } else if (operation != null) {
    return _internalFindReferencedInvalidType(operation);
  } else if (operation == null) {
    return _internalFindReferencedInvalidType((Void)null);
  } else {
    throw new IllegalArgumentException("Unhandled parameter types: " +
      Arrays.<Object>asList(operation).toString());
  }
}
 
Example #19
Source Project: xtext-extras   Author: eclipse   File: XbaseCompiler.java    License: Eclipse Public License 2.0 6 votes vote down vote up
protected void _toJavaExpression(final XClosure closure, final ITreeAppendable b) {
	if (b.hasName(closure)) {
		b.trace(closure, false).append(getVarName(closure, b));
	} else {
		LightweightTypeReference type = getLightweightType(closure);
		JvmOperation operation = findImplementingOperation(type);
		if (operation != null) {
			GeneratorConfig config = b.getGeneratorConfig();
			if (config != null && config.getJavaSourceVersion().isAtLeast(JAVA8) && canCompileToJavaLambda(closure, type, operation)) {
				toLambda(closure, b.trace(closure, false), type, operation, true);
			} else {
				toAnonymousClass(closure, b.trace(closure, false), type, operation);
			}
		}
	}
}
 
Example #20
Source Project: xtext-xtend   Author: eclipse   File: InferredJvmModelTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testInferredFunction_04() throws Exception {
		XtendFile xtendFile = file("class Foo { def Iterable<CharSequence> create result: newArrayList(s) newList(String s) {} }");
		JvmGenericType inferredType = getInferredType(xtendFile);
		XtendClass xtendClass = (XtendClass) xtendFile.getXtendTypes().get(0);
		EList<JvmMember> jvmMembers = inferredType.getMembers();
		assertEquals(4, jvmMembers.size());
		JvmMember jvmMember = jvmMembers.get(1);
		assertTrue(jvmMember instanceof JvmOperation);
		XtendFunction xtendFunction = (XtendFunction) xtendClass.getMembers().get(0);
		assertEquals(xtendFunction.getName(), jvmMember.getSimpleName());
		assertEquals(JvmVisibility.PUBLIC, jvmMember.getVisibility());
		assertEquals("java.lang.Iterable<java.lang.CharSequence>", ((JvmOperation) jvmMember).getReturnType().getIdentifier());
		
		JvmField cacheVar = (JvmField) jvmMembers.get(2);
		assertEquals("_createCache_" + xtendFunction.getName(), cacheVar.getSimpleName());
		assertEquals(JvmVisibility.PRIVATE, cacheVar.getVisibility());
		assertEquals("java.util.HashMap<java.util.ArrayList<? extends java.lang.Object>, java.lang.Iterable<java.lang.CharSequence>>", cacheVar.getType().getIdentifier());
		
		JvmOperation privateInitializer = (JvmOperation) jvmMembers.get(3);
		assertEquals("_init_"+xtendFunction.getName(), privateInitializer.getSimpleName());
		assertEquals(JvmVisibility.PRIVATE, privateInitializer.getVisibility());
//		This used to be a bogus assertion since Iterable<CharSequence> is not assignable from ArrayList<String>
//		assertEquals("java.util.ArrayList<java.lang.String>", privateInitializer.getParameters().get(0).getParameterType().getIdentifier());
		assertEquals("java.util.ArrayList<java.lang.CharSequence>", privateInitializer.getParameters().get(0).getParameterType().getIdentifier());
		assertEquals("java.lang.String", privateInitializer.getParameters().get(1).getParameterType().getIdentifier());
	}
 
Example #21
Source Project: xtext-xtend   Author: eclipse   File: XtendHoverSignatureProvider.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected String getSimpleSignature(EObject container) {
	if(container instanceof XtendFunction){
		XtendFunction function = (XtendFunction) container;
		JvmOperation inferredOperation = associations.getDirectlyInferredOperation(function);
		if (inferredOperation != null) {
			return function.getName() + uiStrings.parameters(inferredOperation);
		}
	}
	else if (container instanceof XtendConstructor){
		XtendConstructor constructor = (XtendConstructor) container;
		XtendClass xtendClazz = EcoreUtil2.getContainerOfType(constructor, XtendClass.class);
		JvmConstructor inferredConstructor = associations.getInferredConstructor(constructor);
		return xtendClazz.getName() + " " + uiStrings.parameters(inferredConstructor);
	}
	return super.getSimpleSignature(container);
}
 
Example #22
Source Project: xtext-extras   Author: eclipse   File: AbstractTypeProviderTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testMethods_privateSynchronizedMethod_01() {
	String typeName = Methods.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmOperation method = getMethodFromType(type, Methods.class, "privateSynchronizedMethod()");
	assertSame(type, method.getDeclaringType());
	assertFalse(method.isAbstract());
	assertFalse(method.isFinal());
	assertFalse(method.isStatic());
	assertTrue(method.isSynchronized());
	assertFalse(method.isStrictFloatingPoint());
	assertFalse(method.isNative());
	assertEquals(JvmVisibility.PRIVATE, method.getVisibility());
	JvmType methodType = method.getReturnType().getType();
	assertEquals("void", methodType.getIdentifier());
}
 
Example #23
Source Project: xtext-extras   Author: eclipse   File: ResolvedOperationTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testListToArrayHasTwoOrThreeCandidates() {
  final IResolvedOperation operation = this.toOperation("(null as java.util.List<String>).toArray(null)");
  final List<JvmOperation> candidates = operation.getOverriddenAndImplementedMethodCandidates();
  final Function1<JvmOperation, String> _function = (JvmOperation it) -> {
    return it.getIdentifier();
  };
  final String message = IterableExtensions.join(ListExtensions.<JvmOperation, String>map(candidates, _function), ", ");
  try {
    Assert.assertEquals(message, 2, candidates.size());
  } catch (final Throwable _t) {
    if (_t instanceof AssertionError) {
      Assert.assertEquals(message, 3, candidates.size());
    } else {
      throw Exceptions.sneakyThrow(_t);
    }
  }
}
 
Example #24
Source Project: xtext-extras   Author: eclipse   File: FunctionTypes.java    License: Eclipse Public License 2.0 6 votes vote down vote up
public Map<JvmTypeParameter, List<LightweightBoundTypeArgument>> getFunctionTypeParameterMapping(
		LightweightTypeReference functionType, JvmOperation operation,
		ActualTypeArgumentCollector typeArgumentCollector, ITypeReferenceOwner owner) {
	/* 
	 * The mapping is populated by means of the function type to declarator mapping, though a method
	 * 
	 * m(Zonk zonk) { .. }
	 * 
	 * with
	 * 
	 * interface Foo<X> {
	 *   void bar(X x);
	 * }
	 * interface Zonk extends Foo<CharSequence> {}
	 * 
	 * infers the parameter type CharSequence for the lamba param
	 */
	LightweightTypeReference lightweightTypeReference = owner.toLightweightTypeReference(operation.getDeclaringType());
	typeArgumentCollector.populateTypeParameterMapping(lightweightTypeReference, functionType);
	Map<JvmTypeParameter, List<LightweightBoundTypeArgument>> typeParameterMapping = typeArgumentCollector.rawGetTypeParameterMapping();
	return typeParameterMapping;
}
 
Example #25
Source Project: sarl   Author: sarl   File: AbstractNewSarlElementWizardPage.java    License: Apache License 2.0 6 votes vote down vote up
private static void createInfoCall(IExpressionBuilder builder, String message) {
	final JvmParameterizedTypeReference capacity = builder.newTypeRef(null, LOGGING_CAPACITY_NAME);
	final String objectType = Object.class.getName();
	final String objectArrayType = objectType + "[]"; //$NON-NLS-1$
	final JvmOperation infoMethod = Iterables.find(
			((JvmDeclaredType) capacity.getType()).getDeclaredOperations(), it -> {
			if (Objects.equals(it.getSimpleName(), "info") //$NON-NLS-1$
					&& it.getParameters().size() == 2) {
				final String type1 = it.getParameters().get(0).getParameterType().getIdentifier();
				final String type2 = it.getParameters().get(1).getParameterType().getIdentifier();
				return Objects.equals(objectType, type1) && Objects.equals(objectArrayType, type2);
			}
			return false;
		},
		null);
	if (infoMethod != null) {
		builder.setExpression("info(\"" + message + "\")"); //$NON-NLS-1$ //$NON-NLS-2$
		((XFeatureCall) builder.getXExpression()).setFeature(infoMethod);
	}
}
 
Example #26
Source Project: xtext-web   Author: eclipse   File: EntitiesValidator.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Check
public void checkOperationNamesAreUnique(Entity entity) {
	JvmGenericType inferredJavaClass = IterableExtensions
			.head(Iterables.filter(jvmModelAssociations.getJvmElements(entity), JvmGenericType.class));
	Multimap<String, JvmOperation> signature2Declarations = HashMultimap.create();
	overrideHelper.getResolvedFeatures(inferredJavaClass).getDeclaredOperations().forEach((it) -> {
		signature2Declarations.put(it.getResolvedErasureSignature(), it.getDeclaration());
	});
	signature2Declarations.asMap().values().forEach((jvmOperations) -> {
		if (jvmOperations.size() > 1) {
			Iterables
					.filter(IterableExtensions.map(jvmOperations,
							(op) -> jvmModelAssociations.getPrimarySourceElement(op)), Operation.class)
					.forEach((op) -> {
						error("Duplicate operation " + op.getName(), op, DomainmodelPackage.Literals.FEATURE__NAME, IssueCodes.DUPLICATE_OPERATION);
					});
		}
	});
}
 
Example #27
Source Project: xtext-extras   Author: eclipse   File: AbstractTypeProviderTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testMethods_publicAbstractMethod_01() {
	String typeName = Methods.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmOperation method = getMethodFromType(type, Methods.class, "publicAbstractMethod()");
	assertSame(type, method.getDeclaringType());
	assertTrue(method.isAbstract());
	assertFalse(method.isFinal());
	assertFalse(method.isStatic());
	assertFalse(method.isSynchronized());
	assertFalse(method.isStrictFloatingPoint());
	assertFalse(method.isNative());
	assertEquals(JvmVisibility.PUBLIC, method.getVisibility());
	JvmType methodType = method.getReturnType().getType();
	assertEquals("void", methodType.getIdentifier());
}
 
Example #28
Source Project: xtext-extras   Author: eclipse   File: AbstractTypeProviderTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void test_twoListParamsListResult_02() {
	JvmOperation twoListParamsListResult = getMethodFromParameterizedMethods(
			"twoListParamsListResult(java.util.List,java.util.List)");
	JvmTypeReference returnType = twoListParamsListResult.getReturnType();
	assertNotNull(returnType);
	assertEquals("java.util.List<T>", returnType.getIdentifier());
	assertTrue(returnType instanceof JvmParameterizedTypeReference);
	JvmParameterizedTypeReference parameterized = (JvmParameterizedTypeReference) returnType;
	JvmType rawType = parameterized.getType();
	assertFalse(rawType.eIsProxy());
	assertEquals("java.util.List", rawType.getIdentifier());
}
 
Example #29
Source Project: sarl   Author: sarl   File: SARLDescriptionLabelProviderTest.java    License: Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #30
Source Project: xtext-extras   Author: eclipse   File: Proxies.java    License: Eclipse Public License 2.0 5 votes vote down vote up
protected JvmOperation createMethodProxy(BinaryTypeSignature type, String methodName) {
	BinarySimpleMemberSignature method = type.appendMethod(methodName);
	JvmOperation proxy = operationProxies.get(method);
	if (proxy == null) {
		proxy = TypesFactory.eINSTANCE.createJvmOperation();
		URI uri = method.getURI();
		((InternalEObject) proxy).eSetProxyURI(uri);
		operationProxies.put(method, proxy);
	}
	return proxy;
}