Skip to content

Commit 9e20607

Browse files
committed
General defensiveness about the bootstrap ClassLoader (i.e. null ClassLoader)
Issue: SPR-11721
1 parent 2c1203d commit 9e20607

File tree

30 files changed

+229
-179
lines changed

30 files changed

+229
-179
lines changed

spring-beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -289,15 +289,15 @@ private Object attemptToConvertStringToEnum(Class<?> requiredType, String trimme
289289
if (index > - 1) {
290290
String enumType = trimmedValue.substring(0, index);
291291
String fieldName = trimmedValue.substring(index + 1);
292-
ClassLoader loader = this.targetObject.getClass().getClassLoader();
292+
ClassLoader cl = this.targetObject.getClass().getClassLoader();
293293
try {
294-
Class<?> enumValueType = loader.loadClass(enumType);
294+
Class<?> enumValueType = ClassUtils.forName(enumType, cl);
295295
Field enumField = enumValueType.getField(fieldName);
296296
convertedValue = enumField.get(null);
297297
}
298298
catch (ClassNotFoundException ex) {
299299
if(logger.isTraceEnabled()) {
300-
logger.trace("Enum class [" + enumType + "] cannot be loaded from [" + loader + "]", ex);
300+
logger.trace("Enum class [" + enumType + "] cannot be loaded", ex);
301301
}
302302
}
303303
catch (Throwable ex) {

spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -135,9 +135,9 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean
135135
public AutowiredAnnotationBeanPostProcessor() {
136136
this.autowiredAnnotationTypes.add(Autowired.class);
137137
this.autowiredAnnotationTypes.add(Value.class);
138-
ClassLoader cl = AutowiredAnnotationBeanPostProcessor.class.getClassLoader();
139138
try {
140-
this.autowiredAnnotationTypes.add((Class<? extends Annotation>) cl.loadClass("javax.inject.Inject"));
139+
this.autowiredAnnotationTypes.add((Class<? extends Annotation>)
140+
ClassUtils.forName("javax.inject.Inject", AutowiredAnnotationBeanPostProcessor.class.getClassLoader()));
141141
logger.info("JSR-330 'javax.inject.Inject' annotation found and supported for autowiring");
142142
}
143143
catch (ClassNotFoundException ex) {

spring-beans/src/main/java/org/springframework/beans/factory/annotation/QualifierAnnotationAutowireCandidateResolver.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -71,9 +71,9 @@ public class QualifierAnnotationAutowireCandidateResolver implements AutowireCan
7171
@SuppressWarnings("unchecked")
7272
public QualifierAnnotationAutowireCandidateResolver() {
7373
this.qualifierTypes.add(Qualifier.class);
74-
ClassLoader cl = QualifierAnnotationAutowireCandidateResolver.class.getClassLoader();
7574
try {
76-
this.qualifierTypes.add((Class<? extends Annotation>) cl.loadClass("javax.inject.Qualifier"));
75+
this.qualifierTypes.add((Class<? extends Annotation>)
76+
ClassUtils.forName("javax.inject.Qualifier", QualifierAnnotationAutowireCandidateResolver.class.getClassLoader()));
7777
}
7878
catch (ClassNotFoundException ex) {
7979
// JSR-330 API not available - simply skip.

spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
103103
private static Class<?> javaxInjectProviderClass = null;
104104

105105
static {
106-
ClassLoader cl = DefaultListableBeanFactory.class.getClassLoader();
107106
try {
108-
javaxInjectProviderClass = cl.loadClass("javax.inject.Provider");
107+
javaxInjectProviderClass =
108+
ClassUtils.forName("javax.inject.Provider", DefaultListableBeanFactory.class.getClassLoader());
109109
}
110110
catch (ClassNotFoundException ex) {
111111
// JSR-330 API not available - Provider interface simply not supported then.

spring-beans/src/main/java/org/springframework/beans/factory/support/DisposableBeanAdapter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -69,7 +69,8 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable {
6969

7070
static {
7171
try {
72-
closeableInterface = DisposableBeanAdapter.class.getClassLoader().loadClass("java.lang.AutoCloseable");
72+
closeableInterface = ClassUtils.forName("java.lang.AutoCloseable",
73+
DisposableBeanAdapter.class.getClassLoader());
7374
}
7475
catch (ClassNotFoundException ex) {
7576
closeableInterface = Closeable.class;

spring-context-support/src/main/java/org/springframework/scheduling/quartz/CronTriggerFactoryBean.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,6 +34,7 @@
3434
import org.springframework.beans.factory.InitializingBean;
3535
import org.springframework.core.Constants;
3636
import org.springframework.util.Assert;
37+
import org.springframework.util.ClassUtils;
3738
import org.springframework.util.ReflectionUtils;
3839

3940
/**
@@ -259,7 +260,7 @@ public void afterPropertiesSet() {
259260
Class<?> cronTriggerClass;
260261
Method jobKeyMethod;
261262
try {
262-
cronTriggerClass = getClass().getClassLoader().loadClass("org.quartz.impl.triggers.CronTriggerImpl");
263+
cronTriggerClass = ClassUtils.forName("org.quartz.impl.triggers.CronTriggerImpl", getClass().getClassLoader());
263264
jobKeyMethod = JobDetail.class.getMethod("getKey");
264265
}
265266
catch (ClassNotFoundException ex) {

spring-context-support/src/main/java/org/springframework/scheduling/quartz/JobDetailFactoryBean.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@
3030
import org.springframework.beans.factory.InitializingBean;
3131
import org.springframework.context.ApplicationContext;
3232
import org.springframework.context.ApplicationContextAware;
33+
import org.springframework.util.ClassUtils;
3334

3435
/**
3536
* A Spring {@link FactoryBean} for creating a Quartz {@link org.quartz.JobDetail}
@@ -56,7 +57,7 @@ public class JobDetailFactoryBean
5657

5758
private String group;
5859

59-
private Class jobClass;
60+
private Class<?> jobClass;
6061

6162
private JobDataMap jobDataMap = new JobDataMap();
6263

@@ -92,7 +93,7 @@ public void setGroup(String group) {
9293
/**
9394
* Specify the job's implementation class.
9495
*/
95-
public void setJobClass(Class jobClass) {
96+
public void setJobClass(Class<?> jobClass) {
9697
this.jobClass = jobClass;
9798
}
9899

@@ -207,7 +208,7 @@ public void afterPropertiesSet() {
207208

208209
Class<?> jobDetailClass;
209210
try {
210-
jobDetailClass = getClass().getClassLoader().loadClass("org.quartz.impl.JobDetailImpl");
211+
jobDetailClass = ClassUtils.forName("org.quartz.impl.JobDetailImpl", getClass().getClassLoader());
211212
}
212213
catch (ClassNotFoundException ex) {
213214
jobDetailClass = JobDetail.class;

spring-context-support/src/main/java/org/springframework/scheduling/quartz/MethodInvokingJobDetailFactoryBean.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -86,14 +86,15 @@ public class MethodInvokingJobDetailFactoryBean extends ArgumentConvertingMethod
8686

8787
static {
8888
try {
89-
jobDetailImplClass = Class.forName("org.quartz.impl.JobDetailImpl");
89+
jobDetailImplClass = ClassUtils.forName("org.quartz.impl.JobDetailImpl",
90+
MethodInvokingJobDetailFactoryBean.class.getClassLoader());
9091
}
9192
catch (ClassNotFoundException ex) {
9293
jobDetailImplClass = null;
9394
}
9495
try {
95-
Class<?> jobExecutionContextClass =
96-
QuartzJobBean.class.getClassLoader().loadClass("org.quartz.JobExecutionContext");
96+
Class<?> jobExecutionContextClass = ClassUtils.forName("org.quartz.JobExecutionContext",
97+
MethodInvokingJobDetailFactoryBean.class.getClassLoader());
9798
setResultMethod = jobExecutionContextClass.getMethod("setResult", Object.class);
9899
}
99100
catch (Exception ex) {

spring-context-support/src/main/java/org/springframework/scheduling/quartz/QuartzJobBean.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
2828
import org.springframework.beans.BeanWrapper;
2929
import org.springframework.beans.MutablePropertyValues;
3030
import org.springframework.beans.PropertyAccessorFactory;
31+
import org.springframework.util.ClassUtils;
3132
import org.springframework.util.ReflectionUtils;
3233

3334
/**
@@ -79,8 +80,8 @@ public abstract class QuartzJobBean implements Job {
7980

8081
static {
8182
try {
82-
Class jobExecutionContextClass =
83-
QuartzJobBean.class.getClassLoader().loadClass("org.quartz.JobExecutionContext");
83+
Class<?> jobExecutionContextClass =
84+
ClassUtils.forName("org.quartz.JobExecutionContext", QuartzJobBean.class.getClassLoader());
8485
getSchedulerMethod = jobExecutionContextClass.getMethod("getScheduler");
8586
getMergedJobDataMapMethod = jobExecutionContextClass.getMethod("getMergedJobDataMap");
8687
}
@@ -99,7 +100,7 @@ public final void execute(JobExecutionContext context) throws JobExecutionExcept
99100
try {
100101
// Reflectively adapting to differences between Quartz 1.x and Quartz 2.0...
101102
Scheduler scheduler = (Scheduler) ReflectionUtils.invokeMethod(getSchedulerMethod, context);
102-
Map mergedJobDataMap = (Map) ReflectionUtils.invokeMethod(getMergedJobDataMapMethod, context);
103+
Map<?, ?> mergedJobDataMap = (Map<?, ?>) ReflectionUtils.invokeMethod(getMergedJobDataMapMethod, context);
103104

104105
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
105106
MutablePropertyValues pvs = new MutablePropertyValues();

spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerAccessor.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.springframework.transaction.TransactionException;
4343
import org.springframework.transaction.TransactionStatus;
4444
import org.springframework.transaction.support.DefaultTransactionDefinition;
45+
import org.springframework.util.ClassUtils;
4546
import org.springframework.util.ReflectionUtils;
4647

4748
/**
@@ -66,8 +67,8 @@ public abstract class SchedulerAccessor implements ResourceLoaderAware {
6667
static {
6768
// Quartz 2.0 job/trigger key available?
6869
try {
69-
jobKeyClass = Class.forName("org.quartz.JobKey");
70-
triggerKeyClass = Class.forName("org.quartz.TriggerKey");
70+
jobKeyClass = ClassUtils.forName("org.quartz.JobKey", SchedulerAccessor.class.getClassLoader());
71+
triggerKeyClass = ClassUtils.forName("org.quartz.TriggerKey", SchedulerAccessor.class.getClassLoader());
7172
}
7273
catch (ClassNotFoundException ex) {
7374
jobKeyClass = null;
@@ -254,7 +255,7 @@ protected void registerJobsAndTriggers() throws SchedulerException {
254255
clh.initialize();
255256
try {
256257
// Quartz 1.8 or higher?
257-
Class<?> dataProcessorClass = getClass().getClassLoader().loadClass("org.quartz.xml.XMLSchedulingDataProcessor");
258+
Class<?> dataProcessorClass = ClassUtils.forName("org.quartz.xml.XMLSchedulingDataProcessor", getClass().getClassLoader());
258259
logger.debug("Using Quartz 1.8 XMLSchedulingDataProcessor");
259260
Object dataProcessor = dataProcessorClass.getConstructor(ClassLoadHelper.class).newInstance(clh);
260261
Method processFileAndScheduleJobs = dataProcessorClass.getMethod("processFileAndScheduleJobs", String.class, Scheduler.class);
@@ -264,7 +265,7 @@ protected void registerJobsAndTriggers() throws SchedulerException {
264265
}
265266
catch (ClassNotFoundException ex) {
266267
// Quartz 1.6
267-
Class<?> dataProcessorClass = getClass().getClassLoader().loadClass("org.quartz.xml.JobSchedulingDataProcessor");
268+
Class<?> dataProcessorClass = ClassUtils.forName("org.quartz.xml.JobSchedulingDataProcessor", getClass().getClassLoader());
268269
logger.debug("Using Quartz 1.6 JobSchedulingDataProcessor");
269270
Object dataProcessor = dataProcessorClass.getConstructor(ClassLoadHelper.class, boolean.class, boolean.class).newInstance(clh, true, true);
270271
Method processFileAndScheduleJobs = dataProcessorClass.getMethod("processFileAndScheduleJobs", String.class, Scheduler.class, boolean.class);

0 commit comments

Comments
 (0)