|
| 1 | +/* |
| 2 | + * Copyright 2002-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.nativex.substitutions.framework.test; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Collection; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import com.oracle.svm.core.annotate.Alias; |
| 24 | +import com.oracle.svm.core.annotate.Substitute; |
| 25 | +import com.oracle.svm.core.annotate.TargetClass; |
| 26 | + |
| 27 | +import org.apache.commons.logging.Log; |
| 28 | + |
| 29 | +import org.springframework.beans.BeanInstantiationException; |
| 30 | +import org.springframework.beans.BeanUtils; |
| 31 | +import org.springframework.nativex.substitutions.OnlyIfPresent; |
| 32 | +import org.springframework.test.context.TestExecutionListener; |
| 33 | + |
| 34 | +/** |
| 35 | + * Native substitution for {@link org.springframework.test.context.support.AbstractTestContextBootstrapper}. |
| 36 | + * |
| 37 | + * <p>Necessary since class instantiation via reflection can result in a |
| 38 | + * {@link NoSuchMethodException} instead of a {@link NoClassDefFoundError} |
| 39 | + * within a native image, IF a {@link NoClassDefFoundError} was thrown during |
| 40 | + * native image build time while attempting to register the class for reflection. |
| 41 | + * In that case, the reflection registration still occurs, and we unfortunately |
| 42 | + * see different behavior at runtime within the native image. |
| 43 | + * |
| 44 | + * <p>In summary, the only change this substitution makes is the following |
| 45 | + * which additionally checks for a {@code NoSuchMethodException}: |
| 46 | + * {@code if (cause instanceof NoClassDefFoundError || cause instanceof NoSuchMethodException)}. |
| 47 | + * |
| 48 | + * @author Sam Brannen |
| 49 | + */ |
| 50 | +@TargetClass(className = "org.springframework.test.context.support.AbstractTestContextBootstrapper", onlyWith = OnlyIfPresent.class) |
| 51 | +final class Target_AbstractTestContextBootstrapper { |
| 52 | + |
| 53 | + @Alias |
| 54 | + private Log logger; |
| 55 | + |
| 56 | + |
| 57 | + @Substitute |
| 58 | + private List<TestExecutionListener> instantiateListeners(Collection<Class<? extends TestExecutionListener>> classes) { |
| 59 | + List<TestExecutionListener> listeners = new ArrayList<>(classes.size()); |
| 60 | + for (Class<? extends TestExecutionListener> listenerClass : classes) { |
| 61 | + try { |
| 62 | + listeners.add(BeanUtils.instantiateClass(listenerClass)); |
| 63 | + } |
| 64 | + catch (BeanInstantiationException ex) { |
| 65 | + Throwable cause = ex.getCause(); |
| 66 | + // Within a native image, NoSuchMethodException may be thrown instead of NoClassDefFoundError. |
| 67 | + if (cause instanceof NoClassDefFoundError || cause instanceof NoSuchMethodException) { |
| 68 | + // TestExecutionListener not applicable due to a missing dependency |
| 69 | + if (logger.isDebugEnabled()) { |
| 70 | + logger.debug(String.format( |
| 71 | + "Skipping candidate TestExecutionListener [%s] due to a missing dependency. " + |
| 72 | + "Specify custom listener classes or make the default listener classes " + |
| 73 | + "and their required dependencies available. Offending class: [%s]", |
| 74 | + listenerClass.getName(), cause.getMessage())); |
| 75 | + } |
| 76 | + } |
| 77 | + else { |
| 78 | + throw ex; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + return listeners; |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments