3
3
import java .io .IOException ;
4
4
import java .nio .file .Path ;
5
5
import java .nio .file .Paths ;
6
- import java .util .ArrayList ;
7
- import java .util .Collections ;
8
- import java .util .HashMap ;
9
- import java .util .HashSet ;
10
- import java .util .LinkedHashMap ;
11
- import java .util .List ;
12
- import java .util .Map ;
13
- import java .util .Objects ;
14
- import java .util .Optional ;
15
- import java .util .Set ;
6
+ import java .util .*;
16
7
import java .util .stream .Collectors ;
17
8
import java .util .stream .IntStream ;
18
9
25
16
import com .github .javaparser .ast .nodeTypes .NodeWithJavadoc ;
26
17
import com .github .javaparser .ast .stmt .*;
27
18
import com .github .javaparser .printer .lexicalpreservation .LexicalPreservingPrinter ;
19
+ import com .github .javaparser .resolution .declarations .ResolvedMethodLikeDeclaration ;
28
20
import com .ibm .cldk .entities .*;
29
21
import com .ibm .cldk .javaee .EntrypointsFinderFactory ;
30
22
import org .apache .commons .lang3 .tuple .Pair ;
@@ -501,7 +493,7 @@ private static Pair<String, Callable> processCallableDeclaration(CallableDeclara
501
493
callableNode .setFilePath (filePath );
502
494
503
495
// add callable signature
504
- callableNode .setSignature (callableDecl . getSignature (). asString ( ));
496
+ callableNode .setSignature (getTypeErasureSignature ( callableDecl ));
505
497
506
498
// add comment associated with method/constructor
507
499
callableNode .setComments (
@@ -581,11 +573,62 @@ private static Pair<String, Callable> processCallableDeclaration(CallableDeclara
581
573
callableNode .setVariableDeclarations (getVariableDeclarations (body ));
582
574
callableNode .setCyclomaticComplexity (getCyclomaticComplexity (callableDecl ));
583
575
584
- String callableSignature = (callableDecl instanceof MethodDeclaration ) ? callableDecl .getSignature ().asString ()
585
- : callableDecl .getSignature ().asString ().replace (callableDecl .getSignature ().getName (), "<init>" );
576
+ String callableSignature = getTypeErasureSignature (callableDecl );
586
577
return Pair .of (callableSignature , callableNode );
587
578
}
588
579
580
+ /**
581
+ * Returns type erasure signature for the given callable. Returns regular signature if an
582
+ * error occurs in getting erased types.
583
+ *
584
+ * @param callableDecl: Callable to compute type erasure signature for
585
+ * @return String representing type erasure or regular signature
586
+ */
587
+ private static String getTypeErasureSignature (CallableDeclaration callableDecl ) {
588
+ try {
589
+ StringBuffer signature = new StringBuffer (
590
+ (callableDecl instanceof MethodDeclaration ) ? callableDecl .getNameAsString () : "<init>"
591
+ );
592
+ List <String > erasureParameterTypes = new ArrayList <>();
593
+ for (Object param : callableDecl .getParameters ()) {
594
+ Parameter parameter = (Parameter ) param ;
595
+ ResolvedType resolvedType = parameter .getType ().resolve ();
596
+ if (parameter .isVarArgs ()) {
597
+ erasureParameterTypes .add (resolvedType .describe () + "[]" );
598
+ } else {
599
+ erasureParameterTypes .add (resolvedType .erasure ().describe ());
600
+ }
601
+ }
602
+ signature .append ("(" );
603
+ signature .append (String .join (", " , erasureParameterTypes ));
604
+ signature .append (")" );
605
+ return signature .toString ();
606
+ } catch (Throwable e ) {
607
+ Log .warn ("Could not compute type erasure signature for " +callableDecl .getSignature ().asString ()+
608
+ "; computing regular signature" );
609
+ return callableDecl .getSignature ().asString ();
610
+ }
611
+ }
612
+
613
+ /**
614
+ * Returns type erasure signature for the given method or constructor declaration
615
+ * resolved for a call site.
616
+ *
617
+ * @param methodDecl: Resolved method/constructor to compute type erasure signature for
618
+ * @return String representing type erasure signature
619
+ */
620
+ private static String getTypeErasureSignature (ResolvedMethodLikeDeclaration methodDecl ) {
621
+ StringBuffer signature = new StringBuffer (methodDecl .getName ());
622
+ List <String > erasureParameterTypes = new ArrayList <>();
623
+ for (int i = 0 ; i < methodDecl .getNumberOfParams (); i ++) {
624
+ erasureParameterTypes .add (methodDecl .getParam (i ).getType ().erasure ().describe ());
625
+ }
626
+ signature .append ("(" );
627
+ signature .append (String .join (", " , erasureParameterTypes ));
628
+ signature .append (")" );
629
+ return signature .toString ();
630
+ }
631
+
589
632
private static boolean isEntryPointMethod (CallableDeclaration callableDecl ) {
590
633
return EntrypointsFinderFactory .getEntrypointFinders ()
591
634
.anyMatch (finder -> finder .isEntrypointMethod (callableDecl ));
@@ -835,8 +878,8 @@ private static List<CallSite> getCallSites(Optional<BlockStmt> callableBody) {
835
878
// resolve callee and get signature
836
879
String calleeSignature = "" ;
837
880
try {
838
- calleeSignature = methodCallExpr .resolve (). getSignature ( );
839
- } catch (RuntimeException exception ) {
881
+ calleeSignature = getTypeErasureSignature ( methodCallExpr .resolve ());
882
+ } catch (Throwable exception ) {
840
883
Log .debug ("Could not resolve method call: " + methodCallExpr + ": " + exception .getMessage ());
841
884
}
842
885
@@ -845,7 +888,7 @@ private static List<CallSite> getCallSites(Optional<BlockStmt> callableBody) {
845
888
try {
846
889
ResolvedMethodDeclaration resolvedMethodDeclaration = methodCallExpr .resolve ();
847
890
accessSpecifier = resolvedMethodDeclaration .accessSpecifier ();
848
- } catch (RuntimeException exception ) {
891
+ } catch (Throwable exception ) {
849
892
Log .debug ("Could not resolve access specifier for method call: " + methodCallExpr + ": "
850
893
+ exception .getMessage ());
851
894
}
@@ -901,8 +944,8 @@ private static List<CallSite> getCallSites(Optional<BlockStmt> callableBody) {
901
944
// resolve callee and get signature
902
945
String calleeSignature = "" ;
903
946
try {
904
- calleeSignature = objectCreationExpr .resolve (). getSignature ( );
905
- } catch (RuntimeException exception ) {
947
+ calleeSignature = getTypeErasureSignature ( objectCreationExpr .resolve ());
948
+ } catch (Throwable exception ) {
906
949
Log .debug ("Could not resolve constructor call: " + objectCreationExpr + ": " + exception .getMessage ());
907
950
}
908
951
@@ -1039,7 +1082,7 @@ private static String resolveExpression(Expression expression) {
1039
1082
if (resolvedType .isReferenceType () || resolvedType .isUnionType ()) {
1040
1083
return resolvedType .describe ();
1041
1084
}
1042
- } catch (RuntimeException exception ) {
1085
+ } catch (Throwable exception ) {
1043
1086
Log .debug ("Could not resolve expression: " + expression + ": " + exception .getMessage ());
1044
1087
unresolvedExpressions .add (expression .toString ());
1045
1088
}
@@ -1060,7 +1103,7 @@ private static String resolveType(Type type) {
1060
1103
if (!unresolvedTypes .contains (type .asString ())) {
1061
1104
try {
1062
1105
return type .resolve ().describe ();
1063
- } catch (RuntimeException e ) {
1106
+ } catch (Throwable e ) {
1064
1107
Log .warn ("Could not resolve type: " + type .asString () + ": " + e .getMessage ());
1065
1108
unresolvedTypes .add (type .asString ());
1066
1109
}
0 commit comments