You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by dr...@apache.org on 2011/01/28 18:25:40 UTC
svn commit: r1064787 - in /tapestry/tapestry5/trunk/tapestry-ioc/src:
main/java/org/apache/tapestry5/ioc/internal/
main/java/org/apache/tapestry5/ioc/internal/util/
test/java/org/apache/tapestry5/ioc/internal/
Author: drobiazko
Date: Fri Jan 28 17:25:39 2011
New Revision: 1064787
URL: http://svn.apache.org/viewvc?rev=1064787&view=rev
Log:
TAP5-1385: Added the possibility to define service id with @Named annotation
Added:
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/NamedServiceModule.java (with props)
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/NamedViaAnnotationServiceImpl.java (with props)
Modified:
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/DefaultModuleDefImpl.java
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/ServiceBinderImpl.java
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/InternalUtils.java
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/DefaultModuleDefImplTest.java
Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/DefaultModuleDefImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/DefaultModuleDefImpl.java?rev=1064787&r1=1064786&r2=1064787&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/DefaultModuleDefImpl.java (original)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/DefaultModuleDefImpl.java Fri Jan 28 17:25:39 2011
@@ -1,4 +1,4 @@
-// Copyright 2006, 2007, 2008, 2009, 2010 The Apache Software Foundation
+// Copyright 2006, 2007, 2008, 2009, 2010, 2011 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -420,15 +420,9 @@ public class DefaultModuleDefImpl implem
*/
private void addServiceDef(final Method method, boolean modulePreventsServiceDecoration)
{
- ServiceId serviceIdAnnotation = method.getAnnotation(ServiceId.class);
+ String serviceId = InternalUtils.getServiceId(method);
- String serviceId;
-
- if (serviceIdAnnotation != null)
- {
- serviceId = serviceIdAnnotation.value();
- }
- else
+ if(serviceId == null)
{
serviceId = stripMethodPrefix(method, BUILD_METHOD_NAME_PREFIX);
}
Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/ServiceBinderImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/ServiceBinderImpl.java?rev=1064787&r1=1064786&r2=1064787&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/ServiceBinderImpl.java (original)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/ServiceBinderImpl.java Fri Jan 28 17:25:39 2011
@@ -1,4 +1,4 @@
-// Copyright 2007, 2008, 2009, 2010 The Apache Software Foundation
+// Copyright 2007, 2008, 2009, 2010, 2011 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -247,16 +247,12 @@ public class ServiceBinderImpl implement
// Set defaults for the other properties.
eagerLoad = serviceImplementation.getAnnotation(EagerLoad.class) != null;
-
- ServiceId serviceIdAnnotation = serviceImplementation.getAnnotation(ServiceId.class);
-
- if (serviceIdAnnotation != null)
- {
- serviceId = serviceIdAnnotation.value();
- }
- else
+
+ serviceId = InternalUtils.getServiceId(serviceImplementation);
+
+ if(serviceId == null)
{
- serviceId = serviceInterface.getSimpleName();
+ serviceId = serviceInterface.getSimpleName();
}
Scope scope = serviceImplementation.getAnnotation(Scope.class);
Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/InternalUtils.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/InternalUtils.java?rev=1064787&r1=1064786&r2=1064787&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/InternalUtils.java (original)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/InternalUtils.java Fri Jan 28 17:25:39 2011
@@ -1,4 +1,4 @@
-// Copyright 2006, 2007, 2008, 2009, 2010 The Apache Software Foundation
+// Copyright 2006, 2007, 2008, 2009, 2010, 2011 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
+import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
@@ -70,6 +71,7 @@ import org.apache.tapestry5.ioc.annotati
import org.apache.tapestry5.ioc.annotations.InjectResource;
import org.apache.tapestry5.ioc.annotations.InjectService;
import org.apache.tapestry5.ioc.annotations.PostInjection;
+import org.apache.tapestry5.ioc.annotations.ServiceId;
import org.apache.tapestry5.ioc.def.ContributionDef;
import org.apache.tapestry5.ioc.def.ContributionDef2;
import org.apache.tapestry5.ioc.def.DecoratorDef;
@@ -1253,5 +1255,38 @@ public class InternalUtils
{
return uuidGenerator.incrementAndGet();
}
+
+ /**
+ * Extracts the service id from the passed annotated element. First the {@link ServiceId} annotation is checked.
+ * If present, its value is returned. Otherwise {@link Named} annotation is checked. If present, its value is returned.
+ * If neither of the annotations is present, <code>null</code> value is returned
+ *
+ * @param annotated annotated element to get annotations from
+ *
+ * @since 5.3.0
+ */
+ public static String getServiceId(AnnotatedElement annotated)
+ {
+ ServiceId serviceIdAnnotation = annotated.getAnnotation(ServiceId.class);
+
+ if (serviceIdAnnotation != null)
+ {
+ return serviceIdAnnotation.value();
+ }
+
+ Named namedAnnotation = annotated.getAnnotation(Named.class);
+
+ if(namedAnnotation != null)
+ {
+ String value = namedAnnotation.value();
+
+ if(InternalUtils.isNonBlank(value))
+ {
+ return value;
+ }
+ }
+
+ return null;
+ }
}
Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/DefaultModuleDefImplTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/DefaultModuleDefImplTest.java?rev=1064787&r1=1064786&r2=1064787&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/DefaultModuleDefImplTest.java (original)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/DefaultModuleDefImplTest.java Fri Jan 28 17:25:39 2011
@@ -1,4 +1,4 @@
-// Copyright 2006, 2007, 2008, 2009, 2010 The Apache Software Foundation
+// Copyright 2006, 2007, 2008, 2009, 2010, 2011 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -143,6 +143,42 @@ public class DefaultModuleDefImplTest ex
}
@Test
+ public void default_service_id_from_method_named_annotation()
+ {
+ Logger logger = mockLogger();
+
+ replay();
+
+ ModuleDef def = new DefaultModuleDefImpl(NamedServiceModule.class, logger, null);
+
+ assertEquals(def.getServiceIds().size(), 2);
+
+ ServiceDef sd = def.getServiceDef("BazService");
+
+ assertEquals(sd.getServiceId(), "BazService");
+
+ verify();
+ }
+
+ @Test
+ public void default_service_id_from_named_annotation()
+ {
+ Logger logger = mockLogger();
+
+ replay();
+
+ ModuleDef def = new DefaultModuleDefImpl(NamedServiceModule.class, logger, null);
+
+ assertEquals(def.getServiceIds().size(), 2);
+
+ ServiceDef sd = def.getServiceDef("QuuxService");
+
+ assertEquals(sd.getServiceId(), "QuuxService");
+
+ verify();
+ }
+
+ @Test
public void default_service_id_from_return_type()
{
Logger logger = mockLogger();
Added: tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/NamedServiceModule.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/NamedServiceModule.java?rev=1064787&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/NamedServiceModule.java (added)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/NamedServiceModule.java Fri Jan 28 17:25:39 2011
@@ -0,0 +1,34 @@
+// Copyright 2011 The Apache Software Foundation
+//
+// 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.apache.tapestry5.ioc.internal;
+
+import javax.inject.Named;
+
+import org.apache.tapestry5.ioc.ServiceBinder;
+
+public class NamedServiceModule
+{
+ @Named("QuuxService")
+ public static Runnable buildSomething()
+ {
+ return new ServiceIdViaMethodAnnotationServiceImpl();
+ }
+
+ public static void bind(ServiceBinder binder)
+ {
+ binder.bind(Runnable.class, NamedViaAnnotationServiceImpl.class);
+ }
+
+}
Propchange: tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/NamedServiceModule.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/NamedViaAnnotationServiceImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/NamedViaAnnotationServiceImpl.java?rev=1064787&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/NamedViaAnnotationServiceImpl.java (added)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/NamedViaAnnotationServiceImpl.java Fri Jan 28 17:25:39 2011
@@ -0,0 +1,27 @@
+// Copyright 2011 The Apache Software Foundation
+//
+// 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.apache.tapestry5.ioc.internal;
+
+import javax.inject.Named;
+
+@Named("BazService")
+public class NamedViaAnnotationServiceImpl implements Runnable
+{
+
+ public void run()
+ {
+ }
+
+}
Propchange: tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/NamedViaAnnotationServiceImpl.java
------------------------------------------------------------------------------
svn:mime-type = text/plain