Skip to content

Commit 6deafe6

Browse files
committed
Move Types utility class to scijava-common3
Unlike Nil, it has no dependencies. Having it in scijava-common3 means people will be able to use the Types utility methods without incurring the heavyweight Guava dependency, but rather only scijava-common3.
1 parent 727c85d commit 6deafe6

File tree

70 files changed

+191
-201
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+191
-201
lines changed

scijava-types/src/main/java/org/scijava/types/Any.java renamed to scijava-common3/src/main/java/org/scijava/common3/Any.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*-
22
* #%L
3-
* SciJava library for generic type reasoning.
3+
* Common functionality widely used across SciJava modules.
44
* %%
5-
* Copyright (C) 2016 - 2024 SciJava developers.
5+
* Copyright (C) 2021 - 2024 SciJava developers.
66
* %%
77
* Redistribution and use in source and binary forms, with or without
88
* modification, are permitted provided that the following conditions are met:
@@ -27,7 +27,7 @@
2727
* #L%
2828
*/
2929

30-
package org.scijava.types;
30+
package org.scijava.common3;
3131

3232
import java.lang.reflect.Type;
3333
import java.util.Arrays;

scijava-types/src/main/java/org/scijava/types/GenericTyped.java renamed to scijava-common3/src/main/java/org/scijava/common3/GenericTyped.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
22
* #%L
3-
* SciJava library for generic type reasoning.
3+
* Common functionality widely used across SciJava modules.
44
* %%
5-
* Copyright (C) 2016 - 2024 SciJava developers.
5+
* Copyright (C) 2021 - 2024 SciJava developers.
66
* %%
77
* Redistribution and use in source and binary forms, with or without
88
* modification, are permitted provided that the following conditions are met:
@@ -27,7 +27,7 @@
2727
* #L%
2828
*/
2929

30-
package org.scijava.types;
30+
package org.scijava.common3;
3131

3232
import java.lang.reflect.Type;
3333

scijava-types/src/main/java/org/scijava/types/Types.java renamed to scijava-common3/src/main/java/org/scijava/common3/Types.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
22
* #%L
3-
* SciJava library for generic type reasoning.
3+
* Common functionality widely used across SciJava modules.
44
* %%
5-
* Copyright (C) 2016 - 2024 SciJava developers.
5+
* Copyright (C) 2021 - 2024 SciJava developers.
66
* %%
77
* Redistribution and use in source and binary forms, with or without
88
* modification, are permitted provided that the following conditions are met:
@@ -27,7 +27,7 @@
2727
* #L%
2828
*/
2929

30-
package org.scijava.types;
30+
package org.scijava.common3;
3131

3232
// Portions of this class were adapted from the
3333
// org.apache.commons.lang3.reflect.TypeUtils and

scijava-types/src/test/java/org/scijava/types/GreatestCommonSupertypeTest.java renamed to scijava-common3/src/test/java/org/scijava/common3/GreatestCommonSupertypeTest.java

Lines changed: 69 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*-
22
* #%L
3-
* SciJava library for generic type reasoning.
3+
* Common functionality widely used across SciJava modules.
44
* %%
5-
* Copyright (C) 2016 - 2024 SciJava developers.
5+
* Copyright (C) 2021 - 2024 SciJava developers.
66
* %%
77
* Redistribution and use in source and binary forms, with or without
88
* modification, are permitted provided that the following conditions are met:
@@ -27,7 +27,7 @@
2727
* #L%
2828
*/
2929

30-
package org.scijava.types;
30+
package org.scijava.common3;
3131

3232
import static org.junit.jupiter.api.Assertions.assertFalse;
3333
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -64,158 +64,142 @@ static class WeirdThing extends RecursiveThing<WeirdThing> { }
6464

6565
@Test
6666
public void testDouble() {
67-
Type t1 = new Nil<Double>() {}.type();
68-
Type t2 = new Nil<Double>() {}.type();
67+
Type t1 = Double.class;
68+
Type t2 = Double.class;
6969
Type superType = Types.superTypeOf(new Type[] { t1, t2 },
7070
false);
7171
assertTrue(superType.equals(Double.class));
7272
}
7373

7474
@Test
7575
public void testNumber() {
76-
Type t1 = new Nil<Double>() {}.type();
77-
Type t2 = new Nil<Long>() {}.type();
76+
Type t1 = Double.class;
77+
Type t2 = Long.class;
7878
Type superType = Types.superTypeOf(new Type[] { t1, t2 },
7979
false);
8080
assertTrue(superType.equals(Number.class));
8181
}
8282

8383
@Test
8484
public void testObject() {
85-
Type t1 = new Nil<Double>() {}.type();
86-
Type t2 = new Nil<String>() {}.type();
85+
Type t1 = Double.class;
86+
Type t2 = String.class;
8787
Type superType = Types.superTypeOf(new Type[] { t1, t2 },
8888
false);
89-
Type expected = new Nil<Comparable<?>>() {}.type();
89+
Type expected = Types.parameterize(Comparable.class, new Type[] { Types.wildcard() });
9090
assertTrue(superType.equals(expected));
9191
}
9292

