FilterClassLoader.java revision 2610:d4c30fc77c75
1274987Srpaulo/*
2274987Srpaulo * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
3274987Srpaulo * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4274987Srpaulo *
5274987Srpaulo * This code is free software; you can redistribute it and/or modify it
6274987Srpaulo * under the terms of the GNU General Public License version 2 only, as
7274987Srpaulo * published by the Free Software Foundation.
8274987Srpaulo *
9274987Srpaulo * This code is distributed in the hope that it will be useful, but WITHOUT
10274987Srpaulo * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11274987Srpaulo * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12274987Srpaulo * version 2 for more details (a copy is included in the LICENSE file that
13274987Srpaulo * accompanied this code).
14274987Srpaulo *
15274987Srpaulo * You should have received a copy of the GNU General Public License version
16274987Srpaulo * 2 along with this work; if not, write to the Free Software Foundation,
17274987Srpaulo * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18274987Srpaulo *
19274987Srpaulo * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20274987Srpaulo * or visit www.oracle.com if you need additional information or have any
21274987Srpaulo * questions.
22274987Srpaulo */
23274987Srpaulo
24274987Srpaulopackage jdk.test.lib.classloader;
25274987Srpaulo
26274987Srpauloimport java.util.function.Predicate;
27274987Srpaulo/**
28274987Srpaulo * A classloader, which using target classloader in case provided condition
29274987Srpaulo * for class name is met, and using parent otherwise
30274987Srpaulo */
31274987Srpaulopublic class FilterClassLoader extends ClassLoader {
32274987Srpaulo
33274987Srpaulo    private final ClassLoader target;
34274987Srpaulo    private final Predicate<String> condition;
35274987Srpaulo
36274987Srpaulo    public FilterClassLoader(ClassLoader target, ClassLoader parent,
37274987Srpaulo            Predicate<String> condition) {
38274987Srpaulo        super(parent);
39274987Srpaulo        this.condition = condition;
40274987Srpaulo        this.target = target;
41274987Srpaulo    }
42274987Srpaulo
43274987Srpaulo    @Override
44274987Srpaulo    public Class<?> loadClass(String name) throws ClassNotFoundException {
45274987Srpaulo        if (condition.test(name)) {
46274987Srpaulo            return target.loadClass(name);
47274987Srpaulo        }
48274987Srpaulo        return super.loadClass(name);
49274987Srpaulo    }
50274987Srpaulo}
51274987Srpaulo