001/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*- 002 * 003 * Copyright © 2025 microBean™. 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 006 * the License. You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 011 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 012 * specific language governing permissions and limitations under the License. 013 */ 014package org.microbean.proxy; 015 016import java.util.Objects; 017 018import java.util.function.Supplier; 019 020import org.microbean.construct.Domain; 021 022/** 023 * An abstract base class for subclassses that create {@linkplain Proxy proxies}. 024 * 025 * @param <PS> the {@link ProxySpecification} type 026 * 027 * @author <a href="https://about.me/lairdnelson" target="_top">Laird Nelson</a> 028 */ 029public abstract sealed class AbstractProxier<PS extends ProxySpecification> 030 permits AbstractReflectiveProxier, AbstractToolkitProxier { 031 032 private final Domain domain; 033 034 /** 035 * Creates a new {@link AbstractProxier} implementation. 036 * 037 * @param domain a {@link Domain}; must not be {@code null} 038 * 039 * @exception NullPointerException if {@code domain} is {@code null} 040 */ 041 protected AbstractProxier(final Domain domain) { 042 this.domain = Objects.requireNonNull(domain, "domain"); 043 } 044 045 /** 046 * Returns the {@link ClassLoader} for loading classes. 047 * 048 * <p>The default implementation of this method returns the return value of an invocation of the {@link 049 * Thread#getContextClassLoader()} method.</p> 050 * 051 * <p>Overrides of this method must not return {@code null} or undefined behavior may result.</p> 052 * 053 * @return a non-{@code null} {@link ClassLoader} 054 */ 055 protected ClassLoader classLoader() { 056 return Thread.currentThread().getContextClassLoader(); 057 } 058 059 /** 060 * Returns the {@link Domain} supplied at construction time. 061 * 062 * @return a non-{@code null} {@link Domain} 063 */ 064 protected final Domain domain() { 065 return this.domain; 066 } 067 068 /** 069 * Returns a {@link Proxy} appropriate for the supplied proxy specification and {@link Supplier} of contextual 070 * instances. 071 * 072 * @param <R> the contextual instance type 073 * 074 * @param ps an appropriate proxy specification; must not be {@code null} 075 * 076 * @param instanceSupplier a {@link Supplier} of contextual instances; must not be {@code null}; may or may not create 077 * a new contextual instance each time it is invoked; may or may not be invoked multiple times depending on the 078 * subclass implementation 079 * 080 * @return a non-{@code null} {@link Proxy} 081 * 082 * @exception NullPointerException if any argument is {@code null} 083 */ 084 public abstract <R> Proxy<R> proxy(final PS ps, final Supplier<? extends R> instanceSupplier); 085 086}