Skip to content

Commit 1141ddb

Browse files
committed
Embedded FieldsAndGetters into SwtDebug directly, so that we don't need it as a runtime dependency.
1 parent 6eb8da7 commit 1141ddb

File tree

3 files changed

+84
-16
lines changed

3 files changed

+84
-16
lines changed

Diff for: META-INF/MANIFEST.MF

+10-11
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@ Export-Package: com.diffplug.common.swt;uses:="com.diffplug.common.bas
1818
ewers,org.eclipse.swt.graphics,org.eclipse.swt.widgets";version="2.0.
1919
0",com.diffplug.common.swt.os;version="2.0.0"
2020
Import-Package: com.diffplug.common.base;version="[1.2,2)",com.diffplu
21-
g.common.collect;version="[1.2,2)",com.diffplug.common.debug;version=
22-
"[1.0,2)",com.diffplug.common.primitives;version="[1.2,2)",com.diffpl
23-
ug.common.rx;version="[2.0,3)",com.diffplug.common.swt,com.diffplug.c
24-
ommon.swt.os,com.diffplug.common.tree;version="[1.2,2)",com.diffplug.
25-
common.util.concurrent;version="[1.2,2)",javax.annotation,org.eclipse
26-
.jface.action,org.eclipse.jface.layout,org.eclipse.jface.resource,org
27-
.eclipse.jface.util,org.eclipse.jface.viewers,org.eclipse.swt,org.ecl
28-
ipse.swt.custom,org.eclipse.swt.graphics,org.eclipse.swt.layout,org.e
29-
clipse.swt.widgets,rx;version="[1.1,2)",rx.functions;version="[1.1,2)
30-
",rx.schedulers;version="[1.1,2)",rx.subjects;version="[1.1,2)",rx.su
31-
bscriptions;version="[1.1,2)"
21+
g.common.collect;version="[1.2,2)",com.diffplug.common.primitives;ver
22+
sion="[1.2,2)",com.diffplug.common.rx;version="[2.0,3)",com.diffplug.
23+
common.swt,com.diffplug.common.swt.os,com.diffplug.common.tree;versio
24+
n="[1.2,2)",com.diffplug.common.util.concurrent;version="[1.2,2)",jav
25+
ax.annotation,org.eclipse.jface.action,org.eclipse.jface.layout,org.e
26+
clipse.jface.resource,org.eclipse.jface.util,org.eclipse.jface.viewer
27+
s,org.eclipse.swt,org.eclipse.swt.custom,org.eclipse.swt.graphics,org
28+
.eclipse.swt.layout,org.eclipse.swt.widgets,rx;version="[1.1,2)",rx.f
29+
unctions;version="[1.1,2)",rx.schedulers;version="[1.1,2)",rx.subject
30+
s;version="[1.1,2)",rx.subscriptions;version="[1.1,2)"
3231
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.8))"

