Skip to content
Permalink
Browse files

Relax Thenable requirements to accept Thenable from parents

PiperOrigin-RevId: 255738499
  • Loading branch information...
gkdn authored and Copybara-Service committed Jun 29, 2019
1 parent ee2a989 commit 1f8f3f9eab741051e7c45e870bcaa424560dfca9
@@ -21,6 +21,7 @@
import com.google.auto.common.MoreTypes;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;
@@ -37,11 +38,27 @@
* Helper methods for APT processing.
*/
public class MoreApt {
/** Returns the list of the type and its super types from most to least specific order. */

/** Returns the list of the type and its super types without a specific order. */
public static ImmutableSet<TypeElement> getTypeHierarchy(TypeElement typeElement) {
return ImmutableSet.copyOf(getHierarchyImpl(typeElement, true));
}

/** Returns the list of the class and its super classes from most to least specific order. */
public static ImmutableList<TypeElement> getClassHierarchy(TypeElement typeElement) {
return getHierarchyImpl(typeElement, false);
}

public static ImmutableList<TypeElement> getHierarchyImpl(
TypeElement typeElement, boolean includeInterfaces) {
ImmutableList.Builder<TypeElement> classHierarchyBuilder = new ImmutableList.Builder<>();
for (TypeElement t = typeElement; t != null; t = asTypeElement(t.getSuperclass())) {
classHierarchyBuilder.add(t);
if (includeInterfaces) {
for (TypeMirror i : t.getInterfaces()) {
classHierarchyBuilder.addAll(getHierarchyImpl(asTypeElement(i), includeInterfaces));
}
}
}
return classHierarchyBuilder.build();
}
@@ -38,27 +38,18 @@
input -> input.getSimpleName().toString().startsWith("test");

private static final boolean isPromise(TypeElement type) {
// We consider a type to be working under the following conditions:
//
// - It is a JsType
// - It has a then method with either one or two parameters
// - All the parameters of the then methods are annotated with @JsFunction
//
// For now we do not take a look at the hierarchy of the type, so we
// could be missing then methods in parent classes or interfaces. Since in tests
// one can always use the base type this is probably fine.
if (type == null || !isAnnotationPresent(type, JsType.class)) {
if (type == null) {
return false;
}

return ElementFilter.methodsIn(type.getEnclosedElements())
.stream()
return MoreApt.getTypeHierarchy(type).stream()
.filter(t -> isAnnotationPresent(t, JsType.class))
.flatMap(t -> ElementFilter.methodsIn(t.getEnclosedElements()).stream())
.filter(m -> m.getSimpleName().contentEquals("then"))
.filter(m -> m.getParameters().size() == 1 || m.getParameters().size() == 2)
.anyMatch(
m ->
m.getParameters()
.stream()
m.getParameters().stream()
.map(ve -> MoreApt.asTypeElement(ve.asType()))
.allMatch(t -> t != null && isAnnotationPresent(t, JsFunction.class)));
}
@@ -82,7 +82,9 @@ public void testResolvesAfterDelay() throws Exception {
TestResult testResult =
newTestResultBuilder()
.testClassName(testName)
.addTestSuccess("testResolvesAfterDelay")
.addTestSuccess("testResolvesAfterDelay1")
.addTestSuccess("testResolvesAfterDelay2")
.addTestSuccess("testResolvesAfterDelay3")
.build();

List<String> logLines = runTest(testName);
@@ -24,7 +24,26 @@
public class TestResolvesAfterDelay {

@Test(timeout = 200L)
public Thenable testResolvesAfterDelay() {
public Thenable testResolvesAfterDelay1() {
return (onFulfilled, onRejected) -> Timer.schedule(() -> onFulfilled.execute(null), 0);
}

private interface SubThenable extends Thenable {}

@Test(timeout = 200L)
public SubThenable testResolvesAfterDelay2() {
return (onFulfilled, onRejected) -> Timer.schedule(() -> onFulfilled.execute(null), 0);
}

private abstract static class ThenableImpl implements SubThenable {}

@Test(timeout = 200L)
public ThenableImpl testResolvesAfterDelay3() {
return new ThenableImpl() {
@Override
public void then(FullFilledCallback onFulfilled, RejectedCallback onRejected) {
Timer.schedule(() -> onFulfilled.execute(null), 0);
}
};
}
}

0 comments on commit 1f8f3f9

Please sign in to comment.
You can’t perform that action at this time.