Skip to content

Commit

Permalink
Refactored the Wire components and renamed to IoC.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Feb 17, 2016
1 parent cd99e33 commit ae9e795
Show file tree
Hide file tree
Showing 25 changed files with 216 additions and 194 deletions.
18 changes: 9 additions & 9 deletions rapidoid-commons/src/main/java/org/rapidoid/cls/Cls.java
Expand Up @@ -142,7 +142,7 @@ public static TypeKind kindOf(Object value) {

public static void setFieldValue(Object instance, String fieldName, Object value) {
try {
for (Class<?> c = instance.getClass(); c != Object.class; c = c.getSuperclass()) {
for (Class<?> c = instance.getClass(); c.getSuperclass() != null; c = c.getSuperclass()) {
try {
Field field = c.getDeclaredField(fieldName);
field.setAccessible(true);
Expand Down Expand Up @@ -181,7 +181,7 @@ public static <T> T getFieldValue(Object instance, String fieldName, T defaultVa

public static Object getFieldValue(Object instance, String fieldName) {
try {
for (Class<?> c = instance.getClass(); c != Object.class; c = c.getSuperclass()) {
for (Class<?> c = instance.getClass(); c.getSuperclass() != null; c = c.getSuperclass()) {
try {
Field field = c.getDeclaredField(fieldName);
return getFieldValue(field, instance);
Expand Down Expand Up @@ -212,7 +212,7 @@ public static List<Annotation> getAnnotations(Class<?> clazz) {
List<Annotation> allAnnotations = U.list();

try {
for (Class<?> c = clazz; c != Object.class; c = c.getSuperclass()) {
for (Class<?> c = clazz; c.getSuperclass() != null; c = c.getSuperclass()) {
Annotation[] annotations = c.getDeclaredAnnotations();
for (Annotation an : annotations) {
allAnnotations.add(an);
Expand All @@ -230,7 +230,7 @@ public static List<Field> getFields(Class<?> clazz) {
List<Field> allFields = U.list();

try {
for (Class<?> c = clazz; c != Object.class; c = c.getSuperclass()) {
for (Class<?> c = clazz; c.getSuperclass() != null; c = c.getSuperclass()) {
Field[] fields = c.getDeclaredFields();
for (Field field : fields) {
allFields.add(field);
Expand All @@ -248,7 +248,7 @@ public static List<Field> getFieldsAnnotated(Class<?> clazz, Class<? extends Ann
List<Field> annotatedFields = U.list();

try {
for (Class<?> c = clazz; c != Object.class; c = c.getSuperclass()) {
for (Class<?> c = clazz; c.getSuperclass() != null; c = c.getSuperclass()) {
Field[] fields = c.getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(annotation)) {
Expand All @@ -268,7 +268,7 @@ public static List<Method> getMethods(Class<?> clazz) {
List<Method> methods = U.list();

try {
for (Class<?> c = clazz; c != Object.class; c = c.getSuperclass()) {
for (Class<?> c = clazz; c.getSuperclass() != null; c = c.getSuperclass()) {
for (Method method : c.getDeclaredMethods()) {
methods.add(method);
}
Expand All @@ -285,7 +285,7 @@ public static List<Method> getMethodsAnnotated(Class<?> clazz, Class<? extends A
List<Method> annotatedMethods = U.list();

try {
for (Class<?> c = clazz; c != Object.class; c = c.getSuperclass()) {
for (Class<?> c = clazz; c.getSuperclass() != null; c = c.getSuperclass()) {
Method[] methods = c.getDeclaredMethods();
for (Method method : methods) {
if (method.isAnnotationPresent(annotation)) {
Expand All @@ -305,7 +305,7 @@ public static List<Method> getMethodsNamed(Class<?> clazz, String name) {
List<Method> annotatedMethods = U.list();

try {
for (Class<?> c = clazz; c != Object.class; c = c.getSuperclass()) {
for (Class<?> c = clazz; c.getSuperclass() != null; c = c.getSuperclass()) {
Method[] methods = c.getDeclaredMethods();
for (Method method : methods) {
if (method.getName().equals(name)) {
Expand Down Expand Up @@ -403,7 +403,7 @@ public static Class<?>[] getImplementedInterfaces(Class<?> clazz) {
try {
List<Class<?>> interfaces = new LinkedList<Class<?>>();

for (Class<?> c = clazz; c != Object.class; c = c.getSuperclass()) {
for (Class<?> c = clazz; c.getSuperclass() != null; c = c.getSuperclass()) {
for (Class<?> interf : c.getInterfaces()) {
interfaces.add(interf);
}
Expand Down
36 changes: 36 additions & 0 deletions rapidoid-commons/src/main/java/org/rapidoid/ioc/ClassMetadata.java
@@ -0,0 +1,36 @@
package org.rapidoid.ioc;

import org.rapidoid.annotation.*;
import org.rapidoid.cls.Cls;
import org.rapidoid.commons.Coll;
import org.rapidoid.lambda.F3;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Set;

@Authors("Nikolche Mihajlovski")
@Since("5.1.0")
public class ClassMetadata {

final Class<?> clazz;

final List<Field> injectableFields;

final List<Field> sessionFields;

final List<Field> localFields;

final Set<Object> injectors = Coll.synchronizedSet();

final List<F3<Object, Object, Method, Object[]>> interceptors = Coll.synchronizedList();

public ClassMetadata(Class<?> clazz) {
this.clazz = clazz;
this.injectableFields = Cls.getFieldsAnnotated(clazz, Inject.class);
this.sessionFields = Cls.getFieldsAnnotated(clazz, Session.class);
this.localFields = Cls.getFieldsAnnotated(clazz, Local.class);
}

}
58 changes: 58 additions & 0 deletions rapidoid-commons/src/main/java/org/rapidoid/ioc/IoC.java
@@ -0,0 +1,58 @@
package org.rapidoid.ioc;

/*
* #%L
* rapidoid-commons
* %%
* Copyright (C) 2014 - 2016 Nikolche Mihajlovski and contributors
* %%
* 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.
* #L%
*/

import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;

import java.util.Map;

@Authors("Nikolche Mihajlovski")
@Since("5.1.0")
public class IoC {

private static final IoCContext DEFAULT_CONTEXT = new IoCContext();

public static synchronized IoCContext getDefault() {
return DEFAULT_CONTEXT;
}

public static synchronized void manage(Object... classesOrInstances) {
DEFAULT_CONTEXT.manage(classesOrInstances);
}

public static synchronized <T> T singleton(Class<T> type) {
return DEFAULT_CONTEXT.singleton(type);
}

public static synchronized <T> T autowire(T target) {
return DEFAULT_CONTEXT.autowire(target);
}

public static synchronized <T> T inject(T target) {
return DEFAULT_CONTEXT.inject(target);
}

public static synchronized <T> T inject(T target, Map<String, Object> properties) {
return DEFAULT_CONTEXT.inject(target, properties);
}

}

0 comments on commit ae9e795

Please sign in to comment.