-
Notifications
You must be signed in to change notification settings - Fork 7.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
typesafe-core #319
Closed
Closed
typesafe-core #319
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d61584e
Make rxjava-core typesafe
mattrjacobs b0291ac
Add Code generator that adapts the core Observable to a dynamic langu…
mattrjacobs 08b7757
Reworked Scala adaptor to use implicits in RxImplicits, rather than c…
mattrjacobs 6d15ac6
Miscellaneous cleanup:
mattrjacobs 26d116d
Gradle fixes/optimizations
benjchristensen afc81d0
Adding support for the special case of subscribe(Map) to code-generation
mattrjacobs 598a94f
Added a special case to bytecode rewriting for methods which would ot…
mattrjacobs 9577bde
Re-adding UnitTests to Jar generation (except for rxjava-core). This…
mattrjacobs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
apply plugin: 'java' | ||
apply plugin: 'groovy' | ||
apply plugin: 'eclipse' | ||
apply plugin: 'idea' | ||
apply plugin: 'osgi' | ||
|
||
dependencies { | ||
compile project(':rxjava-core') | ||
compile 'org.javassist:javassist:3.17.1-GA' | ||
|
||
provided 'junit:junit:4.10' | ||
} | ||
|
||
jar { | ||
manifest { | ||
name = 'rxjava-codegen' | ||
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' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
Here's the example source of Observable (heavily elided): | ||
|
||
public class rx.Observable { | ||
public static <T> Observable<T> create(Func1<Observer<T>, Subscription> func) { | ||
return new Observable<T>(func); | ||
} | ||
|
||
public static <T> Observable<T> create(Object func) { | ||
... | ||
} | ||
|
||
public static <T> Observable<T> take(final Observable<T> items, final int num) { | ||
return create(OperationTake.take(items, num)); | ||
} | ||
|
||
public static <T> Observable<T> takeWhile(final Observable<T> items, Func1<T, Boolean> predicate) { | ||
return create(OperationTakeWhile.takeWhile(items, predicate)); | ||
} | ||
|
||
public Observable<T> filter(Func1<T, Boolean> predicate) { | ||
return filter(this, predicate); | ||
} | ||
|
||
public static <T> Observable<T> filter(Observable<T> that, Func1<T, Boolean> predicate) { | ||
return create(OperationFilter.filter(that, predicate)); | ||
} | ||
} | ||
|
||
Groovy-friendly version adds: | ||
|
||
public class rx.Observable { | ||
public static <T> rx.Observable<T> create(groovy.lang.Closure func) { | ||
return create(new GroovyFunctionAdaptor(func)); | ||
} | ||
|
||
public static <T> rx.Observable<T> takeWhile(final Observable<T> items, groovy.lang.Closure predicate) { | ||
return takeWhile(items, new GroovyFunctionAdaptor(predicate)); | ||
} | ||
|
||
public rx.Observable<T> filter(groovy.lang.Closure predicate) { | ||
return filter(new GroovyFunctionAdaptor(predicate)); | ||
} | ||
|
||
public static <T> rxObservable<T> filter(Observable<T> that, groovy.lang.Closure predicate) { | ||
return filter(that, new GroovyFunctionAdaptor(predicate)); | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
language-adaptors/codegen/src/main/java/rx/codegen/AddSpecializedDynamicMethodRewriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* 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.codegen; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import javassist.CtClass; | ||
import javassist.CtMethod; | ||
import javassist.Modifier; | ||
|
||
import rx.util.functions.FunctionLanguageAdaptor; | ||
|
||
/** | ||
* An implementation of method rewriting that leaves the rx.Function variant alone and adds dynamic language | ||
* support by adding a new method per native function class. The new methods do nothing more than convert the | ||
* native function class into an rx.Function and forward to the core implementation. | ||
* | ||
* For example, if {@code Observable} has a method Integer foo(String s, Func1<A, B> f, Integer i), then the | ||
* rewritten class will add more 'foo's with dynamic language support. | ||
* | ||
* For Groovy (as an example), the new class would contain: | ||
* Integer foo(String s, Func1<A, B> f, Integer i); | ||
* Integer foo(String s, Closure f, Integer i) { | ||
* return foo(s, new GroovyFunctionWrapper(f), i); | ||
* } | ||
*/ | ||
public class AddSpecializedDynamicMethodRewriter extends MethodRewriter { | ||
|
||
public AddSpecializedDynamicMethodRewriter(CtClass enclosingClass, CtMethod method, FunctionLanguageAdaptor adaptor) { | ||
this.enclosingClass = enclosingClass; | ||
this.initialMethod = method; | ||
this.adaptor = adaptor; | ||
} | ||
|
||
@Override | ||
public boolean needsRewrite() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isReplacement() { | ||
return false; | ||
} | ||
|
||
@Override | ||
protected List<MethodRewriteRequest> getNewMethodsToRewrite(CtClass[] initialArgTypes) { | ||
return duplicatedMethodsWithWrappedFunctionTypes(); | ||
} | ||
|
||
@Override | ||
protected String getRewrittenMethodBody(MethodRewriteRequest req) { | ||
StringBuffer newBody = new StringBuffer(); | ||
List<String> argumentList = new ArrayList<String>(); | ||
newBody.append("{ return "); | ||
if (Modifier.isStatic(initialMethod.getModifiers())) { | ||
newBody.append(enclosingClass.getName() + "."); | ||
} else { | ||
newBody.append("this."); | ||
} | ||
newBody.append(initialMethod.getName()); | ||
newBody.append("("); | ||
try { | ||
for (int i = 0; i < initialMethod.getParameterTypes().length; i++) { | ||
CtClass argType = initialMethod.getParameterTypes()[i]; | ||
if (isRxActionType(argType) && req.getActionAdaptorClass() != null) { | ||
argumentList.add(getAdaptedArg(req.getActionAdaptorClass(), i + 1)); | ||
} else if (isRxFunctionType(argType) && req.getFunctionAdaptorClass() != null) { | ||
argumentList.add(getAdaptedArg(req.getFunctionAdaptorClass(), i + 1)); | ||
} else { | ||
argumentList.add(getUntouchedArg(i + 1)); | ||
} | ||
} | ||
} catch (Exception ex) { | ||
System.out.println("Exception while creating body for dynamic version of : " + initialMethod.getName()); | ||
} | ||
newBody.append(makeArgList(argumentList)); | ||
newBody.append(")"); | ||
newBody.append(";}"); | ||
return newBody.toString(); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
language-adaptors/codegen/src/main/java/rx/codegen/ClassPathBasedRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* 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.codegen; | ||
|
||
import java.io.File; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import javassist.ClassPool; | ||
import javassist.CtClass; | ||
|
||
import rx.util.functions.FunctionLanguageAdaptor; | ||
|
||
/** | ||
* Java Executable that performs bytecode rewriting at compile-time. | ||
* It accepts 2 args: dynamic language and dir to store result output classfiles | ||
*/ | ||
public class ClassPathBasedRunner { | ||
public static void main(String[] args) { | ||
if (args.length != 2) { | ||
System.out.println("Usage : Expects 2 args: (Language, File to place classfiles)"); | ||
System.out.println("Currently supported languages: Groovy/Clojure/JRuby"); | ||
System.exit(1); | ||
} | ||
String lang = args[0]; | ||
File dir = new File(args[1]); | ||
System.out.println("Using dir : " + dir + " for outputting classfiles"); | ||
System.out.println("Looking for Adaptor for : " + lang); | ||
String className = "rx.lang." + lang.toLowerCase() + "." + lang + "Adaptor"; | ||
try { | ||
ClassPool pool = ClassPool.getDefault(); | ||
|
||
Class<?> adaptorClass = Class.forName(className); | ||
System.out.println("Found Adaptor : " + adaptorClass); | ||
FunctionLanguageAdaptor adaptor = (FunctionLanguageAdaptor) adaptorClass.newInstance(); | ||
|
||
Func1Generator func1Generator = new Func1Generator(pool, adaptor); | ||
CtClass dynamicFunc1Class = func1Generator.createDynamicFunc1Class(); | ||
writeClassFile(dynamicFunc1Class, dir); | ||
pool.appendPathList(dir.getCanonicalPath()); | ||
|
||
ObservableRewriter rewriter = new ObservableRewriter(pool, adaptor); | ||
for (Class<?> observableClass: getObservableClasses()) { | ||
CtClass rewrittenClass = rewriter.addMethods(observableClass); | ||
writeClassFile(rewrittenClass, dir); | ||
} | ||
} catch (ClassNotFoundException ex) { | ||
System.out.println("Did not find adaptor class : " + className); | ||
System.exit(1); | ||
} catch (InstantiationException ex) { | ||
System.out.println("Reflective constuctor on : " + className + " failed"); | ||
System.exit(1); | ||
} catch (IllegalAccessException ex) { | ||
System.out.println("Access to constructor on : " + className + " failed"); | ||
System.exit(1); | ||
} catch (Exception ex) { | ||
System.out.println("Exception : " + ex.getMessage()); | ||
System.exit(1); | ||
} | ||
} | ||
|
||
protected static void writeClassFile(CtClass clazz, File dir) { | ||
try { | ||
System.out.println("Using " + dir.getCanonicalPath() + " for dynamic class file"); | ||
clazz.writeFile(dir.getCanonicalPath()); | ||
} catch (java.io.IOException ioe) { | ||
System.out.println("Could not write classfile to : " + dir.toString()); | ||
System.exit(1); | ||
} catch (javassist.CannotCompileException cce) { | ||
System.out.println("Could not create a valid classfile"); | ||
System.exit(2); | ||
} | ||
} | ||
|
||
private static List<Class<?>> getObservableClasses() { | ||
List<Class<?>> observableClasses = new ArrayList<Class<?>>(); | ||
observableClasses.add(rx.Observable.class); | ||
observableClasses.add(rx.observables.BlockingObservable.class); | ||
return observableClasses; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this wrap-and-throw rather than printing and ignoring the exception?