9393
@SuppressWarnings("rawtypes")
9494
@Test
9595
public void testListOfSame() {
96-
Type t1 = new Nil<List<Double>>() {}.type();
97-
Type t2 = new Nil<List<Double>>() {}.type();
98-
Type superType = Types.superTypeOf(new Type[] { t1, t2 },
96+
Type listOfDouble = Types.parameterize(List.class, new Type[] { Double.class });
97+
Type superType = Types.superTypeOf(new Type[] { listOfDouble, listOfDouble },
9998
false);
100-
assertTrue(superType.equals(new Nil<List<Double>>() {}.type()));
101-
assertFalse(superType.equals(new Nil<List<Object>>() {}.type()),
99+
assertTrue(superType.equals(listOfDouble));
100+
Type listOfObject = Types.parameterize(List.class, new Type[] { Object.class });
101+
assertFalse(superType.equals(listOfObject),
102102
"Class Double should take precedence over Object");
103-
assertFalse(superType.equals(new Nil<List<?>>() {}.type()),
103+
Type listOfQ = Types.parameterize(List.class, new Type[] { Types.wildcard() });
104+
assertFalse(superType.equals(listOfQ),
104105
"Class Double should be discernable over wildcard");
105-
assertFalse(superType.equals(new Nil<List>() {}.type()),
106+
assertFalse(superType.equals(List.class),
106107
"Class Double should be discernable, rawtype should not be returned");
107108
}
108109

109110
@Test
110111
public void testListOfDifferent() {
111-
Type t1 = new Nil<List<Double>>() {}.type();
112-
Type t2 = new Nil<List<String>>() {}.type();
112+
Type t1 = Types.parameterize(List.class, new Type[] { Double.class });
113+
Type t2 = Types.parameterize(List.class, new Type[] { String.class });
113114
Type superType = Types.superTypeOf(new Type[] { t1, t2 },
114115
false);
115-
Type expectedListType = Types.wildcard(new Type[] {
116-
new Nil<Comparable<?>>()
117-
{}.type() }, new Type[] {});
118-
Type expected = Types.parameterize(List.class, new Type[] {
119-
expectedListType });
116+
Type comparableQ = Types.parameterize(Comparable.class, new Type[] { Types.wildcard() });
117+
Type expectedListType = Types.wildcard(new Type[] { comparableQ }, new Type[] {});
118+
Type expected = Types.parameterize(List.class, new Type[] { expectedListType });
120119
assertTrue(superType.equals(expected));
121120
}
122121

123122
@Test
124123
public void testListOfListOfDifferent() {
125-
Type t1 = new Nil<List<List<Double>>>() {}.type();
126-
Type t2 = new Nil<List<List<String>>>() {}.type();
127-
Type superType = Types.superTypeOf(new Type[] { t1, t2 },
128-
false);
129-
Type expectedType = Types.wildcard(new Type[] { new Nil<Comparable<?>>() {}
130-
.type() }, new Type[] {});
131-
Type expectedList = Types.parameterize(List.class, new Type[] {
132-
expectedType });
133-
Type expectedListType = Types.wildcard(new Type[] { expectedList },
134-
new Type[] {});
135-
Type expected = Types.parameterize(List.class, new Type[] {
136-
expectedListType });
124+
Type listDouble = Types.parameterize(List.class, new Type[] { Double.class });
125+
Type listString = Types.parameterize(List.class, new Type[] { String.class });
126+
Type t1 = Types.parameterize(List.class, new Type[] { listDouble });
127+
Type t2 = Types.parameterize(List.class, new Type[] { listString });
128+
Type superType = Types.superTypeOf(new Type[] { t1, t2 }, false);
129+
Type comparableQ = Types.parameterize(Comparable.class, new Type[] { Types.wildcard() });
130+
Type expectedType = Types.wildcard(new Type[] { comparableQ }, new Type[] {});
131+
Type expectedList = Types.parameterize(List.class, new Type[] { expectedType });
132+
Type expectedListType = Types.wildcard(new Type[] { expectedList }, new Type[] {});
133+
Type expected = Types.parameterize(List.class, new Type[] { expectedListType });
137134
assertTrue(superType.equals(expected));
138135
}
139136

140137
@Test
141138
public void testArrayListAndList() {
142-
Type t1 = new Nil<List<Double>>() {}.type();
143-
Type t2 = new Nil<ArrayList<Double>>() {}.type();
144-
Type superType = Types.superTypeOf(new Type[] { t1, t2 },
145-
false);
146-
assertTrue(superType.equals(new Nil<List<Double>>() {}.type()));
147-
Type t3 = new Nil<ArrayList<Double>>() {}.type();
148-
Type t4 = new Nil<List<Double>>() {}.type();
149-
Type superType2 = Types.superTypeOf(new Type[] { t3, t4 },
150-
false);
151-
assertTrue(superType2.equals(new Nil<List<Double>>() {}.type()));
139+
Type t1 = Types.parameterize(List.class, new Type[] { Double.class });
140+
Type t2 = Types.parameterize(ArrayList.class, new Type[] { Double.class });
141+
Type superType = Types.superTypeOf(new Type[] { t1, t2 }, false);
142+
assertTrue(superType.equals(t1));
143+
Type superType2 = Types.superTypeOf(new Type[] { t2, t1 }, false);
144+
assertTrue(superType2.equals(t1));
152145
}
153146

154147
@Test
155148
public void testNThingQThing() {
156-
Type t3 = new Nil<NThing>() {}.type();
157-
Type t4 = new Nil<QThing>() {}.type();
158-
Type superType = Types.superTypeOf(new Type[] { t3, t4 },
159-
false);
160-
Type expected = Types.wildcard(new Type[] { new Nil<Thing>() {}.type(),
161-
new Nil<Stuff>()
162-
{}.type() }, new Type[] {});
149+
Type t3 = NThing.class;
150+
Type t4 = QThing.class;
151+
Type superType = Types.superTypeOf(new Type[] { t3, t4 }, false);
152+
Type expected = Types.wildcard(new Type[] { Thing.class, Stuff.class }, new Type[] {});
163153
assertTrue(superType.equals(expected));
164154
}
165155

166156
@Test
167157
public void testNThingYThing() {
168-
Type t3 = new Nil<NThing>() {}.type();
169-
Type t4 = new Nil<YThing>() {}.type();
170-
Type superType = Types.superTypeOf(new Type[] { t3, t4 },
171-
false);
172-
Type expected = Types.wildcard(new Type[] { new Nil<Thing>() {}.type() },
173-
new Type[] {});
158+
Type t3 = NThing.class;
159+
Type t4 = YThing.class;
160+
Type superType = Types.superTypeOf(new Type[] { t3, t4 }, false);
161+
Type expected = Types.wildcard(new Type[] { Thing.class }, new Type[] {});
174162
assertFalse(superType.equals(expected),
175163
"Greatest common type should not be a wildcard");
176-
assertTrue(superType.equals(new Nil<Thing>() {}.type()));
164+
assertTrue(superType.equals(Thing.class));
177165
}
178166

179167
@Test
180168
public void testNThingXThing() {
181-
Type t3 = new Nil<NThing>() {}.type();
182-
Type t4 = new Nil<XThing>() {}.type();
183-
Type superType = Types.superTypeOf(new Type[] { t3, t4 },
184-
false);
185-
assertTrue(superType.equals(new Nil<Base>() {}.type()));
186-
assertFalse(superType.equals(new Nil<Thing>() {}.type()),
169+
Type t3 = NThing.class;
170+
Type t4 = XThing.class;
171+
Type superType = Types.superTypeOf(new Type[] { t3, t4 }, false);
172+
assertTrue(superType.equals(Base.class));
173+
assertFalse(superType.equals(Thing.class),
187174
"Non-Object classes should take precedence over interfaces");
188175
}
189176

190177
@Test
191178
public void testRecursiveClass() {
192-
Type t1 = new Nil<StrangeThing>() {}.type();
193-
Type t2 = new Nil<WeirdThing>() {}.type();
179+
Type t1 = StrangeThing.class;
180+
Type t2 = WeirdThing.class;
194181
Type superType = Types.superTypeOf(new Type[] { t1, t2 },
195182
false);
196-
Nil<RecursiveThing<?>> expected = new Nil<>() {};
197-
assertTrue(superType.equals(expected.type()));
183+
Type expected = Types.parameterize(RecursiveThing.class, new Type[] { Types.wildcard() });
184+
assertTrue(superType.equals(expected));
198185
}
199186

200187
@Test
201-
public <T extends Base> void testTypeVar() {
202-
Type t1 = new Nil<T>() {}.type();
203-
Type t2 = new Nil<NThing>() {}.type();
204-
Type superType = Types.superTypeOf(new Type[] { t1, t2 },
205-
false);
206-
Nil<Base> expected = new Nil<>() {};
207-
assertTrue(superType.equals(expected.type()));
188+
public void testTypeVar() {
189+
class C<T extends Base> {}
190+
Type t1 = C.class.getTypeParameters()[0];
191+
Type t2 = NThing.class;
192+
Type superType = Types.superTypeOf(new Type[] { t1, t2 }, false);
193+
assertTrue(superType.equals(Base.class));
208194
}
209195

210196
@Test
211197
public void testWildcardType() {
212-
Type typeWithWildcard = new Nil<List<? extends NThing>>() {}.type();
213-
Type t1 = ((ParameterizedType) typeWithWildcard)
214-
.getActualTypeArguments()[0];
215-
Type t2 = new Nil<XThing>() {}.type();
216-
Type superType = Types.superTypeOf(new Type[] { t1, t2 },
217-
false);
218-
Nil<Base> expected = new Nil<>() {};
219-
assertTrue(superType.equals(expected.type()));
198+
Type qNThing = Types.wildcard(NThing.class, null);
199+
Type typeWithWildcard = Types.parameterize(List.class, new Type[] { qNThing });
200+
Type t1 = ((ParameterizedType) typeWithWildcard).getActualTypeArguments()[0];
201+
Type t2 = XThing.class;
202+
Type superType = Types.superTypeOf(new Type[] { t1, t2 }, false);
203+
assertTrue(superType.equals(Base.class));
220204
}
221205
}

0 commit comments

Comments
 (0)