You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cz...@apache.org on 2012/05/22 08:05:30 UTC
svn commit: r1341311 - in
/felix/sandbox/cziegeler/scrplugin/annotations/src/main/java/org/apache/felix:
scr/annotations/Services.java scrplugin/SCRAnnotationProcessor.java
Author: cziegeler
Date: Tue May 22 06:05:29 2012
New Revision: 1341311
URL: http://svn.apache.org/viewvc?rev=1341311&view=rev
Log:
New scr plugin
Added:
felix/sandbox/cziegeler/scrplugin/annotations/src/main/java/org/apache/felix/scr/annotations/Services.java (with props)
Modified:
felix/sandbox/cziegeler/scrplugin/annotations/src/main/java/org/apache/felix/scrplugin/SCRAnnotationProcessor.java
Added: felix/sandbox/cziegeler/scrplugin/annotations/src/main/java/org/apache/felix/scr/annotations/Services.java
URL: http://svn.apache.org/viewvc/felix/sandbox/cziegeler/scrplugin/annotations/src/main/java/org/apache/felix/scr/annotations/Services.java?rev=1341311&view=auto
==============================================================================
--- felix/sandbox/cziegeler/scrplugin/annotations/src/main/java/org/apache/felix/scr/annotations/Services.java (added)
+++ felix/sandbox/cziegeler/scrplugin/annotations/src/main/java/org/apache/felix/scr/annotations/Services.java Tue May 22 06:05:29 2012
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.felix.scr.annotations;
+
+import java.lang.annotation.*;
+
+/**
+ * Allows to define multiple {@link Service} annotations for one type.
+ *
+ * @deprecated Since 1.5. It's now possible to use multiple classes in the {@link Service} annotation.
+ */
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.CLASS)
+@Documented
+@Deprecated
+public @interface Services {
+
+ /**
+ * List of {@link Service} annotations.
+ */
+ Service[] value();
+
+}
Propchange: felix/sandbox/cziegeler/scrplugin/annotations/src/main/java/org/apache/felix/scr/annotations/Services.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: felix/sandbox/cziegeler/scrplugin/annotations/src/main/java/org/apache/felix/scr/annotations/Services.java
------------------------------------------------------------------------------
svn:keywords = author date id revision rev url
Propchange: felix/sandbox/cziegeler/scrplugin/annotations/src/main/java/org/apache/felix/scr/annotations/Services.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: felix/sandbox/cziegeler/scrplugin/annotations/src/main/java/org/apache/felix/scrplugin/SCRAnnotationProcessor.java
URL: http://svn.apache.org/viewvc/felix/sandbox/cziegeler/scrplugin/annotations/src/main/java/org/apache/felix/scrplugin/SCRAnnotationProcessor.java?rev=1341311&r1=1341310&r2=1341311&view=diff
==============================================================================
--- felix/sandbox/cziegeler/scrplugin/annotations/src/main/java/org/apache/felix/scrplugin/SCRAnnotationProcessor.java (original)
+++ felix/sandbox/cziegeler/scrplugin/annotations/src/main/java/org/apache/felix/scrplugin/SCRAnnotationProcessor.java Tue May 22 06:05:29 2012
@@ -33,6 +33,7 @@ import org.apache.felix.scr.annotations.
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
+import org.apache.felix.scr.annotations.Services;
import org.apache.felix.scrplugin.description.ClassDescription;
import org.apache.felix.scrplugin.description.ComponentConfigurationPolicy;
import org.apache.felix.scrplugin.description.ComponentDescription;
@@ -62,37 +63,36 @@ public class SCRAnnotationProcessor impl
* @throws SCRDescriptorFailureException
* @see org.apache.felix.scrplugin.AnnotationProcessor#process(ScannedClass, ClassDescription)
*/
- public void process(final ScannedClass scannedClass,
- final ClassDescription describedClass)
- throws SCRDescriptorFailureException, SCRDescriptorException {
+ public void process(final ScannedClass scannedClass, final ClassDescription describedClass)
+ throws SCRDescriptorFailureException, SCRDescriptorException {
final List<ClassAnnotation> componentTags = scannedClass.getClassAnnotations(Component.class.getName());
scannedClass.processed(componentTags);
- for(final ClassAnnotation cad : componentTags) {
+ for (final ClassAnnotation cad : componentTags) {
describedClass.add(createComponent(cad, scannedClass));
}
// search for the component descriptions and use the first one
final List<ComponentDescription> componentDescs = describedClass.getDescriptions(ComponentDescription.class);
ComponentDescription found = null;
- if ( !componentDescs.isEmpty() ) {
+ if (!componentDescs.isEmpty()) {
found = componentDescs.get(0);
}
- if ( found != null ) {
+ if (found != null) {
final ComponentDescription cd = found;
// search for methods
final List<MethodAnnotation> methodTags = scannedClass.getMethodAnnotations(null);
- for(final MethodAnnotation m : methodTags) {
- if ( m.getName().equals(Activate.class.getName()) ) {
+ for (final MethodAnnotation m : methodTags) {
+ if (m.getName().equals(Activate.class.getName())) {
cd.setActivate(new MethodDescription(m.getAnnotatedMethod()));
scannedClass.processed(m);
- } else if ( m.getName().equals(Deactivate.class.getName()) ) {
+ } else if (m.getName().equals(Deactivate.class.getName())) {
cd.setDeactivate(new MethodDescription(m.getAnnotatedMethod()));
scannedClass.processed(m);
- } else if ( m.getName().equals(Modified.class.getName()) ) {
+ } else if (m.getName().equals(Modified.class.getName())) {
cd.setModified(new MethodDescription(m.getAnnotatedMethod()));
scannedClass.processed(m);
}
@@ -101,10 +101,25 @@ public class SCRAnnotationProcessor impl
}
// service tags
+ final List<ClassAnnotation> allServiceTags = new ArrayList<ClassAnnotation>();
final List<ClassAnnotation> serviceTags = scannedClass.getClassAnnotations(Service.class.getName());
- if ( serviceTags.size() > 0 ) {
+ if (serviceTags.size() > 0) {
scannedClass.processed(serviceTags);
- describedClass.add(createService(serviceTags, scannedClass));
+ allServiceTags.addAll(serviceTags);
+ }
+ // services tags - class level
+ final List<ClassAnnotation> servicesTags = scannedClass.getClassAnnotations(Services.class.getName());
+ if (servicesTags.size() > 0) {
+ scannedClass.processed(servicesTags);
+ for (final ClassAnnotation cad : servicesTags) {
+ final ClassAnnotation[] values = (ClassAnnotation[]) cad.getValue("value");
+ if (values != null) {
+ allServiceTags.addAll(Arrays.asList(values));
+ }
+ }
+ }
+ if (allServiceTags.size() > 0) {
+ describedClass.add(createService(allServiceTags, scannedClass));
}
// reference - class level
@@ -120,9 +135,9 @@ public class SCRAnnotationProcessor impl
// properties - class level
final List<ClassAnnotation> propsClassTags = scannedClass.getClassAnnotations(Properties.class.getName());
scannedClass.processed(propsClassTags);
- for(final ClassAnnotation cad : propsClassTags) {
+ for (final ClassAnnotation cad : propsClassTags) {
final ClassAnnotation[] values = (ClassAnnotation[]) cad.getValue("value");
- if ( values != null ) {
+ if (values != null) {
createProperties(Arrays.asList(values), describedClass);
}
}
@@ -147,8 +162,11 @@ public class SCRAnnotationProcessor impl
/**
* Create a component description.
- * @param cad The component annotation for the class.
- * @param scannedClass The scanned class.
+ *
+ * @param cad
+ * The component annotation for the class.
+ * @param scannedClass
+ * The scanned class.
*/
private ComponentDescription createComponent(final ClassAnnotation cad, final ScannedClass scannedClass) {
final ComponentDescription component = new ComponentDescription(cad);
@@ -167,20 +185,21 @@ public class SCRAnnotationProcessor impl
component.setCreateMetatype(cad.getBooleanValue("metatype", false));
- if ( cad.getValue("enabled") != null ) {
+ if (cad.getValue("enabled") != null) {
component.setEnabled(cad.getBooleanValue("enabled", true));
}
- if ( cad.getValue("specVersion") != null ) {
+ if (cad.getValue("specVersion") != null) {
component.setSpecVersion(SpecVersion.fromName(cad.getValue("specVersion").toString()));
}
component.setFactory(cad.getStringValue("factory", null));
// FELIX-593: immediate attribute does not default to true all the
// times hence we only set it if declared in the tag
- if ( cad.getValue("immediate") != null) {
+ if (cad.getValue("immediate") != null) {
component.setEnabled(cad.getBooleanValue("immediate", false));
}
component.setInherit(cad.getBooleanValue("inherit", true));
- component.setConfigurationPolicy(ComponentConfigurationPolicy.valueOf(cad.getEnumValue("policy", ComponentConfigurationPolicy.OPTIONAL.name())));
+ component.setConfigurationPolicy(ComponentConfigurationPolicy.valueOf(cad.getEnumValue("policy",
+ ComponentConfigurationPolicy.OPTIONAL.name())));
component.setSetMetatypeFactoryPid(cad.getBooleanValue("configurationFactory", false));
return component;
@@ -188,27 +207,30 @@ public class SCRAnnotationProcessor impl
/**
* Create a service description
- * @param descs The service annotations.
- * @param scannedClass The scanned class
+ *
+ * @param descs
+ * The service annotations.
+ * @param scannedClass
+ * The scanned class
*/
private ServiceDescription createService(final List<ClassAnnotation> descs, final ScannedClass scannedClass) {
final ServiceDescription service = new ServiceDescription(descs.get(0));
final List<String> listedInterfaces = new ArrayList<String>();
- for(final ClassAnnotation d : descs) {
- if ( d.getBooleanValue("serviceFactory", false) ) {
+ for (final ClassAnnotation d : descs) {
+ if (d.getBooleanValue("serviceFactory", false)) {
service.setServiceFactory(true);
}
- if ( d.getValue("value") != null ) {
+ if (d.getValue("value") != null) {
final String[] interfaces = (String[]) d.getValue("value");
- for(String t : interfaces) {
+ for (String t : interfaces) {
listedInterfaces.add(t);
}
}
}
- if ( listedInterfaces.size() > 0 && !listedInterfaces.contains(AutoDetect.class.getName()) ) {
- for(final String i : listedInterfaces) {
+ if (listedInterfaces.size() > 0 && !listedInterfaces.contains(AutoDetect.class.getName())) {
+ for (final String i : listedInterfaces) {
service.addInterface(i);
}
} else {
@@ -222,26 +244,28 @@ public class SCRAnnotationProcessor impl
/**
* Recursively add interfaces to the service.
*/
- private void addInterfaces( final ServiceDescription service, final Class<?> javaClass) {
- if ( javaClass != null ) {
+ private void addInterfaces(final ServiceDescription service, final Class<?> javaClass) {
+ if (javaClass != null) {
final Class<?>[] interfaces = javaClass.getInterfaces();
- for(final Class<?> i : interfaces) {
+ for (final Class<?> i : interfaces) {
// recursivly add interfaces implemented by this interface
- this.addInterfaces( service, i);
+ this.addInterfaces(service, i);
}
// try super class
- this.addInterfaces( service, javaClass.getSuperclass() );
+ this.addInterfaces(service, javaClass.getSuperclass());
}
}
/**
* Create a method description if a name is provided.
- * @param methodName The name of the method or <code>null</code>
+ *
+ * @param methodName
+ * The name of the method or <code>null</code>
* @return A method description if the name is not null.
*/
private MethodDescription getMethodDescription(final String methodName) {
- if ( methodName != null ) {
+ if (methodName != null) {
return new MethodDescription(methodName);
}
return null;
@@ -249,31 +273,36 @@ public class SCRAnnotationProcessor impl
/**
* Create reference descriptions
- * @param descs List of reference annotations.s
- * @param describedClass The described class.
+ *
+ * @param descs
+ * List of reference annotations.s
+ * @param describedClass
+ * The described class.
*/
- private void createReferences(final List<? extends ScannedAnnotation> descs,
- final ClassDescription describedClass) {
- for(final ScannedAnnotation ad : descs) {
+ private void createReferences(final List<? extends ScannedAnnotation> descs, final ClassDescription describedClass) {
+ for (final ScannedAnnotation ad : descs) {
final ReferenceDescription ref = new ReferenceDescription(ad);
// check for field annotation
final FieldAnnotation fieldAnnotation;
- if ( ad instanceof FieldAnnotation ) {
- fieldAnnotation = (FieldAnnotation)ad;
+ if (ad instanceof FieldAnnotation) {
+ fieldAnnotation = (FieldAnnotation) ad;
ref.setField(fieldAnnotation.getAnnotatedField());
} else {
fieldAnnotation = null;
}
- ref.setName(ad.getStringValue("name", (fieldAnnotation != null ? fieldAnnotation.getAnnotatedField().getName() : null)));
+ ref.setName(ad.getStringValue("name",
+ (fieldAnnotation != null ? fieldAnnotation.getAnnotatedField().getName() : null)));
ref.setBind(getMethodDescription(ad.getStringValue("bind", "bind")));
ref.setUnbind(getMethodDescription(ad.getStringValue("unbind", "unbind")));
ref.setUpdated(getMethodDescription(ad.getStringValue("updated", null)));
ref.setTarget(ad.getStringValue("target", null));
- ref.setInterfaceName(ad.getStringValue("referenceInterface", (fieldAnnotation != null ? fieldAnnotation.getAnnotatedField().getType().getName() : null)));
+ ref.setInterfaceName(ad.getStringValue("referenceInterface", (fieldAnnotation != null ? fieldAnnotation
+ .getAnnotatedField().getType().getName() : null)));
- ref.setCardinality(ReferenceCardinality.valueOf(ad.getEnumValue("cardinality", ReferenceCardinality.MANDATORY_UNARY.name())));
+ ref.setCardinality(ReferenceCardinality.valueOf(ad.getEnumValue("cardinality",
+ ReferenceCardinality.MANDATORY_UNARY.name())));
ref.setStrategy(ReferenceStrategy.valueOf(ad.getEnumValue("strategy", ReferenceStrategy.EVENT.name())));
ref.setPolicy(ReferencePolicy.valueOf(ad.getEnumValue("policy", ReferencePolicy.STATIC.name())));
@@ -281,32 +310,25 @@ public class SCRAnnotationProcessor impl
}
}
- private static final String[] PROPERTY_VALUE_PROCESSING = new String[] {"String", "value",
- "String", "classValue",
- "Long", "longValue",
- "Double", "doubleValue",
- "Float", "floatValue",
- "Integer", "intValue",
- "Byte", "byteValue",
- "Char", "charValue",
- "Boolean", "boolValue",
- "Short", "shortValue"
- };
+ private static final String[] PROPERTY_VALUE_PROCESSING = new String[] { "String", "value", "String", "classValue", "Long",
+ "longValue", "Double", "doubleValue", "Float", "floatValue", "Integer", "intValue", "Byte", "byteValue", "Char",
+ "charValue", "Boolean", "boolValue", "Short", "shortValue" };
/**
* Create properties descriptions
+ *
* @throws SCRDescriptorException
* @throws SCRDescriptorFailureException
*/
- private void createProperties(final List<? extends ScannedAnnotation> descs,
- final ClassDescription describedClass) throws SCRDescriptorFailureException, SCRDescriptorException {
- for(final ScannedAnnotation ad : descs) {
+ private void createProperties(final List<? extends ScannedAnnotation> descs, final ClassDescription describedClass)
+ throws SCRDescriptorFailureException, SCRDescriptorException {
+ for (final ScannedAnnotation ad : descs) {
final PropertyDescription prop = new PropertyDescription(ad);
// check for field annotation
final FieldAnnotation fieldAnnotation;
- if ( ad instanceof FieldAnnotation ) {
- fieldAnnotation = (FieldAnnotation)ad;
+ if (ad instanceof FieldAnnotation) {
+ fieldAnnotation = (FieldAnnotation) ad;
prop.setField(fieldAnnotation.getAnnotatedField());
} else {
fieldAnnotation = null;
@@ -316,35 +338,35 @@ public class SCRAnnotationProcessor impl
String type = null;
String[] values = null;
int index = 0;
- while ( type == null && index < PROPERTY_VALUE_PROCESSING.length ) {
+ while (type == null && index < PROPERTY_VALUE_PROCESSING.length) {
final String propType = PROPERTY_VALUE_PROCESSING[index];
- final String propName = PROPERTY_VALUE_PROCESSING[index+1];
+ final String propName = PROPERTY_VALUE_PROCESSING[index + 1];
final Object propValue = ad.getValue(propName);
- if ( propValue != null && propValue.getClass().isArray() ) {
+ if (propValue != null && propValue.getClass().isArray()) {
type = propType;
values = new String[Array.getLength(propValue)];
- for(int i=0; i<values.length; i++) {
+ for (int i = 0; i < values.length; i++) {
values[i] = Array.get(propValue, i).toString();
}
}
index += 2;
}
- if ( values != null ) {
+ if (values != null) {
prop.setType(PropertyType.valueOf(type));
if (values.length == 1) {
prop.setValue(values[0]);
} else {
prop.setMultiValue(values);
}
- } else if ( fieldAnnotation != null ) {
+ } else if (fieldAnnotation != null) {
// Detect values from field
final Object value = fieldAnnotation.getAnnotatedFieldValue();
- if ( value != null ) {
- if ( value.getClass().isArray() ) {
+ if (value != null) {
+ if (value.getClass().isArray()) {
values = new String[Array.getLength(value)];
- for(int i=0; i<values.length; i++) {
+ for (int i = 0; i < values.length; i++) {
values[i] = Array.get(value, i).toString();
}
prop.setMultiValue(values);
@@ -357,12 +379,12 @@ public class SCRAnnotationProcessor impl
}
final String defaultName;
- if ( fieldAnnotation != null ) {
- if ( values == null ) {
+ if (fieldAnnotation != null) {
+ if (values == null) {
defaultName = fieldAnnotation.getAnnotatedField().getName();
} else {
final Object value = fieldAnnotation.getAnnotatedFieldValue();
- if ( value != null ) {
+ if (value != null) {
defaultName = value.toString();
} else {
defaultName = null;
@@ -373,17 +395,19 @@ public class SCRAnnotationProcessor impl
}
prop.setName(ad.getStringValue("name", defaultName));
prop.setLabel(ad.getStringValue("label", prop.getName() != null ? "%" + prop.getName() + ".name" : null));
- prop.setDescription(ad.getStringValue("description", prop.getName() != null ? "%" + prop.getName() + ".description" : null));
+ prop.setDescription(ad.getStringValue("description", prop.getName() != null ? "%" + prop.getName() + ".description"
+ : null));
prop.setPrivate(ad.getBooleanValue("propertyPrivate", false));
// cardinality handling
- final PropertyUnbounded pu = PropertyUnbounded.valueOf(ad.getEnumValue("unbounded", PropertyUnbounded.DEFAULT.name()));
+ final PropertyUnbounded pu = PropertyUnbounded
+ .valueOf(ad.getEnumValue("unbounded", PropertyUnbounded.DEFAULT.name()));
prop.setUnbounded(pu);
- if ( pu == PropertyUnbounded.DEFAULT) {
+ if (pu == PropertyUnbounded.DEFAULT) {
prop.setCardinality(ad.getIntegerValue("cardinality", 0));
- if ( prop.getMultiValue() != null && prop.getCardinality() == 0 ) {
+ if (prop.getMultiValue() != null && prop.getCardinality() == 0) {
prop.setUnbounded(PropertyUnbounded.ARRAY);
}
} else {