FELIX-2876 : <reference> element not generated when name is missing
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1079719 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/SCRDescriptorGenerator.java b/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/SCRDescriptorGenerator.java
index f3ed7a3..639b967 100644
--- a/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/SCRDescriptorGenerator.java
+++ b/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/SCRDescriptorGenerator.java
@@ -525,7 +525,7 @@
final String refName = entry.getKey();
final Object[] values = entry.getValue();
final JavaTag tag = ( JavaTag ) values[0];
- this.doReference( tag, refName, component, values[1].toString() );
+ this.doReference( tag, refName, component, values[1].toString(), ((Boolean)values[2]).booleanValue() );
}
// pid handling
@@ -763,7 +763,13 @@
protected void testReference( Map<String, Object[]> references, JavaTag reference, String defaultName,
boolean isInspectedClass ) throws SCRDescriptorException
{
- final String refName = this.getReferenceName( reference, defaultName );
+ String refName = this.getReferenceName( reference, defaultName );
+
+ boolean setName = refName != null;
+ if ( refName == null)
+ {
+ refName = this.getReferencedInterface(reference, isInspectedClass, null);
+ }
if ( refName != null )
{
@@ -780,41 +786,54 @@
else
{
// ensure interface
- String type = reference.getNamedParameter( Constants.REFERENCE_INTERFACE );
- if ( StringUtils.isEmpty( type ) )
- {
- if ( reference.getField() != null )
- {
- type = reference.getField().getType();
- }
- else
- {
- throw new SCRDescriptorException( "Interface missing for reference " + refName + " in class "
- + reference.getJavaClassDescription().getName(), reference );
- }
- }
- else if ( isInspectedClass )
- {
- // check if the value points to a class/interface
- // and search through the imports
- final JavaClassDescription serviceClass = reference.getJavaClassDescription().getReferencedClass(
- type );
- if ( serviceClass == null )
- {
- throw new SCRDescriptorException( "Interface '" + type + "' in class "
- + reference.getJavaClassDescription().getName()
- + " does not point to a valid class/interface.", reference );
- }
- type = serviceClass.getName();
- }
+ final String type = this.getReferencedInterface(reference, isInspectedClass, refName);
references.put( refName, new Object[]
- { reference, type } );
+ { reference, type, (setName ? Boolean.TRUE : Boolean.FALSE) } );
}
}
+ else
+ {
+ throw new SCRDescriptorException( "No name detectable for reference in class "
+ + reference.getJavaClassDescription().getName(), reference );
+ }
}
+ protected String getReferencedInterface(final JavaTag reference,
+ final boolean isInspectedClass,
+ final String refName) throws SCRDescriptorException
+ {
+ String type = reference.getNamedParameter( Constants.REFERENCE_INTERFACE );
+ if ( StringUtils.isEmpty( type ) )
+ {
+ if ( reference.getField() != null )
+ {
+ type = reference.getField().getType();
+ }
+ else
+ {
+ throw new SCRDescriptorException( "Interface missing for reference " + (refName == null ? "" : refName) + " in class "
+ + reference.getJavaClassDescription().getName(), reference );
+ }
+ }
+ else if ( isInspectedClass )
+ {
+ // check if the value points to a class/interface
+ // and search through the imports
+ final JavaClassDescription serviceClass = reference.getJavaClassDescription().getReferencedClass(
+ type );
+ if ( serviceClass == null )
+ {
+ throw new SCRDescriptorException( "Interface '" + type + "' in class "
+ + reference.getJavaClassDescription().getName()
+ + " does not point to a valid class/interface.", reference );
+ }
+ type = serviceClass.getName();
+ }
+ return type;
+ }
- protected String getReferenceName( JavaTag reference, String defaultName ) throws SCRDescriptorException
+ protected String getReferenceName( final JavaTag reference,
+ final String defaultName) throws SCRDescriptorException
{
String name = reference.getNamedParameter( Constants.REFERENCE_NAME );
if ( !StringUtils.isEmpty( name ) )
@@ -836,7 +855,7 @@
throw new SCRDescriptorException( "Referenced field " + name
+ " has more than one value for a reference name.", reference );
}
- name = values[0];
+ return values[0];
}
return defaultName;
@@ -866,15 +885,20 @@
/**
+ * Process a reference
* @param reference
* @param defaultName
* @param component
*/
- protected void doReference( JavaTag reference, String name, Component component, String type )
+ protected void doReference( JavaTag reference, String name, Component component, String type,
+ final boolean setName)
throws SCRDescriptorException
{
final Reference ref = new Reference( reference, component.getJavaClassDescription() );
- ref.setName( name );
+ if ( setName )
+ {
+ ref.setName( name );
+ }
ref.setInterfacename( type );
ref.setCardinality( reference.getNamedParameter( Constants.REFERENCE_CARDINALITY ) );
if ( ref.getCardinality() == null )
diff --git a/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/om/Reference.java b/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/om/Reference.java
index a4ef68d..72588e9 100644
--- a/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/om/Reference.java
+++ b/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/om/Reference.java
@@ -22,9 +22,7 @@
import org.apache.felix.scrplugin.SCRDescriptorException;
import org.apache.felix.scrplugin.helper.IssueLog;
import org.apache.felix.scrplugin.helper.StringUtils;
-import org.apache.felix.scrplugin.tags.JavaClassDescription;
-import org.apache.felix.scrplugin.tags.JavaMethod;
-import org.apache.felix.scrplugin.tags.JavaTag;
+import org.apache.felix.scrplugin.tags.*;
/**
* <code>Reference.java</code>...
@@ -169,7 +167,9 @@
// validate name
if (StringUtils.isEmpty(this.name)) {
- this.logError( iLog, "Reference has no name" );
+ if ( specVersion < Constants.VERSION_1_1 ) {
+ this.logError( iLog, "Reference has no name" );
+ }
}
// validate interface