summaryrefslogtreecommitdiff
path: root/platform/script-debugger/protocol/protocol-reader/src/org/jetbrains/protocolReader/InterfaceReader.java
blob: f80b8c54bb428e867f6e177a25d9944bfcf2d41e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package org.jetbrains.protocolReader;

import gnu.trove.THashSet;
import org.chromium.protocolReader.JsonField;
import org.chromium.protocolReader.JsonSubtype;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.io.JsonReaderEx;
import org.jetbrains.jsonProtocol.StringIntPair;

import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.WildcardType;
import java.util.*;

class InterfaceReader {
  private static final PrimitiveValueReader LONG_PARSER = new PrimitiveValueReader("long", "-1");

  private static final PrimitiveValueReader INTEGER_PARSER = new PrimitiveValueReader("int", "-1");
  private static final PrimitiveValueReader NULLABLE_INTEGER_PARSER = new PrimitiveValueReader("int", "-1", true, false);

  private static final PrimitiveValueReader BOOLEAN_PARSER = new PrimitiveValueReader("boolean");
  private static final PrimitiveValueReader FLOAT_PARSER = new PrimitiveValueReader("float");

  private static final PrimitiveValueReader NUMBER_PARSER = new PrimitiveValueReader("double");
  private static final PrimitiveValueReader NULLABLE_NUMBER_PARSER = new PrimitiveValueReader("double", true);

  private static final PrimitiveValueReader STRING_PARSER = new PrimitiveValueReader("String");
  private static final PrimitiveValueReader NULLABLE_STRING_PARSER = new PrimitiveValueReader("String", true);

  private static final PrimitiveValueReader RAW_STRING_PARSER = new PrimitiveValueReader("String", null, false, true);
  private static final PrimitiveValueReader RAW_STRING_OR_MAP_PARSER = new PrimitiveValueReader("Object", null, false, true) {
    @Override
    void writeReadCode(ClassScope methodScope, boolean subtyping, String fieldName, TextOutput out) {
      out.append("readRawStringOrMap(");
      addReaderParameter(subtyping, out);
      out.append(')');
    }
  };

  private static final RawValueReader JSON_PARSER = new RawValueReader(false);
  private static final RawValueReader NULLABLE_JSON_PARSER = new RawValueReader(true);

  private static final MapReader MAP_PARSER = new MapReader(false);
  private static final MapReader NULLABLE_MAP_PARSER = new MapReader(true);

  private static final StringIntPairValueReader STRING_INT_PAIR_PARSER = new StringIntPairValueReader();

  final static ValueReader VOID_PARSER = new ValueReader(true) {
    @Override
    public void appendFinishedValueTypeName(TextOutput out) {
      out.append("void");
    }

    @Override
    void writeReadCode(ClassScope scope, boolean subtyping, String fieldName, TextOutput out) {
      out.append("null");
    }

    @Override
    void writeArrayReadCode(ClassScope scope, boolean subtyping, boolean nullable, String fieldName, TextOutput out) {
      throw new UnsupportedOperationException();
    }
  };

  private final LinkedHashMap<Class<?>, TypeHandler<?>> typeToTypeHandler;

  final List<TypeRef<?>> refs = new ArrayList<>();
  final List<SubtypeCaster> subtypeCasters = new ArrayList<>();

  InterfaceReader(Class<?>[] protocolInterfaces) {
    typeToTypeHandler = new LinkedHashMap<>(protocolInterfaces.length);
    for (Class<?> typeClass : protocolInterfaces) {
      typeToTypeHandler.put(typeClass, null);
    }
  }

  private InterfaceReader(LinkedHashMap<Class<?>, TypeHandler<?>> typeToTypeHandler) {
    this.typeToTypeHandler = typeToTypeHandler;
  }

  public static TypeHandler<?> createHandler(LinkedHashMap<Class<?>, TypeHandler<?>> typeToTypeHandler, Class<?> aClass) {
    InterfaceReader reader = new InterfaceReader(typeToTypeHandler);
    reader.processed.addAll(typeToTypeHandler.keySet());
    reader.go(new Class[]{aClass});
    return typeToTypeHandler.get(aClass);
  }

