001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.util.reference; 018 019import java.io.Serializable; 020import java.lang.ref.WeakReference; 021import java.util.function.Supplier; 022 023import org.apache.wicket.core.util.lang.WicketObjects; 024 025/** 026 * A serialization-safe reference to a {@link Class} 027 * 028 * @author igor 029 * 030 * @param <T> 031 * type of class 032 */ 033public class ClassReference<T> implements Serializable, Supplier<Class<T>> 034{ 035 private static final long serialVersionUID = 1L; 036 037 private transient WeakReference<Class<T>> cache; 038 private final String name; 039 040 /** 041 * Constructor 042 * 043 * @param clazz 044 * The referenced class 045 */ 046 public ClassReference(Class<T> clazz) 047 { 048 name = clazz.getName(); 049 cache(clazz); 050 } 051 052 /** 053 * @return the {@link Class} stored in this reference 054 */ 055 @Override 056 public Class<T> get() 057 { 058 Class<T> clazz = cache != null ? cache.get() : null; 059 if (clazz == null) 060 { 061 clazz = WicketObjects.resolveClass(name); 062 if (clazz == null) 063 { 064 throw new RuntimeException("Could not resolve class: " + name); 065 } 066 cache(clazz); 067 } 068 return clazz; 069 } 070 071 private void cache(Class<T> clazz) 072 { 073 cache = new WeakReference<>(clazz); 074 } 075 076 /** 077 * Diamond operator factory 078 * 079 * @param clazz 080 * The referenced class 081 * @return class reference 082 */ 083 public static <T> ClassReference<T> of(Class<T> clazz) 084 { 085 return new ClassReference<>(clazz); 086 } 087}