diff options
author | Roberto E. Escobar | 2012-10-19 22:55:02 +0000 |
---|---|---|
committer | Roberto E. Escobar | 2012-10-19 22:56:14 +0000 |
commit | a32311fc44430fb21bebd794086d8606c312c951 (patch) | |
tree | e78042bbb8e597ce9f6695770945388f37695a3b /plugins/org.eclipse.osee.orcs.db.mock | |
parent | aafe7f0fe15d13e2e79391c1e5866f1a92c75342 (diff) | |
download | org.eclipse.osee-a32311fc44430fb21bebd794086d8606c312c951.tar.gz org.eclipse.osee-a32311fc44430fb21bebd794086d8606c312c951.tar.xz org.eclipse.osee-a32311fc44430fb21bebd794086d8606c312c951.zip |
feature: Create AnnotationProcessor
Diffstat (limited to 'plugins/org.eclipse.osee.orcs.db.mock')
3 files changed, 42 insertions, 39 deletions
diff --git a/plugins/org.eclipse.osee.orcs.db.mock/META-INF/MANIFEST.MF b/plugins/org.eclipse.osee.orcs.db.mock/META-INF/MANIFEST.MF index 5bdd8d4b881..c9daea2071c 100644 --- a/plugins/org.eclipse.osee.orcs.db.mock/META-INF/MANIFEST.MF +++ b/plugins/org.eclipse.osee.orcs.db.mock/META-INF/MANIFEST.MF @@ -11,6 +11,7 @@ Import-Package: org.eclipse.osee.framework.core.data, org.eclipse.osee.framework.database.core, org.eclipse.osee.framework.h2, org.eclipse.osee.framework.jdk.core.util, + org.eclipse.osee.framework.jdk.core.util.annotation, org.eclipse.osee.framework.jdk.core.util.network, org.junit;version="4.8.2", org.junit.rules;version="4.8.2", diff --git a/plugins/org.eclipse.osee.orcs.db.mock/src/org/eclipse/osee/orcs/db/mock/OsgiRule.java b/plugins/org.eclipse.osee.orcs.db.mock/src/org/eclipse/osee/orcs/db/mock/OsgiRule.java index 1210d513480..9dabc96f42b 100644 --- a/plugins/org.eclipse.osee.orcs.db.mock/src/org/eclipse/osee/orcs/db/mock/OsgiRule.java +++ b/plugins/org.eclipse.osee.orcs.db.mock/src/org/eclipse/osee/orcs/db/mock/OsgiRule.java @@ -10,14 +10,18 @@ *******************************************************************************/ package org.eclipse.osee.orcs.db.mock; -import java.lang.reflect.Field; -import org.eclipse.osee.framework.core.exception.OseeCoreException; -import org.eclipse.osee.orcs.db.mock.internal.OsgiUtil; +import java.lang.annotation.Annotation; +import java.util.HashMap; +import java.util.Map; +import org.eclipse.osee.framework.jdk.core.util.annotation.AnnotationProcessor; +import org.eclipse.osee.framework.jdk.core.util.annotation.FieldAnnotationHandler; +import org.eclipse.osee.orcs.db.mock.internal.OsgiServiceFieldAnnotationHandler; import org.junit.rules.TestWatchman; import org.junit.runners.model.FrameworkMethod; public class OsgiRule extends TestWatchman { + private static final AnnotationProcessor processor = createProcessor(); private final Object[] objects; public OsgiRule(Object... objects) { @@ -27,47 +31,17 @@ public class OsgiRule extends TestWatchman { @Override public void starting(FrameworkMethod method) { try { - for (Object object : objects) { - initAnnotations(object); - } + processor.initAnnotations(objects); } catch (Exception ex) { throw new RuntimeException(ex); } } - public static void initAnnotations(Object testClass) throws Exception { - if (testClass == null) { - throw new OseeCoreException( - "testClass cannot be null. For info how to use @OsgiService annotations see examples"); - } + private static AnnotationProcessor createProcessor() { + Map<Class<? extends Annotation>, FieldAnnotationHandler<?>> annotationHandlers = + new HashMap<Class<? extends Annotation>, FieldAnnotationHandler<?>>(); - Class<?> clazz = testClass.getClass(); - while (clazz != Object.class) { - scan(testClass, clazz); - clazz = clazz.getSuperclass(); - } - } - - private static void scan(Object object, Class<?> clazz) throws Exception { - Field[] fields = clazz.getDeclaredFields(); - for (Field field : fields) { - if (field.isAnnotationPresent(OsgiService.class)) { - OsgiService annotation = field.getAnnotation(OsgiService.class); - injectToFields(annotation, object, field); - } - } - } - - private static void injectToFields(OsgiService annotation, Object object, Field field) throws Exception { - boolean wasAccessible = field.isAccessible(); - field.setAccessible(true); - try { - Object service = OsgiUtil.getService(field.getType()); - field.set(object, service); - } catch (Error e) { - throw new Exception("Problems injecting dependencies in " + field.getName(), e); - } finally { - field.setAccessible(wasAccessible); - } + annotationHandlers.put(OsgiService.class, new OsgiServiceFieldAnnotationHandler()); + return new AnnotationProcessor(annotationHandlers); } }
\ No newline at end of file diff --git a/plugins/org.eclipse.osee.orcs.db.mock/src/org/eclipse/osee/orcs/db/mock/internal/OsgiServiceFieldAnnotationHandler.java b/plugins/org.eclipse.osee.orcs.db.mock/src/org/eclipse/osee/orcs/db/mock/internal/OsgiServiceFieldAnnotationHandler.java new file mode 100644 index 00000000000..cc3b5d80073 --- /dev/null +++ b/plugins/org.eclipse.osee.orcs.db.mock/src/org/eclipse/osee/orcs/db/mock/internal/OsgiServiceFieldAnnotationHandler.java @@ -0,0 +1,28 @@ +/******************************************************************************* + * Copyright (c) 2011 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.orcs.db.mock.internal; + +import java.lang.reflect.Field; +import org.eclipse.osee.framework.jdk.core.util.annotation.AbstractFieldAnnotationHandler; +import org.eclipse.osee.orcs.db.mock.OsgiService; + +/** + * @author Roberto E. Escobar + */ +public class OsgiServiceFieldAnnotationHandler extends AbstractFieldAnnotationHandler<OsgiService> { + + @Override + public void handleAnnotation(OsgiService annotation, Object object, Field field) throws Exception { + Object service = OsgiUtil.getService(field.getType()); + injectToFields(annotation, object, field, service); + } + +} |