support @PostConstruct in our magnificent injector
This commit is contained in:
@@ -74,6 +74,8 @@ public class InjectorForJavaSemanticServices {
|
||||
namespaceFactoryImpl.setModuleDescriptor(moduleDescriptor);
|
||||
namespaceFactoryImpl.setTrace(bindingTrace);
|
||||
|
||||
psiClassFinderForJvm.initialize();
|
||||
|
||||
}
|
||||
|
||||
public JavaSemanticServices getJavaSemanticServices() {
|
||||
|
||||
@@ -204,6 +204,8 @@ public class InjectorForTopDownAnalyzerForJvm {
|
||||
javaTypeTransformer.setJavaSemanticServices(javaSemanticServices);
|
||||
javaTypeTransformer.setResolver(javaDescriptorResolver);
|
||||
|
||||
psiClassFinderForJvm.initialize();
|
||||
|
||||
}
|
||||
|
||||
public TopDownAnalyzer getTopDownAnalyzer() {
|
||||
|
||||
+5
-2
@@ -31,6 +31,7 @@ import org.jetbrains.jet.lang.resolve.FqName;
|
||||
import org.jetbrains.jet.lang.resolve.java.alt.AltClassFinder;
|
||||
import org.jetbrains.jet.plugin.JetFileType;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
@@ -46,6 +47,10 @@ public class PsiClassFinderForJvm implements PsiClassFinder {
|
||||
@Inject
|
||||
public void setProject(Project project) {
|
||||
this.project = project;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void initialize() {
|
||||
this.altClassFinder = new AltClassFinder(project);
|
||||
this.javaSearchScope = new DelegatingGlobalSearchScope(GlobalSearchScope.allScope(project)) {
|
||||
@Override
|
||||
@@ -57,8 +62,6 @@ public class PsiClassFinderForJvm implements PsiClassFinder {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public PsiClass findPsiClass(@NotNull FqName qualifiedName) {
|
||||
|
||||
@@ -25,6 +25,7 @@ import com.intellij.openapi.util.text.StringUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
@@ -243,11 +244,36 @@ public class DependencyInjectorGenerator {
|
||||
out.println();
|
||||
}
|
||||
}
|
||||
|
||||
// call @PostConstruct
|
||||
for (Field field : fields) {
|
||||
// TODO: type of field may be different from type of object
|
||||
List<Method> postConstructMethods = getPostConstructMethods(field.getType());
|
||||
for (Method postConstruct : postConstructMethods) {
|
||||
out.println(indent + field.getName() + "." + postConstruct.getName() + "();");
|
||||
}
|
||||
if (postConstructMethods.size() > 0) {
|
||||
out.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
out.println(" }");
|
||||
}
|
||||
|
||||
private static List<Method> getPostConstructMethods(Class<?> clazz) {
|
||||
List<Method> r = Lists.newArrayList();
|
||||
for (Method method : clazz.getMethods()) {
|
||||
if (method.getAnnotation(PostConstruct.class) != null) {
|
||||
if (method.getParameterTypes().length != 0) {
|
||||
throw new IllegalStateException("@PostConstruct method must have no arguments: " + method);
|
||||
}
|
||||
r.add(method);
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
private void generateGetters(PrintStream out) {
|
||||
String indent0 = " ";
|
||||
String indent1 = indent0 + INDENT_STEP;
|
||||
|
||||
Reference in New Issue
Block a user