Merge pull request #6174 from joefarebrother/guava-collections

Java: Model Guava collections package
This commit is contained in:
Chris Smowton
2021-09-23 09:13:24 +01:00
committed by GitHub
91 changed files with 11329 additions and 1090 deletions

View File

@@ -257,9 +257,15 @@ class TestCase extends TTestCase {
if componentStack = baseOutput
then result = "out"
else
result =
SupportMethod::getMethodForContent(componentStack)
.getCall(this.getOutput(componentStack.tail()))
if componentStack.tail() = baseOutput
then
result =
SupportMethod::getMethodFor(this.getOutputType(), componentStack)
.getCall(this.getOutput(componentStack.tail()))
else
result =
SupportMethod::getMethodForContent(componentStack)
.getCall(this.getOutput(componentStack.tail()))
)
}

View File

@@ -145,7 +145,7 @@ private class DefaultGetMethod extends GetMethod {
DefaultGetMethod() { this = "DefaultGet" + contentToken(c) }
string getName() { result = "get" + contentToken(c) }
string getName() { result = "get" + contentToken(c) + "Default" }
override int getPriority() { result = 999 }
@@ -159,12 +159,12 @@ private class DefaultGetMethod extends GetMethod {
override string getCall(string arg) { result = this.getName() + "(" + arg + ")" }
override string getDefinition() {
result = "Object get" + contentToken(c) + "(Object container) { return null; }"
result = "Object get" + contentToken(c) + "Default(Object container) { return null; }"
}
override string getCsvModel() {
result =
"generatedtest;Test;false;" + this.getName() + ";;;" +
"generatedtest;Test;false;" + this.getName() + ";(Object);;" +
getComponentSpec(SummaryComponent::content(c)) + " of Argument[0];ReturnValue;value"
}
}
@@ -201,6 +201,22 @@ private class IteratorGetMethod extends GetMethod {
override string getCall(string arg) { result = "getElement(" + arg + ")" }
}
private class EnumerationGetMethod extends GetMethod {
EnumerationGetMethod() { this = "enumerationgetmethod" }
override predicate appliesTo(Type t, Content c) {
t.(RefType).getASourceSupertype*().hasQualifiedName("java.util", "Enumeration") and
c instanceof CollectionContent
}
override string getDefinition() {
result = "<T> T getElement(Enumeration<T> it) { return it.nextElement(); }"
}
bindingset[arg]
override string getCall(string arg) { result = "getElement(" + arg + ")" }
}
private class OptionalGetMethod extends GetMethod {
OptionalGetMethod() { this = "optionalgetmethod" }
@@ -215,8 +231,8 @@ private class OptionalGetMethod extends GetMethod {
override string getCall(string arg) { result = "getElement(" + arg + ")" }
}
private class MapGetKeyMethod extends GetMethod {
MapGetKeyMethod() { this = "mapgetkeymethod" }
private class MapGetKeytMethod extends GetMethod {
MapGetKeytMethod() { this = "mapgetkeymethod" }
override predicate appliesTo(Type t, Content c) {
t.(RefType).getASourceSupertype*().hasQualifiedName("java.util", "Map") and
@@ -231,8 +247,20 @@ private class MapGetKeyMethod extends GetMethod {
override string getCall(string arg) { result = "getMapKey(" + arg + ")" }
}
private class MapValueGetMethod extends GetMethod {
MapValueGetMethod() { this = "MapValueGetMethod" }
private class MapEntryGetKeyMethod extends GetMethod {
MapEntryGetKeyMethod() { this = "mapentrygetkeymethod" }
override predicate appliesTo(Type t, Content c) {
t.(RefType).getASourceSupertype*().hasQualifiedName("java.util", "Map$Entry") and
c instanceof MapKeyContent
}
bindingset[arg]
override string getCall(string arg) { result = arg + ".getKey()" }
}
private class MapGetValueMethod extends GetMethod {
MapGetValueMethod() { this = "MapGetValueMethod" }
override predicate appliesTo(Type t, Content c) {
t.(RefType).getASourceSupertype*().hasQualifiedName("java.util", "Map") and
@@ -247,6 +275,18 @@ private class MapValueGetMethod extends GetMethod {
override string getCall(string arg) { result = "getMapValue(" + arg + ")" }
}
private class MapEntryGetValueMethod extends GetMethod {
MapEntryGetValueMethod() { this = "mapentrygetvaluemethod" }
override predicate appliesTo(Type t, Content c) {
t.(RefType).getASourceSupertype*().hasQualifiedName("java.util", "Map$Entry") and
c instanceof MapValueContent
}
bindingset[arg]
override string getCall(string arg) { result = arg + ".getValue()" }
}
private class ArrayGetMethod extends GetMethod {
ArrayGetMethod() { this = "arraygetmethod" }
@@ -285,7 +325,7 @@ private class DefaultGenMethod extends GenMethod {
DefaultGenMethod() { this = "DefaultGen" + contentToken(c) }
string getName() { result = "newWith" + contentToken(c) }
string getName() { result = "newWith" + contentToken(c) + "Default" }
override int getPriority() { result = 999 }
@@ -299,12 +339,12 @@ private class DefaultGenMethod extends GenMethod {
override string getCall(string arg) { result = this.getName() + "(" + arg + ")" }
override string getDefinition() {
result = "Object newWith" + contentToken(c) + "(Object element) { return null; }"
result = "Object newWith" + contentToken(c) + "Default(Object element) { return null; }"
}
override string getCsvModel() {
result =
"generatedtest;Test;false;" + this.getName() + ";;;Argument[0];" +
"generatedtest;Test;false;" + this.getName() + ";(Object);;Argument[0];" +
getComponentSpec(SummaryComponent::content(c)) + " of ReturnValue;value"
}
}
@@ -314,7 +354,8 @@ private class ListGenMethod extends GenMethod {
override predicate appliesTo(Type t, Content c) {
exists(GenericType list | list.hasQualifiedName("java.util", "List") |
t = list or list.getAParameterizedType().getASupertype*() = t
t.getErasure() = list.getASourceSupertype*().getErasure() or // cover things like Iterable and Collection
list.getAParameterizedType().getASupertype*() = t
) and
c instanceof CollectionContent
}
@@ -323,12 +364,40 @@ private class ListGenMethod extends GenMethod {
override string getCall(string arg) { result = "List.of(" + arg + ")" }
}
private class SetGenMethod extends GenMethod {
SetGenMethod() { this = "SetGenMethod" }
override predicate appliesTo(Type t, Content c) {
exists(GenericType set | set.hasQualifiedName("java.util", "Set") |
t.getErasure() = set.getErasure()
) and
c instanceof CollectionContent
}
bindingset[arg]
override string getCall(string arg) { result = "Set.of(" + arg + ")" }
}
private class IteratorGenMethod extends GenMethod {
IteratorGenMethod() { this = "IteratorGenMethod" }
override predicate appliesTo(Type t, Content c) {
exists(GenericType set | set.hasQualifiedName("java.util", "Iterator") |
t.getErasure() = set.getErasure()
) and
c instanceof CollectionContent
}
bindingset[arg]
override string getCall(string arg) { result = "List.of(" + arg + ").iterator()" }
}
private class OptionalGenMethod extends GenMethod {
OptionalGenMethod() { this = "optionalgenmethod" }
override predicate appliesTo(Type t, Content c) {
exists(GenericType list | list.hasQualifiedName("java.util", "List") |
list.getAParameterizedType().getASupertype*() = t
exists(GenericType op | op.hasQualifiedName("java.util", "Optional") |
op.getAParameterizedType().getASupertype*() = t
) and
c instanceof CollectionContent
}
@@ -351,6 +420,25 @@ private class MapGenKeyMethod extends GenMethod {
override string getCall(string arg) { result = "Map.of(" + arg + ", null)" }
}
private class MapEntryGenKeyMethod extends GenMethod {
MapEntryGenKeyMethod() { this = "mapentrygenkeymethod" }
override predicate appliesTo(Type t, Content c) {
exists(GenericType map | map.hasQualifiedName("java.util", "Map$Entry") |
map.getAParameterizedType().getASupertype*() = t
) and
c instanceof MapKeyContent
}
override string getDefinition() {
result =
"<K> Map.Entry<K,?> newEntryWithMapKey(K key) { return Map.of(key, null).entrySet().iterator().next(); }"
}
bindingset[arg]
override string getCall(string arg) { result = "newEntryWithMapKey(" + arg + ")" }
}
private class MapGenValueMethod extends GenMethod {
MapGenValueMethod() { this = "mapvaluegenmethod" }
@@ -365,6 +453,25 @@ private class MapGenValueMethod extends GenMethod {
override string getCall(string arg) { result = "Map.of(null, " + arg + ")" }
}
private class MapEntryGenValueMethod extends GenMethod {
MapEntryGenValueMethod() { this = "mapentrygenvaluemethod" }
override predicate appliesTo(Type t, Content c) {
exists(GenericType map | map.hasQualifiedName("java.util", "Map$Entry") |
map.getAParameterizedType().getASupertype*() = t
) and
c instanceof MapValueContent
}
override string getDefinition() {
result =
"<V> Map.Entry<?,V> newEntryWithMapValue(V value) { return Map.of(null, value).entrySet().iterator().next(); }"
}
bindingset[arg]
override string getCall(string arg) { result = "newEntryWithMapValue(" + arg + ")" }
}
/**
* Returns a cast to type `t` if `t` is not `java.lang.Object`, or an empty string otherwise.
*/