Diff for: build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ dependencies {
4949
compile "com.diffplug.durian:durian-core:${VER_DURIAN}"
5050
compile "com.diffplug.durian:durian-collect:${VER_DURIAN}"
5151
compile "com.diffplug.durian:durian-concurrent:${VER_DURIAN}"
52-
compile "com.diffplug.durian:durian-debug:${VER_DURIAN_DEBUG}"
5352
compile "com.diffplug.durian:durian-rx:${VER_DURIAN_RX}"
5453
compile "io.reactivex:rxjava:${VER_RXJAVA}"
5554

@@ -60,6 +59,7 @@ dependencies {
6059
compile "p2:org.eclipse.equinox.common:${VER_EQUINOX_COMMON}"
6160

6261
testCompile "junit:junit:${VER_JUNIT}"
62+
testCompile "com.diffplug.durian:durian-debug:${VER_DURIAN_DEBUG}"
6363
}
6464

6565
//////////

Diff for: src/com/diffplug/common/swt/SwtDebug.java

+73-4
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,27 @@
1515
*/
1616
package com.diffplug.common.swt;
1717

18+
import java.lang.reflect.Field;
19+
import java.lang.reflect.Modifier;
1820
import java.util.Arrays;
1921
import java.util.Collection;
22+
import java.util.Map;
23+
import java.util.Objects;
24+
import java.util.function.Predicate;
2025
import java.util.stream.Collectors;
2126
import java.util.stream.Stream;
2227

28+
import javax.annotation.Nullable;
29+
2330
import org.eclipse.swt.SWT;
2431
import org.eclipse.swt.widgets.Event;
2532
import org.eclipse.swt.widgets.Widget;
2633

34+
import com.diffplug.common.base.Predicates;
2735
import com.diffplug.common.base.StringPrinter;
36+
import com.diffplug.common.base.Throwing;
2837
import com.diffplug.common.collect.ImmutableMap;
29-
import com.diffplug.common.debug.FieldsAndGetters;
38+
import com.diffplug.common.collect.Maps;
3039
import com.diffplug.common.rx.Rx;
3140

3241
/**
@@ -43,7 +52,7 @@ public static void dumpEvent(String name, Event e, StringPrinter to) {
4352
// print the name
4453
to.println(name + ": " + eventType(e));
4554
// print the non-null / non-zero fields
46-
FieldsAndGetters.fields(e).filter(entry -> {
55+
fields(e).filter(entry -> {
4756
Object value = entry.getValue();
4857
if (value == null) {
4958
return false;
@@ -58,12 +67,12 @@ public static void dumpEvent(String name, Event e, StringPrinter to) {
5867
to.println("\t" + entry.getKey().getName() + " = " + entry.getValue());
5968
});
6069
// print the nulls
61-
to.println("\tnull = " + FieldsAndGetters.fields(e)
70+
to.println("\tnull = " + fields(e)
6271
.filter(entry -> entry.getValue() == null)
6372
.map(entry -> entry.getKey().getName())
6473
.collect(Collectors.joining(", ")));
6574
// print the zeroes
66-
to.println("\t0 = " + FieldsAndGetters.fields(e)
75+
to.println("\t0 = " + fields(e)
6776
.filter(entry -> {
6877
if (entry.getValue() instanceof Number) {
6978
return ((Number) entry.getValue()).intValue() == 0;
@@ -184,4 +193,64 @@ public static ImmutableMap<Integer, String> allEvents() {
184193
builder.put(wakeup, "PostExternalEventDispatch"); // 53
185194
events = builder.build();
186195
}
196+
197+
////////////////////////////////////////////////
198+
// Copied from DurianDebug's FieldsAndGetters //
199+
////////////////////////////////////////////////
200+
/**
201+
* Returns a {@code Stream} of all public fields which match {@code predicate} and their values for the given object.
202+
* <p>
203+
* This method uses reflection to find all of the public instance fields of the given object,
204+
* and if they pass the given predicate, it includes them in a stream of {@code Map.Entry<Field, Object>}
205+
* where the entry's value is the value of the field for this object.
206+
*/
207+
private static Stream<Map.Entry<Field, Object>> fields(@Nullable Object obj, Predicate<Field> predicate) {
208+
Objects.requireNonNull(predicate);
209+
return Arrays.asList(getClassNullable(obj).getFields()).stream()
210+
// gotta be public
211+
.filter(field -> Modifier.isPublic(field.getModifiers()))
212+
// gotta be an instance field
213+
.filter(field -> !Modifier.isStatic(field.getModifiers()))
214+
// gotta pass the predicate
215+
.filter(predicate)
216+
// then create the field
217+
.map(field -> Maps.immutableEntry(field, tryCall(field.getName(), () -> field.get(obj))));
218+
}
219+
220+
private static Object tryCall(String methodName, Throwing.Supplier<Object> supplier) {
221+
try {
222+
return supplier.get();
223+
} catch (Throwable error) {
224+
return new CallException(methodName, error);
225+
}
226+
}
227+
228+
/** Exception which wraps up a thrown exception - ensures that users don't think an exception was returned. */
229+
private static class CallException extends Exception {
230+
private static final long serialVersionUID = 1206955156719866328L;
231+
232+
private final String methodName;
233+
234+
private CallException(String methodName, Throwable cause) {
235+
super(cause);
236+
this.methodName = methodName;
237+
}
238+
239+
@Override
240+
public String toString() {
241+
return "When calling " + methodName + ": " + getCause().getMessage();
242+
}
243+
}
244+
245+
private static Stream<Map.Entry<Field, Object>> fields(@Nullable Object obj) {
246+
return fields(obj, Predicates.alwaysTrue());
247+
}
248+
249+
private static Class<?> getClassNullable(@Nullable Object obj) {
250+
return obj == null ? ObjectIsNull.class : obj.getClass();
251+
}
252+
253+
/** Sentinel class for null objects. */
254+
public static class ObjectIsNull {}
255+
187256
}

0 commit comments

Comments
 (0)