001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.bcel.util;
020
021import java.util.Map;
022import java.util.concurrent.ConcurrentHashMap;
023
024/**
025 * This repository is used in situations where a Class is created outside the realm of a ClassLoader. Classes are loaded
026 * from the file systems using the paths specified in the given class path. By default, this is the value returned by
027 * ClassPath.getClassPath().
028 * <p>
029 * This repository uses a factory design, allowing it to maintain a collection of different classpaths, and as such It
030 * is designed to be used as a singleton per classpath.
031 * </p>
032 *
033 * @see org.apache.bcel.Repository
034 */
035public class SyntheticRepository extends MemorySensitiveClassPathRepository {
036
037    private static final Map<ClassPath, SyntheticRepository> MAP = new ConcurrentHashMap<>(); // CLASSPATH X REPOSITORY
038
039    /**
040     * Gets the singleton instance for the system class path.
041     *
042     * @return The singleton instance.
043     */
044    public static SyntheticRepository getInstance() {
045        return getInstance(ClassPath.SYSTEM_CLASS_PATH);
046    }
047
048    /**
049     * Gets the singleton instance for the given class path.
050     *
051     * @param classPath The class path.
052     * @return The singleton instance.
053     */
054    public static SyntheticRepository getInstance(final ClassPath classPath) {
055        return MAP.computeIfAbsent(classPath, SyntheticRepository::new);
056    }
057
058    private SyntheticRepository(final ClassPath path) {
059        super(path);
060    }
061}