Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added rxjava-dynamic language-adaptor
* Adds an Object overload to Observable methods via code generation
  • Loading branch information
mattrjacobs committed Jul 4, 2013
commit d4efc294aff86d8752fdaef63d4afd52e3d50088
43 changes: 43 additions & 0 deletions language-adaptors/rxjava-dynamic/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
dependencies {
core project(':rxjava-core')
compile project(':rxjava-core')
compile project(':language-adaptors:codegen')
provided 'junit:junit-dep:4.10'
provided 'org.mockito:mockito-core:1.8.5'
}

task createAdaptedObservable(type: JavaExec) {
main = 'rx.codegen.ClassPathBasedRunner'
classpath = sourceSets.main.runtimeClasspath
args = ["Dynamic", outputDir]

inputs.files(sourceSets.main.runtimeClasspath)
outputs.dir(outputDir)
}

tasks.test {
dependsOn(createAdaptedObservable)

//Reorders the classpath so that the newly-create Observables win
classpath = createAdaptedObservable.outputs.files + sourceSets.test.runtimeClasspath
}

tasks.jar {
dependsOn(createAdaptedObservable)

from (zipTree(configurations.core.singleFile)) {
exclude "rx/Observable.class"
exclude "rx/observables/BlockingObservable.class"
}
from(outputDir)

exclude('**/*$UnitTest*')

manifest {
name = 'rxjava-dynamic'
instruction 'Bundle-Vendor', 'Netflix'
instruction 'Bundle-DocURL', 'https://github.com/Netflix/RxJava'
instruction 'Import-Package', '!org.junit,!junit.framework,!org.mockito.*,*'
instruction 'Fragment-Host', 'com.netflix.rxjava.core'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.lang.dynamic;

import rx.util.functions.Action0;
import rx.util.functions.Action1;
import rx.util.functions.Functions;

public class DynamicActionWrapper<T1> implements Action0, Action1<T1> {
private Object object;

public DynamicActionWrapper(Object object) {
this.object = object;
}

@Override
public void call() {
Functions.from(object).call();
}

@Override
public void call(T1 t1) {
Functions.from(object).call(t1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.lang.dynamic;

import java.util.HashMap;
import java.util.Map;

import rx.util.functions.FunctionLanguageAdaptor;

import java.util.HashSet;
import java.util.Set;

public class DynamicAdaptor implements FunctionLanguageAdaptor {

@Override
public Map<Class<?>, Class<?>> getFunctionClassRewritingMap() {
Map<Class<?>, Class<?>> m = new HashMap<Class<?>, Class<?>>();
m.put(Object.class, DynamicFunctionWrapper.class);
return m;
}

@Override
public Map<Class<?>, Class<?>> getActionClassRewritingMap() {
Map<Class<?>, Class<?>> m = new HashMap<Class<?>, Class<?>>();
m.put(Object.class, DynamicActionWrapper.class);
return m;
}

@Override
public Set<Class<?>> getAllClassesToRewrite() {
Set<Class<?>> dynamicClasses = new HashSet<Class<?>>();
dynamicClasses.add(Object.class);
return dynamicClasses;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.lang.dynamic;

import rx.util.functions.Func0;
import rx.util.functions.Func1;
import rx.util.functions.Func2;
import rx.util.functions.Func3;
import rx.util.functions.Func4;
import rx.util.functions.FunctionLanguageAdaptor;
import rx.util.functions.Functions;

public class DynamicFunctionWrapper<T1, T2, T3, T4, R> implements Func0<R>, Func1<T1, R>, Func2<T1, T2, R>, Func3<T1, T2, T3, R>, Func4<T1, T2, T3, T4, R> {
private Object object;

public DynamicFunctionWrapper(Object object) {
this.object = object;
}

@Override
public R call() {
return (R) Functions.from(object).call();
}

@Override
public R call(T1 t1) {
return (R) Functions.from(object).call(t1);
}

@Override
public R call(T1 t1, T2 t2) {
return (R) Functions.from(object).call(t1, t2);
}

@Override
public R call(T1 t1, T2 t2, T3 t3) {
return (R) Functions.from(object).call(t1, t2, t3);
}

@Override
public R call(T1 t1, T2 t2, T3 t3, T4 t4) {
return (R) Functions.from(object).call(t1, t2, t3, t4);
}
}
16 changes: 16 additions & 0 deletions rxjava-core/src/main/java/rx/util/functions/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@
*/
public class Functions {

@SuppressWarnings({ "unchecked", "rawtypes" })
public static FuncN from(final Object function) {
if (function == null) {
throw new RuntimeException("function is null. Can't send arguments to null function.");
}

/* check for typed Rx Function implementation first */
if (function instanceof Function) {
return fromFunction((Function) function);
} else if (function instanceof Action) {
return fromAction((Action) function);
}
// no support found
throw new RuntimeException("Unsupported closure type: " + function.getClass().getSimpleName());
}

@SuppressWarnings({ "unchecked", "rawtypes" })
private static FuncN fromFunction(Function function) {
// check Func* classes
Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ include 'rxjava-core', \
'language-adaptors:rxjava-jruby', \
'language-adaptors:rxjava-clojure', \
'language-adaptors:rxjava-scala', \
'language-adaptors:rxjava-dynamic', \
'language-adaptors:codegen', \
'rxjava-contrib:rxjava-swing'