Skip to content

Commit 31141a5

Browse files
committed
Handle collection and map types
1 parent 3008618 commit 31141a5

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

plugins/org.eclipse.epsilon.eol.staticanalyser/src/org/eclipse/epsilon/eol/staticanalyser/EolStaticAnalyser.java

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.eclipse.epsilon.eol.staticanalyser;
22

33
import java.lang.reflect.Method;
4+
import java.lang.reflect.ParameterizedType;
5+
import java.lang.reflect.Type;
46
import java.util.ArrayList;
57
import java.util.HashMap;
68
import java.util.HashSet;
@@ -1201,18 +1203,47 @@ public void preValidate(IEolModule imodule) {
12011203

12021204
for(Method m: oc.getClass().getDeclaredMethods()) {
12031205
List<EolType> operationParameterTypes = new ArrayList<EolType>();
1204-
Class<?>[] javaParameterTypes = m.getParameterTypes();
1205-
for (Class<?> javaParameterType : javaParameterTypes) {
1206-
operationParameterTypes.add(javaClassToEolType(javaParameterType));
1206+
Type[] javaParameterTypes = m.getGenericParameterTypes();
1207+
for (Type javaParameterType : javaParameterTypes) {
1208+
operationParameterTypes.add(javaTypeToEolType(javaParameterType));
12071209
}
1208-
EolType returnType = javaClassToEolType(m.getReturnType());
1210+
EolType returnType = javaTypeToEolType(m.getGenericReturnType());
12091211
operations.add(new SimpleOperation(m.getName(), contextType, returnType, operationParameterTypes));
12101212
}
12111213
}
12121214
}
12131215

1216+
public EolType javaTypeToEolType(Type javaType) {
1217+
1218+
if (javaType instanceof ParameterizedType) {
1219+
Type rawType = ((ParameterizedType)javaType).getRawType();
1220+
Type[] typeArgs = ((ParameterizedType)javaType).getActualTypeArguments();
1221+
EolType eolType = javaClassToEolType((Class<?>)rawType);
1222+
if (eolType instanceof EolCollectionType){
1223+
EolType contentType = javaTypeToEolType(typeArgs[0]);
1224+
eolType = new EolCollectionType(eolType.getName(), contentType);
1225+
return eolType;
1226+
}
1227+
else if (eolType instanceof EolMapType) {
1228+
EolType keyType = javaTypeToEolType(typeArgs[0]);
1229+
EolType valueType = javaTypeToEolType(typeArgs[1]);
1230+
eolType = new EolMapType(keyType, valueType);
1231+
return eolType;
1232+
}
1233+
else {
1234+
return EolAnyType.Instance;
1235+
}
1236+
}
1237+
else if (javaType instanceof Class<?>) {
1238+
Class<?> javaClass = (Class<?>) javaType;
1239+
return javaClassToEolType(javaClass);
1240+
}
1241+
else {
1242+
return EolAnyType.Instance;
1243+
}
1244+
}
1245+
12141246
public EolType javaClassToEolType(Class<?> javaClass) {
1215-
12161247
if (javaClass == String.class || javaClass == char.class) {
12171248
return EolPrimitiveType.String;
12181249
} else if (javaClass == Integer.class || javaClass == int.class) {
@@ -1222,9 +1253,17 @@ public EolType javaClassToEolType(Class<?> javaClass) {
12221253
return EolPrimitiveType.Real;
12231254
} else if (javaClass == boolean.class || javaClass == Boolean.class) {
12241255
return EolPrimitiveType.Boolean;
1256+
} else if (javaClass == java.util.Collection.class) {
1257+
return EolCollectionType.Bag;
1258+
} else if (javaClass == java.util.List.class) {
1259+
return EolCollectionType.Sequence;
1260+
} else if (javaClass == java.util.Set.class) {
1261+
return EolCollectionType.Set;
1262+
} else if (javaClass == java.util.Map.class) {
1263+
return EolMapType.Map;
1264+
} else {
1265+
return EolAnyType.Instance;
12251266
}
1226-
1227-
return EolAnyType.Instance;
12281267
}
12291268

12301269
public void mainValidate(IEolModule module) {

plugins/org.eclipse.epsilon.eol.staticanalyser/src/org/eclipse/epsilon/eol/staticanalyser/types/EolUnionType.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public String getName() {
2626
return containedTypes.stream().map(t -> t.getName()).collect(Collectors.joining("|"));
2727
}
2828

29+
@Override
30+
public String toString() {
31+
return containedTypes.stream().map(t -> t.toString()).collect(Collectors.joining("|"));
32+
}
33+
2934
@Override
3035
public List<EolType> getChildrenTypes(){
3136
List<EolType> children = new ArrayList<EolType>();

0 commit comments

Comments
 (0)