Rename packages etc

This commit is contained in:
Dave Syer
2014-04-14 10:38:16 -07:00
parent 46c3d2bccd
commit 8460ea0425
12 changed files with 28 additions and 29 deletions

View File

@@ -1,167 +0,0 @@
/*
* Copyright 2013-2014 the original author or authors.
*
* 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 demo;
import java.lang.annotation.Annotation;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.support.GenericApplicationContext;
import com.google.inject.Binding;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.MembersInjector;
import com.google.inject.Module;
import com.google.inject.Provider;
import com.google.inject.Scope;
import com.google.inject.TypeLiteral;
import com.google.inject.spi.TypeConverterBinding;
public class SpringInjector implements Injector {
private GenericApplicationContext context;
private Injector injector;
public SpringInjector(GenericApplicationContext context) {
this.context = context;
if (context.getBeanNamesForType(Injector.class).length>0) {
injector = context.getBean(Injector.class);
}
}
@Override
public void injectMembers(Object instance) {
context.getAutowireCapableBeanFactory().autowireBean(instance);
}
@Override
public <T> MembersInjector<T> getMembersInjector(TypeLiteral<T> typeLiteral) {
return new MembersInjector<T>() {
@Override
public void injectMembers(T instance) {
context.getAutowireCapableBeanFactory().autowireBean(instance);
}
};
}
@Override
public <T> MembersInjector<T> getMembersInjector(Class<T> type) {
return getMembersInjector(TypeLiteral.get(type));
}
@Override
public Map<Key<?>, Binding<?>> getBindings() {
return null;
}
@Override
public Map<Key<?>, Binding<?>> getAllBindings() {
return null;
}
@Override
public <T> Binding<T> getBinding(Key<T> key) {
return null;
}
@Override
public <T> Binding<T> getBinding(Class<T> type) {
return null;
}
@Override
public <T> Binding<T> getExistingBinding(Key<T> key) {
return null;
}
@Override
public <T> List<Binding<T>> findBindingsByType(TypeLiteral<T> type) {
return null;
}
@Override
public <T> Provider<T> getProvider(Key<T> key) {
// TODO: support for other metadata in the key
@SuppressWarnings("unchecked")
Provider<T> provider = (Provider<T>) getProvider(key.getTypeLiteral().getRawType());
return provider;
}
@Override
public <T> Provider<T> getProvider(Class<T> type) {
if (context.getBeanNamesForType(type).length==0) {
if (injector!=null && injector.getExistingBinding(Key.get(type))!=null) {
return injector.getProvider(type);
}
// TODO: use prototype scope?
context.getDefaultListableBeanFactory().registerBeanDefinition(type.getSimpleName(), new RootBeanDefinition(type));
}
final Class<T> cls = type;
return new Provider<T>() {
@Override
public T get() {
return context.getBean(cls);
}
};
}
@Override
public <T> T getInstance(Key<T> key) {
// TODO: support for other metadata in the key
@SuppressWarnings("unchecked")
T provider = (T) getInstance(key.getTypeLiteral().getRawType());
return provider;
}
@Override
public <T> T getInstance(Class<T> type) {
if (context.getBeanNamesForType(type).length==0) {
if (injector!=null && injector.getExistingBinding(Key.get(type))!=null) {
return injector.getInstance(type);
}
// TODO: use prototype scope?
context.getDefaultListableBeanFactory().registerBeanDefinition(type.getSimpleName(), new RootBeanDefinition(type));
}
return context.getBean(type);
}
@Override
public Injector getParent() {
return null;
}
@Override
public Injector createChildInjector(Iterable<? extends Module> modules) {
return null;
}
@Override
public Injector createChildInjector(Module... modules) {
return null;
}
@Override
public Map<Class<? extends Annotation>, Scope> getScopeBindings() {
return null;
}
@Override
public Set<TypeConverterBinding> getTypeConverterBindings() {
return null;
}
}

View File

@@ -1,60 +0,0 @@
/*
* Copyright 2013-2014 the original author or authors.
*
* 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 demo;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.util.ClassUtils;
import com.google.inject.Binder;
import com.google.inject.Module;
import com.google.inject.Provider;
/**
* @author Dave Syer
*
*/
public class SpringModule implements Module {
private GenericApplicationContext context;
public SpringModule(GenericApplicationContext context) {
this.context = context;
}
@Override
public void configure(Binder binder) {
for (String name : context.getBeanDefinitionNames()) {
BeanDefinition definition = context.getBeanDefinition(name);
if (definition.isAutowireCandidate() && definition.getRole() == AbstractBeanDefinition.ROLE_APPLICATION) {
Class<?> type = context.getType(name);
@SuppressWarnings("unchecked")
final Class<Object> cls = (Class<Object>) type;
Provider<Object> provider = new Provider<Object>() {
@Override
public Object get() {
return context.getBean(cls);
}
};
for (Class<?> iface : ClassUtils.getAllInterfacesForClass(cls)) {
@SuppressWarnings("unchecked")
Class<Object> unchecked = (Class<Object>) iface;
binder.bind(unchecked).toProvider(provider);
}
}
}
}
}

View File

@@ -1,4 +1,4 @@
package demo;
package org.springframework.guice;
import static org.junit.Assert.assertNotNull;

View File

@@ -11,7 +11,7 @@
* specific language governing permissions and limitations under the License.
*/
package demo;
package org.springframework.guice;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;

View File

@@ -11,7 +11,7 @@
* specific language governing permissions and limitations under the License.
*/
package demo;
package org.springframework.guice;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

View File

@@ -11,7 +11,7 @@
* specific language governing permissions and limitations under the License.
*/
package demo;
package org.springframework.guice;
import java.util.Collections;
import java.util.List;

View File

@@ -1,4 +1,4 @@
package demo;
package org.springframework.guice;
import static org.junit.Assert.assertNotNull;

View File

@@ -11,7 +11,7 @@
* specific language governing permissions and limitations under the License.
*/
package demo;
package org.springframework.guice;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

View File

@@ -11,7 +11,7 @@
* specific language governing permissions and limitations under the License.
*/
package demo;
package org.springframework.guice;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;