Skip to content

added fix and test for #180 #182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ List<MetaData> createFactoryMethodMeta() {
}

MetaData createMeta() {
String type;
if (beanType.getNestingKind().isNested()) {
type = beanType.getEnclosingElement().toString() + "$" + beanType.getSimpleName();
} else {
type = beanType.getQualifiedName().toString();
}
MetaData metaData = new MetaData(type, name);
metaData.update(this);
return metaData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,22 @@ class SimpleBeanWriter {
this.context = context;
TypeElement origin = beanReader.getBeanType();
this.originName = origin.getQualifiedName().toString();
this.shortName = origin.getSimpleName().toString();
this.packageName = Util.packageOf(originName);
if (origin.getNestingKind().isNested()) {
this.packageName = Util.nestedPackageOf(originName);
this.shortName = Util.nestedShortName(originName);
} else {
this.packageName = Util.packageOf(originName);
this.shortName = Util.shortName(originName);
}
this.suffix = beanReader.suffix();
this.proxied = beanReader.isGenerateProxy();
}

private Writer createFileWriter() throws IOException {
String originName = this.originName;
if (beanReader.getBeanType().getNestingKind().isNested()) {
originName = originName.replace(shortName, shortName.replace(".", "$"));
}
JavaFileObject jfo = context.createWriter(originName + suffix);
return jfo.openWriter();
}
Expand Down Expand Up @@ -66,7 +75,7 @@ private void writeGenericProviders() {
final String sn = type.shortName();
writer.append(" public static Provider<");
type.writeShort(writer);
writer.append("> provider%s(Builder builder) {", sn).eol();
writer.append("> provider%s(Builder builder) {", sn).eol();
writer.append(" return builder.getProviderFor(%s.class, TYPE_%s);", shortName, sn).eol();
writer.append(" }").eol();
}
Expand Down Expand Up @@ -221,6 +230,10 @@ private void writeClassStart() {
if (beanReader.isRequestScopedController()) {
writer.append(Constants.AT_SINGLETON).eol();
}
String shortName = this.shortName;
if (beanReader.getBeanType().getNestingKind().isNested()) {
shortName = shortName.replace(".", "$");
}
writer.append("public class ").append(shortName).append(suffix).append(" ");
if (beanReader.isRequestScopedController()) {
writer.append("implements ");
Expand Down
20 changes: 20 additions & 0 deletions inject-generator/src/main/java/io/avaje/inject/generator/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import jakarta.inject.Named;
import jakarta.inject.Qualifier;

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.type.DeclaredType;
Expand Down Expand Up @@ -46,6 +47,15 @@ static String trimGenerics(String type) {
return type.substring(0, i);
}

static String nestedPackageOf(String cls) {
int pos = cls.lastIndexOf('.');
if (pos < 0) {
return "";
}
pos = cls.lastIndexOf('.', pos - 1);
return (pos == -1) ? "" : cls.substring(0, pos);
}

static String packageOf(String cls) {
int pos = cls.lastIndexOf('.');
return (pos == -1) ? "" : cls.substring(0, pos);
Expand Down Expand Up @@ -73,6 +83,16 @@ static String initLower(String name) {
return sb.toString();
}

static String nestedShortName(String fullType) {
int pos = fullType.lastIndexOf('.');
if (pos < 0) {
return fullType;
} else {
pos = fullType.lastIndexOf('.', pos - 1);
return pos < 0 ? fullType : fullType.substring(pos + 1);
}
}

static String shortName(String fullType) {
int p = fullType.lastIndexOf('.');
if (p == -1) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.example.coffee;

import io.avaje.inject.BeanScope;
import org.example.coffee.inner.InnerClassBean;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class InnerClassBeanTest {

@Test
void test() {
try (BeanScope context = BeanScope.newBuilder().build()) {
InnerClassBean.MyBean bean = context.get(InnerClassBean.MyBean.class);
assertThat(bean).isNotNull();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.example.coffee.inner;

import jakarta.inject.Singleton;

public class InnerClassBean {
@Singleton
public static class MyBean {
}
}