Support object declarations in DI generator
This commit is contained in:
@@ -123,25 +123,31 @@ public class Dependencies {
|
||||
}
|
||||
|
||||
private void initializeByConstructorCall(Field field, ImmutableStack<Field> neededFor) {
|
||||
Expression initialization = field.getInitialization();
|
||||
DiType type = ((InstantiateType) initialization).getType();
|
||||
//noinspection RedundantCast
|
||||
DiType type = ((InstantiateType) field.getInitialization()).getType();
|
||||
|
||||
if (type.getClazz().isInterface()) {
|
||||
throw new IllegalArgumentException("cannot instantiate interface: " + type.getClazz().getName() + " needed for " + neededFor);
|
||||
Class<?> clazz = type.getClazz();
|
||||
if (clazz.isInterface()) {
|
||||
throw new IllegalArgumentException("cannot instantiate interface: " + clazz.getName() + " needed for " + neededFor);
|
||||
}
|
||||
if (Modifier.isAbstract(type.getClazz().getModifiers())) {
|
||||
throw new IllegalArgumentException("cannot instantiate abstract class: " + type.getClazz().getName() + " needed for " + neededFor);
|
||||
if (Modifier.isAbstract(clazz.getModifiers())) {
|
||||
throw new IllegalArgumentException("cannot instantiate abstract class: " + clazz.getName() + " needed for " + neededFor);
|
||||
}
|
||||
|
||||
// Note: projections are not computed here
|
||||
|
||||
if (isKotlinSingletonObject(clazz)) {
|
||||
field.setInitialization(new ObjectInstanceFieldAccess(clazz));
|
||||
return;
|
||||
}
|
||||
|
||||
// Look for constructor
|
||||
Constructor<?>[] constructors = type.getClazz().getConstructors();
|
||||
Constructor<?>[] constructors = clazz.getConstructors();
|
||||
if (constructors.length == 0 || !Modifier.isPublic(constructors[0].getModifiers())) {
|
||||
throw new IllegalArgumentException("No constructor: " + type.getClazz().getName() + " needed for " + neededFor);
|
||||
throw new IllegalArgumentException("No constructor: " + clazz.getName() + " needed for " + neededFor);
|
||||
}
|
||||
if (constructors.length > 1) {
|
||||
throw new IllegalArgumentException("Too many constructors in " + type.getClazz().getName() + " needed for " + neededFor);
|
||||
throw new IllegalArgumentException("Too many constructors in " + clazz.getName() + " needed for " + neededFor);
|
||||
}
|
||||
Constructor<?> constructor = constructors[0];
|
||||
|
||||
@@ -161,6 +167,16 @@ public class Dependencies {
|
||||
field.setInitialization(dependency);
|
||||
}
|
||||
|
||||
private static boolean isKotlinSingletonObject(Class<?> clazz) {
|
||||
try {
|
||||
clazz.getDeclaredField("INSTANCE$");
|
||||
return true;
|
||||
}
|
||||
catch (NoSuchFieldException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<Field> satisfyDependencies() {
|
||||
for (Field field : Lists.newArrayList(allFields)) {
|
||||
satisfyDependenciesFor(field, LinkedImmutableStack.<Field>empty());
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 2010-2014 JetBrains s.r.o.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* 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.jetbrains.jet.di;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
public class ObjectInstanceFieldAccess implements Expression {
|
||||
private final Class<?> clazz;
|
||||
|
||||
public ObjectInstanceFieldAccess(@NotNull Class<?> clazz) {
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String renderAsCode() {
|
||||
return clazz.getSimpleName() + ".INSTANCE$";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Collection<DiType> getTypesToImport() {
|
||||
return Collections.singleton(new DiType(clazz));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public DiType getType() {
|
||||
return new DiType(clazz);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return clazz.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user