Commit cf643034 authored by Lionel Seinturier's avatar Lionel Seinturier

Support inherited non public field injection point in component...

Support inherited non public field injection point in component implementations (requested by Philippe.)
Refactor and add tests on injection points.
parent 3aaece1c
Tinfi 1.3
---------
* fix for services and references with generic types (reported by Philippe)
* support inherited non public field injection point in component
implementations (requested by Philippe)
Tinfi 1.2.1
......
......@@ -24,9 +24,6 @@ runtime/oo-dyn/
Having a generic initializer should bypass this difficulty.
runtime-oo/
* recurse in the inheritance hierarchy when seeking annotated fields for
injection points to fetch private and protected inherited fields (requested by
Philippe)
* remove the @EagerDestroy annotation which is misleading wrt @EagerInit which
is a class annotation whereas @EagerDestroy is a method annotation and create
@Start and @Stop method annotations
......
......@@ -91,7 +91,6 @@
<adl>org.ow2.frascati.tinfi.Constructor</adl>
<adl>org.ow2.frascati.tinfi.DuplicateDestroy</adl>
<adl>org.ow2.frascati.tinfi.DuplicateInit</adl>
<adl>org.ow2.frascati.tinfi.DuplicateReference</adl>
<adl>org.ow2.frascati.tinfi.OneWay</adl>
<adl>org.ow2.frascati.tinfi.PolicySetsHandler</adl>
<adl>org.ow2.frascati.tinfi.PolicySets</adl>
......@@ -190,7 +189,6 @@
<adl>org.ow2.frascati.tinfi.Constructor</adl>
<adl>org.ow2.frascati.tinfi.DuplicateDestroy</adl>
<adl>org.ow2.frascati.tinfi.DuplicateInit</adl>
<adl>org.ow2.frascati.tinfi.DuplicateReference</adl>
<adl>org.ow2.frascati.tinfi.OneWay</adl>
<!-- adl>org.ow2.frascati.tinfi.PolicySetsHandler</adl>
<adl>org.ow2.frascati.tinfi.PolicySets</adl -->
......
......@@ -30,11 +30,21 @@ import org.osoa.sca.annotations.Reference;
*
* @author Lionel Seinturier <Lionel.Seinturier@univ-lille1.fr>
*/
public class DuplicateReferenceImpl {
public class DuplicateReferenceImpl extends DuplicateReferenceInheritedImpl {
@Reference
public Runnable r;
@Reference
public void setR( Runnable value ) {}
@Reference public Runnable r1;
@Reference public void setR1( Runnable value ) {}
@Reference public void setR2( Runnable value ) {}
@Reference protected Runnable r3;
}
/**
* @since 1.3
*/
class DuplicateReferenceInheritedImpl {
@Reference protected Runnable r2;
@SuppressWarnings("unused")
@Reference private Runnable r3;
}
......@@ -30,7 +30,7 @@ import org.osoa.sca.annotations.Reference;
*
* @author Lionel Seinturier <Lionel.Seinturier@univ-lille1.fr>
*/
public class ReferenceNotFoundImpl {
public class ReferenceNotFoundImpl extends ReferenceInheritedNotFoundImpl {
@Reference
public Object publicFieldRef;
......@@ -55,3 +55,20 @@ public class ReferenceNotFoundImpl {
@Reference
public void setBadMethodRef3() {}
}
/**
* Super class for component implementation used for testing references.
*
* @author Lionel Seinturier <Lionel.Seinturier@univ-lille1.fr>
* @see ReferenceNotFoundImpl
* @since 1.3
*/
class ReferenceInheritedNotFoundImpl {
@Reference
protected Object inheritedProtectedFieldRef;
@Reference
protected Object inheritedPrivateFieldRef;
}
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE definition PUBLIC "-//objectweb.org//DTD Fractal ADL 2.0//EN" "classpath://org/objectweb/fractal/adl/xml/standard.dtd">
<definition name="org.ow2.frascati.tinfi.DuplicateReference" >
<interface name="r" role="client" contingency="optional" signature="java.lang.Runnable" />
<content class="org.ow2.frascati.tinfi.DuplicateReferenceImpl"/>
<controller desc="scaPrimitive" />
</definition>
......@@ -25,9 +25,6 @@ package org.ow2.frascati.tinfi;
import org.junit.Assert;
import org.junit.Test;
import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.api.control.IllegalLifeCycleException;
import org.objectweb.fractal.api.factory.InstantiationException;
import org.osoa.sca.annotations.Reference;
import org.ow2.frascati.tinfi.control.content.DuplicationInjectionPointException;
import org.ow2.frascati.tinfi.control.content.InjectionPointMap;
......@@ -75,20 +72,36 @@ public class AnnotatedElementsManagerTestCase {
ipm.put("protectedFieldRef");
ipm.put("publicMethodRef");
ipm.put("protectedMethodRef");
ipm.put("inheritedProtectedFieldRef");
ipm.put("inheritedPrivateFieldRef");
}
@Test
public void testDuplicateReferenceException()
throws
ClassNotFoundException, IllegalAccessException,
IllegalLifeCycleException, NoSuchInterfaceException,
java.lang.InstantiationException {
throws NoSuchInjectionPointException {
try {
String adl = getClass().getPackage().getName()+".DuplicateReference";
TinfiDomain.getComponent(adl);
Assert.fail();
}
catch (InstantiationException e) {}
InjectionPointMap<Reference> ipm =
new InjectionPointMap<Reference>(
DuplicateReferenceImpl.class,Reference.class);
try {
ipm.put("r1");
Assert.fail(
"DuplicationInjectionPointException should have been thrown");
}
catch (DuplicationInjectionPointException e) {}
try {
ipm.put("r2");
Assert.fail(
"DuplicationInjectionPointException should have been thrown");
}
catch (DuplicationInjectionPointException e) {}
try {
ipm.put("r3");
Assert.fail(
"DuplicationInjectionPointException should have been thrown");
}
catch (DuplicationInjectionPointException e) {}
}
}
......@@ -102,7 +102,8 @@ extends HashMap<String,InjectionPoint<T>> {
throws NoSuchInjectionPointException, DuplicationInjectionPointException {
AccessibleObject[] aos =
Util.getAllAnnotatedSettersAndFields(cl,annotType);
Util.getAllAnnotatedSettersAndFields(cl,annotType);
boolean found = false;
for (AccessibleObject ao : aos) {
/*
......@@ -126,11 +127,18 @@ extends HashMap<String,InjectionPoint<T>> {
T annot = ao.getAnnotation(annotType);
InjectionPoint<T> ip = InjectionPointImpl.getInjectionPoint(ao,annot);
put(name,ip);
return;
/*
* Needed to be sure we iterate on all elements of aos to check
* for duplicate elements.
*/
found = true;
}
}
throw new NoSuchInjectionPointException(name,cl,annotType);
if(!found) {
throw new NoSuchInjectionPointException(name,cl,annotType);
}
}
/**
......
......@@ -28,8 +28,10 @@ import java.lang.reflect.AccessibleObject;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
......@@ -115,28 +117,48 @@ public class Util {
* @param cl the class
* @since 1.1.2
*/
public static Field[] getAllFields( Class<?> cl ) {
private static Field[] getAllFields( Class<?> cl ) {
/*
* Use a set to accomodate duplicate insertions of public declared
* elements, e.g. public methods which are returned both by
* getDeclaredMethods and getMethods.
* Declared private to avoid regressions due to a change in the
* semantics of the method. This method used to return declared and
* public inherited fields. Since 1.3, this method returns all fields.
*
* This method is used by getAllAnnotatedSettersAndFields().
*
* We can move back this method to public as soon as we are sure that
* it is not used in other locations than the previous one where the
* change could introduce regressions.
*/
Set<Field> result = new HashSet<Field>();
List<Field> result = new ArrayList<Field>();
addAllFields(cl,result);
return result.toArray(new Field[result.size()]);
}
/**
* Add to the specified list, all fields declared by the specified class and
* all fields declared by parent classes, except {@link java.lang.Object},
* of the specified class. All fields are included whatever their access
* modifier is.
*
* @param cl the class
* @param list the list where fields are to be added
* @since 1.3
*/
public static void addAllFields( Class<?> cl, List<Field> list ) {
// Declared fields
Field[] fields = cl.getDeclaredFields();
for (Field field : fields) {
result.add(field);
list.add(field);
}
// Inherited public fields
fields = cl.getFields();
for (Field field : fields) {
result.add(field);
}
return result.toArray(new Field[result.size()]);
// Recurse in the parent class up to java.lang.Object
Class<?> supercl = cl.getSuperclass();
if( ! supercl.equals(Object.class) ) {
addAllFields(supercl,list);
}
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment