|
| 1 | +/** |
| 2 | + * Copyright 2014 Netflix, Inc. |
| 3 | + * <p> |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * <p> |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * <p> |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package rx.quasar; |
| 17 | + |
| 18 | +import co.paralleluniverse.fibers.instrument.MethodDatabase; |
| 19 | +import co.paralleluniverse.fibers.instrument.SimpleSuspendableClassifier; |
| 20 | +import co.paralleluniverse.fibers.instrument.SuspendableClassifier; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.HashSet; |
| 23 | +import java.util.Set; |
| 24 | + |
| 25 | +public class RxSuspendableClassifier implements SuspendableClassifier { |
| 26 | + private static final Set<String> CORE_PACKAGES = new HashSet<String>(Arrays.asList(new String[]{ |
| 27 | + "rx", "rx.joins", "rx.observables", "rx.observers", "rx.operators", "rx.plugins", "rx.schedulers", |
| 28 | + "rx.subjects", "rx.subscriptions", "rx.util", "rx.util.functions" |
| 29 | + })); |
| 30 | + |
| 31 | + private static final Set<String> EXCEPTIONS = new HashSet<String>(Arrays.asList(new String[]{ |
| 32 | + "rx/observers/SynchronizedObserver", |
| 33 | + "rx/schedulers/AbstractSchedulerTests$ConcurrentObserverValidator",})); |
| 34 | + |
| 35 | + private static final Set<String> OBSERVER_METHODS = new HashSet<String>(Arrays.asList(new String[]{ |
| 36 | + "onNext(Ljava/lang/Object;)V", "onCompleted()V", "onError(Ljava/lang/Throwable;)V" |
| 37 | + })); |
| 38 | + |
| 39 | + private static final String FUNCTION_METHOD = "call"; |
| 40 | + |
| 41 | + @Override |
| 42 | + public MethodDatabase.SuspendableType isSuspendable(MethodDatabase db, String className, String superClassName, String[] interfaces, String methodName, String methodDesc, String methodSignature, String[] methodExceptions) { |
| 43 | + MethodDatabase.SuspendableType s = null; |
| 44 | + if (isCoreRx(className) && !EXCEPTIONS.contains(className)) { |
| 45 | + if (isObserverImplementation(db, className, superClassName, interfaces, methodName, methodDesc)) |
| 46 | + s = MethodDatabase.SuspendableType.SUSPENDABLE; |
| 47 | + else if (isUtilFunction(db, className, superClassName, interfaces, methodName, methodDesc)) |
| 48 | + s = MethodDatabase.SuspendableType.SUSPENDABLE; |
| 49 | + } |
| 50 | + // System.out.println("-- " + className + "." + methodName + ": " + s); |
| 51 | + return s; |
| 52 | + } |
| 53 | + |
| 54 | + private boolean isCoreRx(String className) { |
| 55 | + return CORE_PACKAGES.contains(packageOf(className)); |
| 56 | + } |
| 57 | + |
| 58 | + private static boolean isObserverImplementation(MethodDatabase db, String className, String superClassName, String[] interfaces, String methodName, String methodDesc) { |
| 59 | + return !className.equals("rx/Observer") |
| 60 | + && OBSERVER_METHODS.contains(methodName + methodDesc) |
| 61 | + && SimpleSuspendableClassifier.extendsOrImplements("rx/Observer", db, className, superClassName, interfaces); |
| 62 | + } |
| 63 | + |
| 64 | + private static boolean isUtilFunction(MethodDatabase db, String className, String superClassName, String[] interfaces, String methodName, String methodDesc) { |
| 65 | + return (className.startsWith("rx/util/functions/Functions") || className.startsWith("rx/util/functions/Actions")) |
| 66 | + && methodName.equals(FUNCTION_METHOD) |
| 67 | + && (SimpleSuspendableClassifier.extendsOrImplements("rx/util/functions/Function", db, className, superClassName, interfaces) |
| 68 | + || SimpleSuspendableClassifier.extendsOrImplements("rx/util/functions/Action", db, className, superClassName, interfaces)); |
| 69 | + } |
| 70 | + |
| 71 | + private static String packageOf(String className) { |
| 72 | + try { |
| 73 | + return className.substring(0, className.lastIndexOf('/')).replace('/', '.'); |
| 74 | + } catch (RuntimeException e) { |
| 75 | + System.err.println("???? " + className); |
| 76 | + throw e; |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments