11package io .github .bldl .astParsing ;
22
33import com .github .javaparser .ast .CompilationUnit ;
4- import com .github .javaparser .ast .Node ;
54import com .github .javaparser .ast .NodeList ;
65import com .github .javaparser .ast .PackageDeclaration ;
76import com .github .javaparser .ast .body .ClassOrInterfaceDeclaration ;
87import com .github .javaparser .ast .body .MethodDeclaration ;
9- import com .github .javaparser .ast .expr .AssignExpr ;
10- import com .github .javaparser .ast .expr .Expression ;
11- import com .github .javaparser .ast .expr .MethodCallExpr ;
128import com .github .javaparser .ast .expr .Name ;
13- import com .github .javaparser .ast .expr .NameExpr ;
14- import com .github .javaparser .ast .nodeTypes .NodeWithAnnotations ;
159import com .github .javaparser .ast .type .ClassOrInterfaceType ;
1610import com .github .javaparser .ast .type .Type ;
1711import com .github .javaparser .ast .type .TypeParameter ;
2519import io .github .bldl .astParsing .util .ParamData ;
2620import io .github .bldl .astParsing .visitors .CastInsertionVisitor ;
2721import io .github .bldl .astParsing .visitors .MethodCollector ;
22+ import io .github .bldl .astParsing .visitors .SubtypingCheckVisitor ;
2823import io .github .bldl .astParsing .visitors .TypeEraserVisitor ;
2924import io .github .bldl .astParsing .visitors .VariableCollector ;
3025import io .github .bldl .graph .ClassHierarchyGraph ;
3126import io .github .bldl .util .Pair ;
3227import com .github .javaparser .ast .body .Parameter ;
33-
3428import java .io .File ;
3529import java .nio .file .Paths ;
3630import java .util .HashMap ;
@@ -141,7 +135,10 @@ private void changeAST(File dir, ClassData classData, Map<String, MethodData> me
141135
142136 Set <Pair <String , ClassOrInterfaceType >> varsToWatch = new HashSet <>();
143137 cu .accept (new VariableCollector (classData ), varsToWatch );
144- // performSubtypingChecks(cu, classData, methodMap, varsToWatch);
138+ cu .accept (
139+ new SubtypingCheckVisitor (collectMethodParams (cu , classData ), messager , varsToWatch , classData ,
140+ classHierarchy ),
141+ null );
145142 cu .accept (new TypeEraserVisitor (classData ), null );
146143 for (Pair <String , ClassOrInterfaceType > var : varsToWatch ) {
147144 CastInsertionVisitor castInsertionVisitor = new CastInsertionVisitor (var , methodMap );
@@ -175,7 +172,6 @@ private void computeClassHierarchyRec(ClassHierarchyGraph<String> g, File dir, S
175172 if (supertypes .isEmpty ())
176173 g .addEdge ("Object" , cls .getNameAsString ());
177174 });
178- ;
179175 }
180176 }
181177
@@ -194,51 +190,6 @@ private String appendPackageDeclaration(String existing, String toAppend) {
194190 return existing + "." + toAppend ;
195191 }
196192
197- private void performSubtypingChecks (CompilationUnit cu , ClassData classData ,
198- Map <String , MethodData > methodMap ,
199- Set <Pair <String , String >> varsToWatch ) {
200- Map <String , Map <Integer , Type >> methodParams = collectMethodParams (cu , classData );
201- Map <String , String > varsToWatchMap = new HashMap <>();
202- varsToWatch .forEach (p -> {
203- varsToWatchMap .put (p .first , p .second );
204- });
205- cu .findAll (MethodCallExpr .class ).forEach (methodCall -> {
206- if (!methodParams .containsKey (methodCall .getNameAsString ()))
207- return ;
208- for (Integer paramIndex : methodParams .get (methodCall .getNameAsString ()).keySet ()) {
209- Expression e = methodCall .getArgument (paramIndex );
210- if (!(e instanceof NameExpr )) {
211- messager .printMessage (Kind .WARNING , "Cannot resolve type for expression: " + e .toString ());
212- continue ;
213- }
214- String name = ((NameExpr ) e ).getNameAsString ();
215- varsToWatch .forEach (p -> {
216- if (p .first .equals (name )) {
217- // boolean valid = isValidSubtype(name, name, annotation);
218- // if (!valid)
219- messager .printMessage (Kind .ERROR ,
220- String .format ("Invalid subtype for method call: " , methodCall .toString ()));
221- }
222- });
223- }
224-
225- });
226- // cu.findAll(AssignExpr.class).forEach(assignExpr -> {
227- // if (!(assignExpr.getTarget() instanceof NameExpr))
228- // return;
229- // NameExpr name = (NameExpr) assignExpr.getTarget();
230- // if (!varsToWatchMap.containsKey(name.toString()))
231- // return;
232-
233- // });
234- // cu.findAll(ForEachStmt.class).forEach(stmt -> {
235-
236- // });
237- // cu.findAll(VariableDeclarationExpr.class).forEach(stmt -> {
238-
239- // });
240- }
241-
242193 private Map <String , Map <Integer , Type >> collectMethodParams (CompilationUnit cu , ClassData classData ) {
243194 Map <String , Map <Integer , Type >> mp = new HashMap <>();
244195 cu .findAll (MethodDeclaration .class ).forEach (dec -> {
@@ -251,33 +202,11 @@ private Map<String, Map<Integer, Type>> collectMethodParams(CompilationUnit cu,
251202 String methodName = dec .getNameAsString ();
252203 if (type .getNameAsString ().equals (classData .className ())) {
253204 mp .putIfAbsent (methodName , new HashMap <>());
254- // mp.get(methodName).put(i,
255- // type.getTypeArguments().get().get(classData.indexOfParam()) );
205+ mp .get (methodName ).put (i ,
206+ type );
256207 }
257208 }
258209 });
259210 return mp ;
260211 }
261-
262- private boolean isValidSubtype (String assigneeType , String assignedType , MyVariance annotation ) {
263- if (!classHierarchy .containsVertex (assigneeType )) {
264- messager .printMessage (Kind .WARNING ,
265- String .format ("%s is not a user defined type, so no subtyping checks can be made" , assigneeType ));
266- return true ;
267- }
268- if (!classHierarchy .containsVertex (assignedType )) {
269- messager .printMessage (Kind .WARNING ,
270- String .format ("%s is not a user defined type, so no subtyping checks can be made" , assignedType ));
271- return true ;
272- }
273- switch (annotation .variance ()) {
274- case COVARIANT :
275- return classHierarchy .isDescendant (assignedType , assigneeType );
276- case CONTRAVARIANT :
277- return classHierarchy .isDescendant (assigneeType , assignedType );
278- default :
279- return false ;
280- }
281- }
282-
283212}
0 commit comments