public Statement apply()

in meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MeecrowaveRuleBase.java [44:79]


    public Statement apply(final Statement base, final Description description) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                final Thread thread = Thread.currentThread();
                ClassLoader oldCL = thread.getContextClassLoader();
                boolean unlocked = false;
                doLockContext();
                try {
                    ClassLoader newCl = getClassLoader();
                    if (newCl != null) {
                        thread.setContextClassLoader(newCl);
                    }
                    try (final AutoCloseable closeable = onStart()) {
                        doUnlockContext(unlocked);
                        unlocked = true;

                        started.set(true);
                        final Collection<CreationalContext<?>> contexts = toInject.stream()
                                                                                  .map(MeecrowaveRuleBase::doInject)
                                                                                  .collect(toList());
                        try {
                            base.evaluate();
                        } finally {
                            contexts.forEach(CreationalContext::release);
                            started.set(false);
                        }
                    } finally {
                        thread.setContextClassLoader(oldCL);
                    }
                } finally {
                    doUnlockContext(unlocked);
                }
            }
        };
    }