Skip to content

Commit 86f2165

Browse files
committed
Moved some methods from Either to TypeUtils
1 parent 0ad46e1 commit 86f2165

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

org.eclipse.lsp4j.jsonrpc/src/main/java/org/eclipse/lsp4j/jsonrpc/json/adapters/EitherTypeAdapterFactory.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
import java.io.IOException;
1111
import java.lang.reflect.Type;
12-
import java.util.ArrayList;
1312
import java.util.Collection;
13+
import java.util.HashSet;
1414

1515
import org.eclipse.lsp4j.jsonrpc.messages.Either;
1616
import org.eclipse.lsp4j.jsonrpc.messages.Either3;
@@ -31,7 +31,7 @@ public class EitherTypeAdapterFactory implements TypeAdapterFactory {
3131
@SuppressWarnings({ "rawtypes", "unchecked" })
3232
@Override
3333
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
34-
if (!Either.isEither(typeToken.getType())) {
34+
if (!TypeUtils.isEither(typeToken.getType())) {
3535
return null;
3636
}
3737
return new Adapter(gson, typeToken);
@@ -102,9 +102,9 @@ protected static class EitherTypeArgument<T> {
102102
public EitherTypeArgument(Gson gson, Type type) {
103103
this.token = (TypeToken<T>) TypeToken.get(type);
104104
this.adapter = gson.getAdapter(this.token);
105-
this.expectedTokens = new ArrayList<>();
106-
for (Type disjoinType : Either.getAllDisjoinTypes(type)) {
107-
Class<?> rawType = TypeToken.get(disjoinType).getRawType();
105+
this.expectedTokens = new HashSet<>();
106+
for (Type expectedType : TypeUtils.getExpectedTypes(type)) {
107+
Class<?> rawType = TypeToken.get(expectedType).getRawType();
108108
JsonToken expectedToken = getExpectedToken(rawType);
109109
expectedTokens.add(expectedToken);
110110
}

org.eclipse.lsp4j.jsonrpc/src/main/java/org/eclipse/lsp4j/jsonrpc/json/adapters/TypeUtils.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,21 @@
1111
import java.lang.reflect.Type;
1212
import java.lang.reflect.TypeVariable;
1313
import java.lang.reflect.WildcardType;
14+
import java.util.ArrayList;
1415
import java.util.Arrays;
1516
import java.util.Collection;
1617
import java.util.Collections;
1718
import java.util.HashMap;
1819
import java.util.Map;
1920

21+
import org.eclipse.lsp4j.jsonrpc.messages.Either;
22+
2023
import com.google.gson.reflect.TypeToken;
2124

2225
/**
2326
* Utilities for handling types in the JSON parser / serializer.
2427
*/
25-
public class TypeUtils {
28+
public final class TypeUtils {
2629

2730
private TypeUtils() {}
2831

@@ -175,5 +178,47 @@ private String toString(Type type) {
175178
}
176179

177180
}
181+
182+
/**
183+
* Return all possible types that can be expected when an element of the given type is parsed.
184+
* If the type satisfies {@link #isEither(Type)}, a list of the corresponding type arguments is returned,
185+
* otherwise a list containg the type itself is returned. Type parameters are <em>not</em> resolved
186+
* by this method (use {@link #getElementTypes(TypeToken, Class)} to get resolved parameters).
187+
*/
188+
public static Collection<Type> getExpectedTypes(Type type) {
189+
Collection<Type> result = new ArrayList<>();
190+
collectExpectedTypes(type, result);
191+
return result;
192+
}
193+
194+
private static void collectExpectedTypes(Type type, Collection<Type> types) {
195+
if (isEither(type)) {
196+
if (type instanceof ParameterizedType) {
197+
for (Type typeArgument : ((ParameterizedType) type).getActualTypeArguments()) {
198+
collectExpectedTypes(typeArgument, types);
199+
}
200+
}
201+
if (type instanceof Class) {
202+
for (Type typeParameter : ((Class<?>) type).getTypeParameters()) {
203+
collectExpectedTypes(typeParameter, types);
204+
}
205+
}
206+
} else {
207+
types.add(type);
208+
}
209+
}
210+
211+
/**
212+
* Test whether the given type is Either.
213+
*/
214+
public static boolean isEither(Type type) {
215+
if (type instanceof ParameterizedType) {
216+
return isEither(((ParameterizedType) type).getRawType());
217+
}
218+
if (type instanceof Class) {
219+
return Either.class.isAssignableFrom((Class<?>) type);
220+
}
221+
return false;
222+
}
178223

179224
}

org.eclipse.lsp4j.jsonrpc/src/main/java/org/eclipse/lsp4j/jsonrpc/messages/Either.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,15 @@ public static Type getRightDisjointType(Type type) {
120120

121121
/**
122122
* Return all disjoint types.
123+
*
124+
* @deprecated Use {@link org.eclipse.lsp4j.jsonrpc.json.adapters.TypeUtils#getExpectedTypes(Type)} instead
123125
*/
126+
@Deprecated
124127
public static Collection<Type> getAllDisjoinTypes(Type type) {
125128
return collectDisjoinTypes(type, new ArrayList<>());
126129
}
127130

131+
@Deprecated
128132
protected static Collection<Type> collectDisjoinTypes(Type type, Collection<Type> types) {
129133
if (isEither(type)) {
130134
if (type instanceof ParameterizedType) {
@@ -138,13 +142,15 @@ protected static Collection<Type> collectDisjoinTypes(Type type, Collection<Type
138142
return types;
139143
}
140144

145+
@Deprecated
141146
protected static Collection<Type> collectDisjoinTypes(ParameterizedType type, Collection<Type> types) {
142147
for (Type typeArgument : type.getActualTypeArguments()) {
143148
collectDisjoinTypes(typeArgument, types);
144149
}
145150
return types;
146151
}
147152

153+
@Deprecated
148154
protected static Collection<Type> collectDisjoinTypes(Class<?> type, Collection<Type> types) {
149155
for (Type typeParameter : type.getTypeParameters()) {
150156
collectDisjoinTypes(typeParameter, types);
@@ -154,7 +160,10 @@ protected static Collection<Type> collectDisjoinTypes(Class<?> type, Collection<
154160

155161
/**
156162
* Test whether the given type is Either.
163+
*
164+
* @deprecated Use {@link org.eclipse.lsp4j.jsonrpc.json.adapters.TypeUtils#isEither(Type)} instead
157165
*/
166+
@Deprecated
158167
public static boolean isEither(Type type) {
159168
if (type instanceof ParameterizedType) {
160169
return isEither((ParameterizedType) type);
@@ -167,14 +176,20 @@ public static boolean isEither(Type type) {
167176

168177
/**
169178
* Test whether the given type is Either.
179+
*
180+
* @deprecated Use {@link org.eclipse.lsp4j.jsonrpc.json.adapters.TypeUtils#isEither(Type)} instead
170181
*/
182+
@Deprecated
171183
public static boolean isEither(ParameterizedType type) {
172184
return isEither(type.getRawType());
173185
}
174186

175187
/**
176188
* Test whether the given class is Either.
189+
*
190+
* @deprecated Use {@link org.eclipse.lsp4j.jsonrpc.json.adapters.TypeUtils#isEither(Type)} instead
177191
*/
192+
@Deprecated
178193
public static boolean isEither(Class<?> cls) {
179194
return Either.class.isAssignableFrom(cls);
180195
}

0 commit comments

Comments
 (0)