  LinkedHashMap<Class<?>, TypeHandler<?>> go() {
    return go(typeToTypeHandler.keySet().toArray(new Class[typeToTypeHandler.size()]));
  }

  private LinkedHashMap<Class<?>, TypeHandler<?>> go(Class<?>[] classes) {
    for (Class<?> typeClass : classes) {
      createIfNotExists(typeClass);
    }

    boolean hasUnresolved = true;
    while (hasUnresolved) {
      hasUnresolved = false;
      // refs can be modified - new items can be added
      //noinspection ForLoopReplaceableByForEach
      for (int i = 0, n = refs.size(); i < n; i++) {
        TypeRef<?> ref = refs.get(i);
        TypeHandler<?> type = typeToTypeHandler.get(ref.typeClass);
        if (type == null) {
          createIfNotExists(ref.typeClass);
          hasUnresolved = true;
          type = typeToTypeHandler.get(ref.typeClass);
          if (type == null) {
            throw new IllegalStateException();
          }
        }
        ref.set(type);
      }
    }

    for (SubtypeCaster subtypeCaster : subtypeCasters) {
      subtypeCaster.getSubtypeHandler().getSubtypeSupport().setSubtypeCaster(subtypeCaster);
    }

    return typeToTypeHandler;
  }

  private final Set<Class<?>> processed = new THashSet<>();

  private void createIfNotExists(Class<?> typeClass) {
    if (typeClass == Map.class || typeClass == List.class || !typeClass.isInterface()) {
      return;
    }

    if (processed.contains(typeClass)) {
      return;
    }
    processed.add(typeClass);

    typeToTypeHandler.put(typeClass, null);

    for (Class<?> aClass : typeClass.getDeclaredClasses()) {
      createIfNotExists(aClass);
    }

    TypeHandler<?> typeHandler = createTypeHandler(typeClass);
    for (TypeRef<?> ref : refs) {
      if (ref.typeClass == typeClass) {
        assert ref.get() == null;
        ref.set(typeHandler);
        break;
      }
    }
    typeToTypeHandler.put(typeClass, typeHandler);
  }

  private <T> TypeHandler<T> createTypeHandler(Class<T> typeClass) {
    if (!typeClass.isInterface()) {
      throw new JsonProtocolModelParseException("Json model type should be interface: " + typeClass.getName());
    }

    FieldProcessor<T> fields = new FieldProcessor<>(this, typeClass);
    fields.go();

    LinkedHashMap<Method, MethodHandler> methodHandlerMap = fields.getMethodHandlerMap();
    for (Method method : methodHandlerMap.keySet()) {
      Class<?> returnType = method.getReturnType();
      if (returnType != typeClass) {
        createIfNotExists(returnType);
      }
    }

    return new TypeHandler<>(typeClass, getSuperclassRef(typeClass),
                              fields.getVolatileFields(), methodHandlerMap,
                              fields.getFieldLoaders(),
                              fields.lazyRead);
  }

  ValueReader getFieldTypeParser(Type type, boolean declaredNullable, boolean isSubtyping, @Nullable Method method) {
    if (type instanceof Class) {
      Class<?> typeClass = (Class<?>)type;
      if (type == Long.TYPE) {
        nullableIsNotSupported(declaredNullable);
        return LONG_PARSER;
      }
      else if (type == Integer.TYPE) {
        return declaredNullable ? NULLABLE_INTEGER_PARSER : INTEGER_PARSER;
      }
      else if (type == Boolean.TYPE) {
        nullableIsNotSupported(declaredNullable);
        return BOOLEAN_PARSER;
      }
      else if (type == Float.TYPE) {
        nullableIsNotSupported(declaredNullable);
        return FLOAT_PARSER;
      }
      else if (type == Number.class || type == Double.TYPE) {
        return declaredNullable ? NULLABLE_NUMBER_PARSER : NUMBER_PARSER;
      }
      else if (type == Void.TYPE) {
        nullableIsNotSupported(declaredNullable);
        return VOID_PARSER;
      }
      else if (type == String.class) {
        if (declaredNullable) {
          return NULLABLE_STRING_PARSER;
        }
        else {
          if (method != null) {
            JsonField jsonField = method.getAnnotation(JsonField.class);
            if (jsonField != null && jsonField.allowAnyPrimitiveValue()) {
              return RAW_STRING_PARSER;
            }
          }
          return STRING_PARSER;
        }
      }
      else if (type == Object.class) {
        return RAW_STRING_OR_MAP_PARSER;
      }
      else if (type == JsonReaderEx.class) {
        return declaredNullable ? NULLABLE_JSON_PARSER : JSON_PARSER;
      }
      else if (type == Map.class) {
        return declaredNullable ? NULLABLE_MAP_PARSER : MAP_PARSER;
      }
      else if (type == StringIntPair.class) {
        return STRING_INT_PAIR_PARSER;
      }
      else if (typeClass.isArray()) {
        return new ArrayReader(getFieldTypeParser(typeClass.getComponentType(), false, false, null), false,
                               declaredNullable);
      }
      else if (typeClass.isEnum()) {
        //noinspection unchecked
        return EnumReader.create((Class<RetentionPolicy>)typeClass, declaredNullable);
      }
      TypeRef<?> ref = getTypeRef(typeClass);
      if (ref != null) {
        return createJsonParser(ref, declaredNullable, isSubtyping);
      }
      throw new JsonProtocolModelParseException("Method return type " + type + " (simple class) not supported");
    }
    else if (type instanceof ParameterizedType) {
      ParameterizedType parameterizedType = (ParameterizedType)type;
      if (parameterizedType.getRawType() == List.class) {
        Type argumentType = parameterizedType.getActualTypeArguments()[0];
        if (argumentType instanceof WildcardType) {
          WildcardType wildcard = (WildcardType)argumentType;
          if (wildcard.getLowerBounds().length == 0 && wildcard.getUpperBounds().length == 1) {
            argumentType = wildcard.getUpperBounds()[0];
          }
        }
        return new ArrayReader(getFieldTypeParser(argumentType, false, false, null), true, declaredNullable);
      }
      else if (parameterizedType.getRawType() == Map.class) {
        return declaredNullable ? NULLABLE_MAP_PARSER : MAP_PARSER;
      }
      else {
        throw new JsonProtocolModelParseException("Method return type " + type + " (generic) not supported");
      }
    }
    else {
      throw new JsonProtocolModelParseException("Method return type " + type + " not supported");
    }
  }

  private static void nullableIsNotSupported(boolean declaredNullable) {
    if (declaredNullable) {
      throw new JsonProtocolModelParseException("The type cannot be declared nullable");
    }
  }

  private static <T> ObjectValueReader<T> createJsonParser(TypeRef<T> type, boolean isNullable, boolean isSubtyping) {
    return new ObjectValueReader<>(type, isNullable, isSubtyping);
  }

  <T> TypeRef<T> getTypeRef(Class<T> typeClass) {
    TypeRef<T> result = new TypeRef<>(typeClass);
    refs.add(result);
    return result;
  }

  private TypeRef<?> getSuperclassRef(Class<?> typeClass) {
    TypeRef<?> result = null;
    for (Type interfaceGeneric : typeClass.getGenericInterfaces()) {
      if (!(interfaceGeneric instanceof ParameterizedType)) {
        continue;
      }
      ParameterizedType parameterizedType = (ParameterizedType)interfaceGeneric;
      if (parameterizedType.getRawType() != JsonSubtype.class) {
        continue;
      }
      Type param = parameterizedType.getActualTypeArguments()[0];
      if (!(param instanceof Class)) {
        throw new JsonProtocolModelParseException("Unexpected type of superclass " + param);
      }
      Class<?> paramClass = (Class<?>)param;
      if (result != null) {
        throw new JsonProtocolModelParseException("Already has superclass " +
                                                  result.getTypeClass().getName());
      }
      result = getTypeRef(paramClass);
      if (result == null) {
        throw new JsonProtocolModelParseException("Unknown base class " + paramClass.getName());
      }
    }
    return result;
  }
}