项目:xtext-extras
文件:XFunctionTypeRefs.java
public static String buildUri(final boolean procedure,final int functionParamCount) {
final int paramCount = Math.min(6,functionParamCount);
if (procedure) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("java:/Objects/");
String _canonicalName = Procedures.class.getCanonicalName();
_builder.append(_canonicalName);
_builder.append("#");
String _canonicalName_1 = Procedures.class.getCanonicalName();
_builder.append(_canonicalName_1);
_builder.append("$Procedure");
_builder.append(paramCount);
return _builder.toString();
}
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("java:/Objects/");
String _canonicalName_2 = Functions.class.getCanonicalName();
_builder_1.append(_canonicalName_2);
_builder_1.append("#");
String _canonicalName_3 = Functions.class.getCanonicalName();
_builder_1.append(_canonicalName_3);
_builder_1.append("$Function");
_builder_1.append(paramCount);
return _builder_1.toString();
}
@Test public void testFindFirst_exceptionInFilter() {
final RuntimeException expectedException = new RuntimeException();
Function1<Integer,Boolean> filter = new Functions.Function1<Integer,Boolean>() {
@Override
public Boolean apply(Integer p) {
throw expectedException;
}
};
for(IterableOrIterator testMe: testData(first,second,third)) {
try {
findFirst(testMe,filter);
fail("expected exception");
} catch(RuntimeException e) {
assertSame(expectedException,e);
}
}
}
@Test public void testFindLast_exceptionInFilter() {
final RuntimeException expectedException = new RuntimeException();
Function1<Integer,third)) {
try {
findLast(testMe,e);
}
}
}
项目:xtext-extras
文件:ClosureClient2.java
public ClosureClient2(Functions.Function0<String>... functions) {
StringBuilder builder = new StringBuilder("varargs:");
for(Functions.Function0<String> function: functions) {
builder.append(function.apply());
}
value = builder.toString();
}
项目:xtext-extras
文件:ClosureClient.java
public <Obj> Functions.Function1<Obj,Obj> getIdentityFunction() {
return new Functions.Function1<Obj,Obj>() {
@Override
public Obj apply(Obj p) {
return p;
}
};
}
项目:xtext-extras
文件:ClosureClient.java
/**
* @since 2.3
*/
public String concatStrings(Functions.Function0<String>... functions) {
StringBuilder result = new StringBuilder("varargs:");
for(Functions.Function0<String> function: functions) {
result.append(function.apply());
}
return result.toString();
}
项目:xtext-extras
文件:ClosureClient.java
/**
* @since 2.3
*/
public String concatStrings(Functions.Function0<String> function1,Functions.Function0<String> function2) {
StringBuilder result = new StringBuilder("twoArgs:");
result.append(function1.apply());
result.append(function2.apply());
return result.toString();
}
项目:xtext-extras
文件:OnTheFlyJavaCompiler.java
@SuppressWarnings("unchecked")
public <RT,T1,T2> Functions.Function2<T1,T2,RT> createFunction(
String body,Class<RT> returnType,Class<T1> paramType1,Class<T2> paramType2) {
return (Functions.Function2<T1,RT>) internalCreateFunction(body,returnType,Tuples.pair((Type) paramType1,"p1"),Tuples.pair((Type) paramType2,"p2"));
}
项目:xtext-extras
文件:FunctionTypes.java
private Class<?> loadFunctionClass(String simpleFunctionName,boolean procedure) {
try {
if (!procedure) {
return Functions.class.getClassLoader().loadClass(
Functions.class.getCanonicalName() + "$" + simpleFunctionName);
} else {
return Procedures.class.getClassLoader().loadClass(
Procedures.class.getCanonicalName() + "$" + simpleFunctionName);
}
} catch (ClassNotFoundException e) {
throw new WrappedException(e);
}
}
项目:xtext-extras
文件:FunctionTypes.java
public FunctionTypeKind getFunctionTypeKind(ParameterizedTypeReference typeReference) {
JvmType type = typeReference.getType();
if (type.eClass() == TypesPackage.Literals.JVM_GENERIC_TYPE) {
JvmDeclaredType outerType = ((JvmGenericType) type).getDeclaringType();
if (outerType != null) {
if (Procedures.class.getName().equals(outerType.getIdentifier())) {
return FunctionTypeKind.PROCEDURE;
}
if (Functions.class.getName().equals(outerType.getIdentifier())) {
return FunctionTypeKind.FUNCTION;
}
}
}
return FunctionTypeKind.NONE;
}
项目:xtext-extras
文件:XbaseInterpreter.java
protected Object _doEvaluate(XClosure closure,IEvaluationContext context,CancelIndicator indicator) {
Class<?> functionIntf = null;
switch (closure.getFormalParameters().size()) {
case 0:
functionIntf = getClass(Functions.Function0.class);
break;
case 1:
functionIntf = getClass(Functions.Function1.class);
break;
case 2:
functionIntf = getClass(Functions.Function2.class);
break;
case 3:
functionIntf = getClass(Functions.Function3.class);
break;
case 4:
functionIntf = getClass(Functions.Function4.class);
break;
case 5:
functionIntf = getClass(Functions.Function5.class);
break;
case 6:
functionIntf = getClass(Functions.Function6.class);
break;
default:
throw new IllegalStateException("Closures with more then 6 parameters are not supported.");
}
ClosureInvocationHandler invocationHandler = new ClosureInvocationHandler(closure,context,this,indicator);
Object proxy = Proxy.newProxyInstance(classLoader,new Class<?>[] { functionIntf },invocationHandler);
return proxy;
}
项目:xtext-extras
文件:EvaluationCompilerTest.java
protected Functions.Function0<Object> compile(String code) {
String javaCode = compiletoJavaCode(code);
try {
Function0<Object> function = javaCompiler.createFunction(javaCode,Object.class);
return function;
} catch (Exception e) {
throw new RuntimeException("Java compilation Failed. Java code was : \n" + javaCode,e);
}
}
项目:xtext-extras
文件:XbaseExpectedTypeProviderTest.java
@Test public void testTypeParamInference_04() throws Exception {
XBlockExpression block = (XBlockExpression) expression("{ var Integer i = new testdata.ClosureClient().invoke1([e|null],'foo') }");
XVariableDeclaration var = (XVariableDeclaration) block.getExpressions().get(0);
XMemberFeatureCall fc = (XMemberFeatureCall) var.getRight();
final XExpression closure = fc.getMemberCallArguments().get(0);
assertExpected(Functions.class.getCanonicalName()+"$Function1<java.lang.String,java.lang.Integer>",closure);
}
项目:xtext-extras
文件:XbaseExpectedTypeProviderTest.java
@Test
public void testTypeParamInference_07() throws Exception {
XBlockExpression block = (XBlockExpression) expression("{ var this = new testdata.ClosureClient() invoke1([ String e|e.length],'foo') }");
XFeatureCall fc = (XFeatureCall) block.getExpressions().get(1);
final XExpression closure = fc.getFeatureCallArguments().get(0);
assertExpected(Functions.class.getCanonicalName()+"$Function1<java.lang.String,closure);
}
项目:xtext-extras
文件:XbaseExpectedTypeProviderTest.java
@Test
public void testTypeParamInference_07_b() throws Exception {
XBlockExpression block = (XBlockExpression) expression("{ var this = new testdata.ClosureClient() invoke1([ e|e.length],closure);
}
项目:xtext-extras
文件:XbaseExpectedTypeProviderTest.java
@Test public void testTypeParamInference_09() throws Exception {
XBlockExpression block = (XBlockExpression) expression("{ var this = new testdata.ClosureClient() var Integer i = invoke1([e|null],'foo') }");
XVariableDeclaration var = (XVariableDeclaration) block.getExpressions().get(1);
XFeatureCall fc = (XFeatureCall) var.getRight();
final XExpression closure = fc.getFeatureCallArguments().get(0);
assertExpected(Functions.class.getCanonicalName()+"$Function1<java.lang.String,closure);
}
项目:xtext-extras
文件:AbstractXbaseLinkingTest.java
@Test public void testRecursiveClosure_02() throws Exception {
XBlockExpression block = (XBlockExpression) expression("{ val (int)=>int fun = [ self.apply(it) ] }");
XVariableDeclaration variable = (XVariableDeclaration) block.getExpressions().get(0);
XClosure closure = (XClosure) variable.getRight();
XBlockExpression body = (XBlockExpression) closure.getExpression();
XMemberFeatureCall member = (XMemberFeatureCall) body.getExpressions().get(0);
XFeatureCall recursive = (XFeatureCall) member.getMemberCallTarget();
assertEquals(Functions.Function1.class.getName(),recursive.getFeature().getQualifiedname('$'));
}
项目:xtext-extras
文件:OnTheFlyJavaCompilerTest.java
项目:xtext-extras
文件:OnTheFlyJavaCompilerTest.java
@SuppressWarnings("unchecked")
@Test public void testCompiletoClass() throws Exception {
Class<?> class1 = compiler.compiletoClass("Foo","public class Foo implements "+Functions.Function0.class.getCanonicalName()+"<String> {" +
" public String apply() {\n" +
" return \"foo\";\n" +
" }" +
"}");
assertEquals("foo",((Functions.Function0<String>)class1.newInstance()).apply());
}
@Test public void testFlatMap () {
ArrayList<String> list = newArrayList("foo","bar");
final Functions.Function1<String,Iterator<String>> function = new Functions.Function1<String,Iterator<String>>() {
@Override
public Iterator<String> apply(String p) {
return newArrayList("Hello",p).iterator();
}
};
assertEquals(newArrayList("Hello","foo","Hello","bar"),newArrayList(IteratorExtensions.flatMap(list.iterator(),function)));
}
@Test public void testSortBy() throws Exception {
List<? extends CharSequence> list = newArrayList("foo","bar","baz");
List<? extends CharSequence> sorted = IterableExtensions.sortBy(list,new Functions.Function1<CharSequence,String>() {
@Override
public String apply(CharSequence p) {
return p.toString();
}
});
assertNotSame(list,sorted);
assertEquals(sorted,newArrayList("bar","baz","foo"));
}
@Test public void testJoinWithBeforeAndAfter() throws Exception {
ArrayList<String> list = newArrayList("foo","bar");
ArrayList<String> singletonList = newArrayList("foo");
ArrayList<String> emptylist = new ArrayList<String>();
final Functions.Function1<String,String> function = new Functions.Function1<String,String>() {
@Override
public String apply(String p) {
return p;
}
};
assertEquals("<foo,bar>",IterableExtensions.join(list,"<",",">",function));
assertEquals("<foo>",IterableExtensions.join(singletonList,function));
assertEquals("",IterableExtensions.join(emptylist,function));
assertEquals("foo,null,function));
assertEquals("foo>",function));
assertEquals("<foobar>",function));
assertEquals("<foo,bar",function));
assertEquals("<foo",function));
}
@Test public void testFlatMap () {
ArrayList<String> list = newArrayList("foo",Iterable<String>> function = new Functions.Function1<String,Iterable<String>>() {
@Override
public Iterable<String> apply(String p) {
return newArrayList("Hello",p);
}
};
assertEquals(newArrayList("Hello",newArrayList(IterableExtensions.flatMap(list,function)));
}
@Test public void testFindFirst_exceptionInFilter_emptyinput() {
final RuntimeException expectedException = new RuntimeException();
Function1<Integer,Boolean>() {
@Override
public Boolean apply(Integer p) {
throw expectedException;
}
};
for(IterableOrIterator testMe: testData()) {
assertNull(findFirst(testMe,filter));
}
}
@Test public void testFindLast_nomatch() {
Function1<Integer,third)) {
Integer last = findLast(testMe,last);
}
}
@Test public void testFindLast_allMatches() {
Function1<Integer,filter);
assertEquals(third,last);
}
}
@Test public void testFindLast_onematch() {
Function1<Integer,last);
}
}
@Test public void testFindLast_exceptionInFilter_emptyinput() {
final RuntimeException expectedException = new RuntimeException();
Function1<Integer,Boolean>() {
@Override
public Boolean apply(Integer p) {
throw expectedException;
}
};
for(IterableOrIterator testMe: testData()) {
Integer last = findLast(testMe,filter);
assertEquals(null,last);
}
}
@Test public void testFindLast_NPE_noinput() {
try {
findLast(null,Boolean>() {
@Override
public Boolean apply(Integer p) {
return true;
}
});
fail("Expected NPE");
} catch(NullPointerException npe) {
// expected
}
}
项目:xtext-core
文件:AntlrGrammarGenUtil.java