Skip to content

Commit

Permalink
[JBERET-248] NullPointerException when using artifacts with default-p…
Browse files Browse the repository at this point in the history
…ackage (#79)

* Fixed nullpointer exception when using artifacts with default-package.

* revert parts other than the part of the cause of NPE
  • Loading branch information
Kohei Nozaki authored and chengfang committed Mar 2, 2017
1 parent ad40e1c commit 83ca23c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
Expand Up @@ -54,7 +54,7 @@ protected void doInjection(final Object obj, Class<?> cls,
final StepContextImpl stepContext,
final Properties batchProps) throws Exception {
final boolean hasBatchProps = batchProps != null && batchProps.size() > 0;
while (cls != null && cls != Object.class && !cls.getPackage().getName().startsWith("javax.batch")) {
while (cls != null && cls != Object.class && cls.getPackage() != null && !cls.getPackage().getName().startsWith("javax.batch")) {
for (final Field f : cls.getDeclaredFields()) {
if (!f.isSynthetic()) {
Object fieldVal = null;
Expand Down Expand Up @@ -96,7 +96,7 @@ protected void doInjection(final Object obj, Class<?> cls,

protected void invokeAnnotatedLifecycleMethod(final Object obj, Class<?> cls, final Class<? extends Annotation> annCls) throws Exception{
final List<Method> lifecycleMethods = new ArrayList<Method>();
while (cls != null && cls != Object.class && !cls.getPackage().getName().startsWith("javax.batch")) {
while (cls != null && cls != Object.class && cls.getPackage() != null && !cls.getPackage().getName().startsWith("javax.batch")) {
final Method[] methods = cls.getDeclaredMethods();
for (final Method m : methods) {
if (m.getAnnotation(annCls) != null) { //the lifecyle annotation is present
Expand Down
8 changes: 8 additions & 0 deletions jberet-core/src/test/java/NoPackageBatchlet.java
@@ -0,0 +1,8 @@
import javax.batch.api.AbstractBatchlet;

public class NoPackageBatchlet extends AbstractBatchlet {
@Override
public String process() throws Exception {
return null;
}
}
@@ -0,0 +1,38 @@
package org.jberet.creation;

import org.junit.Before;
import org.junit.Test;

public class AbstractArtifactFactoryTest {

private AbstractArtifactFactory factory = new AbstractArtifactFactory() {
@Override
public Object create(String ref, Class<?> cls, ClassLoader classLoader) throws Exception {
return null;
}

@Override
public Class<?> getArtifactClass(String ref, ClassLoader classLoader) {
return null;
}
};

private Class noPackageBatchletClass;
private Object noPackageBatchlet;

@Before
public void setup() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
noPackageBatchletClass = Class.forName("NoPackageBatchlet");
noPackageBatchlet = noPackageBatchletClass.newInstance();
}

@Test
public void testDestroyShouldNotFailForDefaultPackage() {
factory.destroy(noPackageBatchlet);
}

@Test
public void testDoInjectionShouldNotFailForDefaultPackage() throws Exception {
factory.doInjection(noPackageBatchlet, noPackageBatchletClass, null, null, null, null);
}
}

0 comments on commit 83ca23c

Please sign in to comment.