Skip to content

Commit

Permalink
[#43] provide more meaningful message
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown authored and unknown committed Nov 28, 2014
1 parent d2bcf9b commit aba8e14
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 76 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Invalid reference name 'null' (derived from bind method name 'set').
Invalid bind method name 'set'. You should choose a more specific name instead (e.g. 'setMyService').
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
public abstract class AbstractComponentDescription implements IComponentDescription {

/** - */
protected static final String FIELD_NAME_SERVICE = "service";
protected static final String FIELD_NAME_SERVICE = "service";

protected static final String FIELD_NAME_SERVICEFACTORY = "servicefactory";

/** - */
protected static final String FIELD_NAME_TARGET = "target";
protected static final String FIELD_NAME_TARGET = "target";

/** - */
private AbstractTypeAccessor _typeAccessor;
Expand Down Expand Up @@ -80,8 +80,8 @@ public void execute() {
// check service factory / services
if (_typeAccessor.getService() == null && getTypeAccessor().getAllDirectlyImplementedSuperInterfaces().isEmpty()
&& _typeAccessor.getServiceFactory() != null) {
throw new DsAnnotationException(String.format(Messages.ComponentDescription_INVALID_SERVICEFACTORY_DECLARATION,
FIELD_NAME_SERVICEFACTORY));
throw new DsAnnotationException(String.format(Messages.ComponentDescription_INVALID_SERVICEFACTORY_DECLARATION,
FIELD_NAME_SERVICEFACTORY));
}

// name
Expand Down Expand Up @@ -150,7 +150,12 @@ public void execute() {

// references
for (Reference ref : _typeAccessor.getReferences()) {
onAddReference(ref.getService(), ref.getBind(), ref.getName(), ref.getCardinality(), ref.getPolicy(),

// check bind method name
checkReferenceBindMethod(ref.getBind());

//
onAddReference(ref.getService(), ref.getBind(), computeReferenceName(ref.getName(), ref.getBind()), ref.getCardinality(), ref.getPolicy(),
ref.getPolicyOption(), ref.getUnbind(), ref.getUpdated(), ref.getTarget());
}
}
Expand Down Expand Up @@ -227,6 +232,67 @@ protected boolean isNotEmpty(String name) {
return name != null && name.trim().length() > 0;
}

/**
* <p>
* </p>
*
* @param name
* @param bindMethodName
* @return
*/
protected String computeReferenceName(String name, String bindMethodName) {

//
if (name != null) {
if (name.length() == 0) {
throw new DsAnnotationException(String.format(Messages.ComponentDescription_INVALID_REFERENCE_NAME, name));
} else {
return name;
}
}

checkReferenceBindMethod(bindMethodName);

if (bindMethodName.startsWith("add")) { //$NON-NLS-1$
return bindMethodName.substring("add".length()); //$NON-NLS-1$
} else if (bindMethodName.startsWith("set")) { //$NON-NLS-1$
return bindMethodName.substring("set".length()); //$NON-NLS-1$
} else if (bindMethodName.startsWith("bind")) { //$NON-NLS-1$
return bindMethodName.substring("bind".length()); //$NON-NLS-1$
}

//
return bindMethodName;
}

/**
* <p>
* </p>
*
* @param bindMethodName
* @return
*/
private String checkReferenceBindMethod(String bindMethodName) {

//
if (bindMethodName.equals("add")) {
throw new DsAnnotationException(String.format(Messages.ComponentDescription_INVALID_BIND_METHOD_NAME, "add",
"addMyService"));
}

if (bindMethodName.equals("set")) {
throw new DsAnnotationException(String.format(Messages.ComponentDescription_INVALID_BIND_METHOD_NAME, "set",
"setMyService"));
}

if (bindMethodName.equals("bind")) {
throw new DsAnnotationException(String.format(Messages.ComponentDescription_INVALID_BIND_METHOD_NAME, "bind",
"bindMyService"));
}

return bindMethodName;
}

/**
* <p>
* </p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public class Messages extends NLS {

private static final String BUNDLE_NAME = "com.wuetherich.osgi.ds.annotations.internal.componentdescription.impl.messages"; //$NON-NLS-1$

public static String ComponentDescription_INVALID_BIND_METHOD_NAME;

public static String ComponentDescription_INVALID_SERVICEFACTORY_DECLARATION;

public static String ComponentDescription_INVALID_DERIVED_REFERENCE_NAME;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,29 +303,7 @@ public void onAddReference(String service, String bind, String name, String card

// step 2: set the bind method name
reference.setBind(bind);

// step 3: set the name of the bind method
if (isNotEmpty(name)) {
if (name == null || name.isEmpty()) {
throw new DsAnnotationException(String.format(Messages.ComponentDescription_INVALID_REFERENCE_NAME,
reference.getName()));
}
reference.setName(name);
} else {
name = bind;
if (name.startsWith("add")) { //$NON-NLS-1$
name = name.substring("add".length()); //$NON-NLS-1$
} else if (name.startsWith("set")) { //$NON-NLS-1$
name = name.substring("set".length()); //$NON-NLS-1$
} else if (name.startsWith("bind")) { //$NON-NLS-1$
name = name.substring("bind".length()); //$NON-NLS-1$
}
if (name == null || name.isEmpty()) {
throw new DsAnnotationException(String.format(Messages.ComponentDescription_INVALID_DERIVED_REFERENCE_NAME,
reference.getName(), bind));
}
reference.setName(name);
}
reference.setName(name);

// [https://github.com/wuetherich/ds-annotation-builder/issues/21]
// check if reference name is unique
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,29 +315,7 @@ public void onAddReference(String service, String bind, String name, String card

// step 2: set the bind method name
reference.setBind(bind);

// step 3: set the name of the bind method
if (isNotEmpty(name)) {
if (name == null || name.isEmpty()) {
throw new DsAnnotationException(String.format(Messages.ComponentDescription_INVALID_REFERENCE_NAME,
reference.getName()));
}
reference.setName(name);
} else {
name = bind;
if (name.startsWith("add")) { //$NON-NLS-1$
name = name.substring("add".length()); //$NON-NLS-1$
} else if (name.startsWith("set")) { //$NON-NLS-1$
name = name.substring("set".length()); //$NON-NLS-1$
} else if (name.startsWith("bind")) { //$NON-NLS-1$
name = name.substring("bind".length()); //$NON-NLS-1$
}
if (name == null || name.isEmpty()) {
throw new DsAnnotationException(String.format(Messages.ComponentDescription_INVALID_DERIVED_REFERENCE_NAME,
reference.getName(), bind));
}
reference.setName(name);
}
reference.setName(name);

// [https://github.com/wuetherich/ds-annotation-builder/issues/21]
// check if reference name is unique
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,29 +315,7 @@ public void onAddReference(String service, String bind, String name, String card

// step 2: set the bind method name
reference.setBind(bind);

// step 3: set the name of the bind method
if (isNotEmpty(name)) {
if (name == null || name.isEmpty()) {
throw new DsAnnotationException(String.format(Messages.ComponentDescription_INVALID_REFERENCE_NAME,
reference.getName()));
}
reference.setName(name);
} else {
name = bind;
if (name.startsWith("add")) { //$NON-NLS-1$
name = name.substring("add".length()); //$NON-NLS-1$
} else if (name.startsWith("set")) { //$NON-NLS-1$
name = name.substring("set".length()); //$NON-NLS-1$
} else if (name.startsWith("bind")) { //$NON-NLS-1$
name = name.substring("bind".length()); //$NON-NLS-1$
}
if (name == null || name.isEmpty()) {
throw new DsAnnotationException(String.format(Messages.ComponentDescription_INVALID_DERIVED_REFERENCE_NAME,
reference.getName(), bind));
}
reference.setName(name);
}
reference.setName(name);

// [https://github.com/wuetherich/ds-annotation-builder/issues/21]
// check if reference name is unique
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ ComponentDescription_INVALID_SERVICE_TYPE=Invalid service type. The specified co
ComponentDescription_INVALID_FILTER=Invalid filter '%s'.
ComponentDescription_NON_EXISTING_UNBIND_METHOD=Non existing unbind method '%s'.
ComponentDescription_NON_EXISTING_UPDATED_METHOD=Non existing updated method '%s'.
ComponentDescription_INVALID_BIND_METHOD_NAME=Invalid bind method name '%s'. You should choose a more specific name instead (e.g. '%s').

0 comments on commit aba8e14

Please sign in to comment.