Skip to content

Commit

Permalink
Determinism in JavaType.Class.build
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Sep 19, 2020
1 parent 35b17f3 commit b3784f3
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 23 deletions.
@@ -0,0 +1,24 @@
/*
* Copyright 2020 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java

import org.junit.jupiter.api.extension.ExtendWith
import org.openrewrite.DebugOnly
import org.openrewrite.java.tree.JavaTypeTest

@DebugOnly
@ExtendWith(JavaParserResolver::class)
class Java11JavaTypeTest: Java11Test(), JavaTypeTest
45 changes: 38 additions & 7 deletions rewrite-java/src/main/java/org/openrewrite/java/tree/JavaType.java
Expand Up @@ -52,7 +52,7 @@ public interface JavaType extends Serializable {
*/
static JavaType buildType(String typeName) {
JavaType.Primitive primitive = JavaType.Primitive.fromKeyword(typeName);
if(primitive != null) {
if (primitive != null) {
return primitive;
} else {
return JavaType.Class.build(typeName);
Expand Down Expand Up @@ -132,6 +132,11 @@ public boolean deepEquals(JavaType type) {
return type instanceof ShallowClass &&
fullyQualifiedName.equals(((ShallowClass) type).fullyQualifiedName);
}

@Override
public String toString() {
return "ShallowClass{" + + '}';
}
}

@Getter
Expand Down Expand Up @@ -206,12 +211,28 @@ public static Class build(String fullyQualifiedName,

synchronized (flyweights) {
Set<JavaType.Class> variants = flyweights.computeIfAbsent(fullyQualifiedName, fqn -> HashObjSets.newMutableSet());
return (!relaxedClassTypeMatching ? variants.stream().filter(v -> v.deepEquals(test)) : variants.stream())
.findAny()
.orElseGet(() -> {
variants.add(test);
return test;
});

if (relaxedClassTypeMatching) {
return variants.stream()
.findFirst()
.orElseGet(() -> {
variants.add(test);
return test;
});
} else {
return variants.stream().filter(v -> v.deepEquals(test))
.findFirst()
.orElseGet(() -> {
if (test.supertype == null) {
return variants.stream().findFirst().orElseGet(() -> {
variants.add(test);
return test;
});
}
variants.add(test);
return test;
});
}
}
}

Expand Down Expand Up @@ -278,6 +299,11 @@ public boolean deepEquals(@Nullable JavaType type) {
TypeUtils.deepEquals(supertype, c.supertype) &&
TypeUtils.deepEquals(typeParameters, c.typeParameters);
}

@Override
public String toString() {
return "Class{" + fullyQualifiedName + '}';
}
}

@EqualsAndHashCode(callSuper = false)
Expand All @@ -289,6 +315,11 @@ class Cyclic extends FullyQualified {
public boolean deepEquals(JavaType type) {
return this.equals(type);
}

@Override
public String toString() {
return "Cyclic{" + fullyQualifiedName + '}';
}
}

@Data
Expand Down
Expand Up @@ -123,24 +123,9 @@ public J buildDeclaration(J.ClassDecl insertionScope, String snippet, JavaType..
.map(it -> (JavaType.Class) it)
.toArray(JavaType.Class[]::new);

JavaType.FullyQualified[] genericTypes = Stream.concat(
stream(types)
.filter(it -> it instanceof JavaType.GenericTypeVariable),
stream(imports)
.filter(it -> it.getTypeParameters().size() > 0)
.flatMap(it -> it.getTypeParameters().stream()))
.map(it -> (JavaType.FullyQualified) it)
.toArray(JavaType.FullyQualified[]::new);
String typeParameters = "";
if (genericTypes.length > 0) {
typeParameters = "<" + stream(genericTypes)
.map(JavaType.FullyQualified::getFullyQualifiedName)
.collect(joining(", ", "", "")) + ">";
}

String source = stream(imports)
.map(i -> "import " + i.getFullyQualifiedName() + ";").collect(joining("\n", "", "\n\n")) +
"class CodeSnippet" + typeParameters + " {\n" +
"class CodeSnippet {\n" +
scopeVariables +
StringUtils.trimIndent(snippet) + "\n" +
"}";
Expand Down

0 comments on commit b3784f3

Please sign in to comment.