001/* 002 * JDrupes Builder 003 * Copyright (C) 2025 Michael N. Lipp 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU Affero General Public License as 007 * published by the Free Software Foundation, either version 3 of the 008 * License, or (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU Affero General Public License for more details. 014 * 015 * You should have received a copy of the GNU Affero General Public License 016 * along with this program. If not, see <https://www.gnu.org/licenses/>. 017 */ 018 019package org.jdrupes.builder.java; 020 021import java.lang.reflect.Proxy; 022import java.nio.file.Path; 023import org.jdrupes.builder.api.FileResource; 024import org.jdrupes.builder.api.Proxyable; 025import org.jdrupes.builder.api.ResourceType; 026import org.jdrupes.builder.core.ForwardingHandler; 027 028/// A [FileResource] that represents a Java jar. 029/// 030public class DefaultLibraryJarFile extends DefaultJarFile 031 implements LibraryJarFile { 032 033 /// Instantiates a new library jar file. 034 /// 035 /// @param type the resource type 036 /// @param path the path 037 /// 038 protected DefaultLibraryJarFile(ResourceType<? extends JarFile> type, 039 Path path) { 040 super(type, path); 041 } 042 043 /// Creates a jar file. 044 /// 045 /// @param <T> the generic type 046 /// @param type the type 047 /// @param path the path 048 /// @return the jar file 049 /// 050 @SuppressWarnings({ "unchecked" }) 051 public static <T extends JarFile> T createLibraryJarFile( 052 ResourceType<T> type, Path path) { 053 return (T) Proxy.newProxyInstance(type.rawType().getClassLoader(), 054 new Class<?>[] { type.rawType(), Proxyable.class }, 055 new ForwardingHandler(new DefaultLibraryJarFile(type, path))); 056 } 057}