Skip to content

Commit 50a28f3

Browse files
committed
Updated symbol table construction to omit certain source roots (e.g., test/resources);
added symbol table test for duplicate signatures with generics and varargs Signed-off-by: Saurabh Sinha <[email protected]>
1 parent 20ebf41 commit 50a28f3

File tree

3 files changed

+173
-2
lines changed

3 files changed

+173
-2
lines changed

src/main/java/com/ibm/cldk/SymbolTable.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.nio.file.Path;
55
import java.nio.file.Paths;
66
import java.util.*;
7+
import java.util.regex.Pattern;
78
import java.util.stream.Collectors;
89
import java.util.stream.IntStream;
910

@@ -586,7 +587,7 @@ private static Pair<String, Callable> processCallableDeclaration(CallableDeclara
586587
*/
587588
private static String getTypeErasureSignature(CallableDeclaration callableDecl) {
588589
try {
589-
StringBuffer signature = new StringBuffer(
590+
StringBuilder signature = new StringBuilder(
590591
(callableDecl instanceof MethodDeclaration) ? callableDecl.getNameAsString() : "<init>"
591592
);
592593
List<String> erasureParameterTypes = new ArrayList<>();
@@ -618,7 +619,7 @@ private static String getTypeErasureSignature(CallableDeclaration callableDecl)
618619
* @return String representing type erasure signature
619620
*/
620621
private static String getTypeErasureSignature(ResolvedMethodLikeDeclaration methodDecl) {
621-
StringBuffer signature = new StringBuffer(methodDecl.getName());
622+
StringBuilder signature = new StringBuilder(methodDecl.getName());
622623
List<String> erasureParameterTypes = new ArrayList<>();
623624
for (int i = 0; i < methodDecl.getNumberOfParams(); i++) {
624625
erasureParameterTypes.add(methodDecl.getParam(i).getType().erasure().describe());
@@ -1122,6 +1123,20 @@ private static String resolveType(Type type) {
11221123
* project
11231124
* @throws IOException
11241125
*/
1126+
private static final String[] EXCLUDED_SOURCE_ROOTS = {
1127+
Paths.get("src", "test", "resources").toString(),
1128+
Paths.get("src", "it", "resources").toString(),
1129+
Paths.get("src", "xdocs-examples").toString()
1130+
};
1131+
private static boolean excludeSourceRoot(Path sourceRoot) {
1132+
for (String excludedSrcRoot : EXCLUDED_SOURCE_ROOTS) {
1133+
if (Pattern.compile(excludedSrcRoot).matcher(sourceRoot.toString()).find()) {
1134+
return true;
1135+
}
1136+
}
1137+
return false;
1138+
}
1139+
11251140
public static Pair<Map<String, JavaCompilationUnit>, Map<String, List<Problem>>> extractAll(Path projectRootPath)
11261141
throws IOException {
11271142
SymbolSolverCollectionStrategy symbolSolverCollectionStrategy = new SymbolSolverCollectionStrategy();
@@ -1131,6 +1146,9 @@ public static Pair<Map<String, JavaCompilationUnit>, Map<String, List<Problem>>>
11311146
Map<String, JavaCompilationUnit> symbolTable = new LinkedHashMap<>();
11321147
Map<String, List<Problem>> parseProblems = new HashMap<>();
11331148
for (SourceRoot sourceRoot : projectRoot.getSourceRoots()) {
1149+
if (excludeSourceRoot(sourceRoot.getRoot())) {
1150+
continue;
1151+
}
11341152
for (ParseResult<CompilationUnit> parseResult : sourceRoot.tryToParse()) {
11351153
if (parseResult.isSuccessful()) {
11361154
CompilationUnit compilationUnit = LexicalPreservingPrinter.setup(parseResult.getResult().get());
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.ibm.cldk;
2+
3+
import com.ibm.cldk.entities.Callable;
4+
import com.ibm.cldk.entities.JavaCompilationUnit;
5+
import com.ibm.cldk.entities.Type;
6+
import org.junit.jupiter.api.Assertions;
7+
import org.junit.jupiter.api.Test;
8+
9+
import java.io.BufferedReader;
10+
import java.io.IOException;
11+
import java.io.InputStream;
12+
import java.io.InputStreamReader;
13+
import java.nio.charset.StandardCharsets;
14+
import java.nio.file.Paths;
15+
import java.util.Map;
16+
import java.util.stream.Collectors;
17+
18+
public class SymbolTableTest {
19+
20+
private String getJavaCodeForTestResource(String resourcePath) {
21+
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(resourcePath);
22+
assert inputStream != null;
23+
return new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))
24+
.lines()
25+
.collect(Collectors.joining("\n"));
26+
}
27+
28+
@Test
29+
public void testExtractSingleGenricsDuplicateSignature() throws IOException {
30+
String javaCode = getJavaCodeForTestResource("test-applications/generics-varargs-duplicate-signature-test/Validate.java");
31+
Map<String, JavaCompilationUnit> symbolTable = SymbolTable.extractSingle(javaCode).getLeft();
32+
Assertions.assertEquals(1, symbolTable.size());
33+
Map<String, Type> typeDeclaration = symbolTable.values().iterator().next().getTypeDeclarations();
34+
Assertions.assertEquals(1, typeDeclaration.size());
35+
Map<String, Callable> callables = typeDeclaration.values().iterator().next().getCallableDeclarations();
36+
Assertions.assertEquals(17, callables.size());
37+
}
38+
39+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import java.util.Collection;
2+
import java.util.Map;
3+
import java.util.Objects;
4+
import java.util.function.Supplier;
5+
6+
public class Validate {
7+
8+
private static final String DEFAULT_NOT_EMPTY_ARRAY_EX_MESSAGE = "The validated array is empty";
9+
private static final String DEFAULT_NOT_EMPTY_CHAR_SEQUENCE_EX_MESSAGE =
10+
"The validated character sequence is empty";
11+
private static final String DEFAULT_NOT_EMPTY_COLLECTION_EX_MESSAGE = "The validated collection is empty";
12+
private static final String DEFAULT_NOT_EMPTY_MAP_EX_MESSAGE = "The validated map is empty";
13+
private static final String DEFAULT_VALID_INDEX_ARRAY_EX_MESSAGE = "The validated array index is invalid: %d";
14+
private static final String DEFAULT_VALID_INDEX_CHAR_SEQUENCE_EX_MESSAGE =
15+
"The validated character sequence index is invalid: %d";
16+
private static final String DEFAULT_VALID_INDEX_COLLECTION_EX_MESSAGE =
17+
"The validated collection index is invalid: %d";
18+
19+
private static String getMessage(final String message, final Object... values) {
20+
return ArrayUtils.isEmpty(values) ? message : String.format(message, values);
21+
}
22+
23+
public static <T extends Collection<?>> T notEmpty(final T collection) {
24+
return notEmpty(collection, DEFAULT_NOT_EMPTY_COLLECTION_EX_MESSAGE);
25+
}
26+
27+
public static <T extends Map<?, ?>> T notEmpty(final T map) {
28+
return notEmpty(map, DEFAULT_NOT_EMPTY_MAP_EX_MESSAGE);
29+
}
30+
31+
public static <T extends CharSequence> T notEmpty(final T chars) {
32+
return notEmpty(chars, DEFAULT_NOT_EMPTY_CHAR_SEQUENCE_EX_MESSAGE);
33+
}
34+
35+
public static <T extends Collection<?>> T notEmpty(final T collection, final String message, final Object... values) {
36+
Objects.requireNonNull(collection, toSupplier(message, values));
37+
if (collection.isEmpty()) {
38+
throw new IllegalArgumentException(getMessage(message, values));
39+
}
40+
return collection;
41+
}
42+
43+
public static <T extends Map<?, ?>> T notEmpty(final T map, final String message, final Object... values) {
44+
Objects.requireNonNull(map, toSupplier(message, values));
45+
if (map.isEmpty()) {
46+
throw new IllegalArgumentException(getMessage(message, values));
47+
}
48+
return map;
49+
}
50+
51+
public static <T extends CharSequence> T notEmpty(final T chars, final String message, final Object... values) {
52+
Objects.requireNonNull(chars, toSupplier(message, values));
53+
if (chars.length() == 0) {
54+
throw new IllegalArgumentException(getMessage(message, values));
55+
}
56+
return chars;
57+
}
58+
59+
public static <T> T[] notEmpty(final T[] array) {
60+
return notEmpty(array, DEFAULT_NOT_EMPTY_ARRAY_EX_MESSAGE);
61+
}
62+
63+
public static <T> T[] notEmpty(final T[] array, final String message, final Object... values) {
64+
Objects.requireNonNull(array, toSupplier(message, values));
65+
if (array.length == 0) {
66+
throw new IllegalArgumentException(getMessage(message, values));
67+
}
68+
return array;
69+
}
70+
71+
private static Supplier<String> toSupplier(final String message, final Object... values) {
72+
return () -> getMessage(message, values);
73+
}
74+
75+
private static Supplier<String> toSupplier(final String message, final Object values) {
76+
return () -> getMessage(message, values);
77+
}
78+
79+
public static <T extends Collection<?>> T validIndex(final T collection, final int index) {
80+
return validIndex(collection, index, DEFAULT_VALID_INDEX_COLLECTION_EX_MESSAGE, Integer.valueOf(index));
81+
}
82+
83+
public static <T extends CharSequence> T validIndex(final T chars, final int index) {
84+
return validIndex(chars, index, DEFAULT_VALID_INDEX_CHAR_SEQUENCE_EX_MESSAGE, Integer.valueOf(index));
85+
}
86+
87+
public static <T extends Collection<?>> T validIndex(final T collection, final int index, final String message, final Object... values) {
88+
Objects.requireNonNull(collection, "collection");
89+
if (index < 0 || index >= collection.size()) {
90+
throw new IndexOutOfBoundsException(getMessage(message, values));
91+
}
92+
return collection;
93+
}
94+
95+
public static <T extends CharSequence> T validIndex(final T chars, final int index, final String message, final Object... values) {
96+
Objects.requireNonNull(chars, "chars");
97+
if (index < 0 || index >= chars.length()) {
98+
throw new IndexOutOfBoundsException(getMessage(message, values));
99+
}
100+
return chars;
101+
}
102+
103+
public static <T> T[] validIndex(final T[] array, final int index) {
104+
return validIndex(array, index, DEFAULT_VALID_INDEX_ARRAY_EX_MESSAGE, Integer.valueOf(index));
105+
}
106+
107+
public static <T> T[] validIndex(final T[] array, final int index, final String message, final Object... values) {
108+
Objects.requireNonNull(array, "array");
109+
if (index < 0 || index >= array.length) {
110+
throw new IndexOutOfBoundsException(getMessage(message, values));
111+
}
112+
return array;
113+
}
114+
}

0 commit comments

Comments
 (0)