Skip to content

Commit

Permalink
Progress
Browse files Browse the repository at this point in the history
  • Loading branch information
dstepanov committed Nov 14, 2024
1 parent 14da794 commit a1d885f
Show file tree
Hide file tree
Showing 11 changed files with 745 additions and 119 deletions.
7 changes: 7 additions & 0 deletions sourcegen-bytecode-writer/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ plugins {
id("io.micronaut.build.internal.sourcegen-module")
}

repositories {
maven(
url = uri("https://www.jetbrains.com/intellij-repository/releases/")
)
}

dependencies {
api(projects.sourcegenModel)
compileOnly(mn.micronaut.core.processor)
Expand All @@ -10,6 +16,7 @@ dependencies {
api(libs.asm.util)
testImplementation(mn.micronaut.core.processor)
testImplementation(libs.junit.jupiter.engine)
testImplementation("com.jetbrains.intellij.java:java-decompiler-engine:242.23726.103")
}

tasks.withType<Test> {
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.micronaut.sourcegen.model.TypeDef;
import org.objectweb.asm.Type;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -35,6 +36,7 @@
*/
final class TypeUtils {

private static final Type OBJECT_TYPE = Type.getType(Object.class);
private static final Pattern ARRAY_PATTERN = Pattern.compile("(\\[])+$");

static Type getType(TypeDef typeDef, @Nullable ObjectDef objectDef) {
Expand All @@ -55,10 +57,10 @@ static Type getType(TypeDef typeDef, @Nullable ObjectDef objectDef) {
}
if (typeDef instanceof TypeDef.Wildcard wildcard) {
if (!wildcard.lowerBounds().isEmpty()) {
return getType(wildcard.lowerBounds().get(0), objectDef);
return getBoundsType(wildcard.lowerBounds(), objectDef);
}
if (!wildcard.upperBounds().isEmpty()) {
return getType(wildcard.upperBounds().get(0), objectDef);
return getBoundsType(wildcard.upperBounds(), objectDef);
}
return Type.getType(Object.class);
}
Expand All @@ -68,25 +70,36 @@ static Type getType(TypeDef typeDef, @Nullable ObjectDef objectDef) {
TypeDef.TypeVariable tvDef = classDef.getTypeVariables().stream()
.filter(tv -> tv.name().equals(typeVariable.name())).findFirst()
.orElse(null);
if (tvDef != null && !tvDef.bounds().isEmpty()) {
return getType(tvDef.bounds().get(0), objectDef);
if (tvDef != null) {
return getBoundsType(tvDef.bounds(), objectDef);
}
}
if (objectDef instanceof InterfaceDef interfaceDef) {
TypeDef.TypeVariable tvDef = interfaceDef.getTypeVariables().stream()
.filter(tv -> tv.name().equals(typeVariable.name())).findFirst()
.orElse(null);
if (tvDef != null && !tvDef.bounds().isEmpty()) {
return getType(tvDef.bounds().get(0), objectDef);
if (tvDef != null) {
return getBoundsType(tvDef.bounds(), objectDef);
}
}
return Type.getType(Object.class);
}
return getType(typeVariable.bounds().get(0), objectDef);
return getBoundsType(typeVariable.bounds(), objectDef);
}
throw new IllegalStateException("Unsupported type: " + typeDef);
}

private static Type getBoundsType(List<TypeDef> bounds, ObjectDef objectDef) {
// Select first non-object type
for (TypeDef bound : bounds) {
Type type = getType(bound, objectDef);
if (!type.equals(OBJECT_TYPE)) {
return type;
}
}
return OBJECT_TYPE;
}

public static Type getType(TypeDef.Primitive primitive) {
return Type.getType(JavaModelUtils.NAME_TO_TYPE_MAP.get(primitive.name()));
}
Expand Down
Loading

0 comments on commit a1d885f

Please sign in to comment.