Path: blob/master/src/jdk.dynalink/share/classes/jdk/dynalink/beans/StaticClass.java
41161 views
/*1* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* This file is available under and governed by the GNU General Public27* License version 2 only, as published by the Free Software Foundation.28* However, the following notice accompanied the original version of this29* file, and Oracle licenses the original version of this file under the BSD30* license:31*/32/*33Copyright 2009-2013 Attila Szegedi3435Redistribution and use in source and binary forms, with or without36modification, are permitted provided that the following conditions are37met:38* Redistributions of source code must retain the above copyright39notice, this list of conditions and the following disclaimer.40* Redistributions in binary form must reproduce the above copyright41notice, this list of conditions and the following disclaimer in the42documentation and/or other materials provided with the distribution.43* Neither the name of the copyright holder nor the names of44contributors may be used to endorse or promote products derived from45this software without specific prior written permission.4647THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS48IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED49TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A50PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER51BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR52CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF53SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR54BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,55WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR56OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF57ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.58*/5960package jdk.dynalink.beans;6162import java.io.Serializable;63import java.util.Objects;64import jdk.dynalink.StandardOperation;6566/**67* Object that allows access to the static members of a class (its static68* methods, properties, and fields), as well as construction of instances using69* {@link StandardOperation#NEW} operation. In Dynalink, {@link Class} objects70* are not treated specially and act as ordinary Java objects; you can use e.g.71* {@code GET:PROPERTY:superclass} as a property getter to72* invoke {@code clazz.getSuperclass()}. On the other hand, you can not use73* {@code Class} objects to access static members of a class, nor to create new74* instances of the class using {@code NEW}. This is consistent with how75* {@code Class} objects behave in Java: in Java, you write e.g.76* {@code new BitSet()} instead of {@code new BitSet.class()}. Similarly, you77* write {@code System.out} and not {@code System.class.out}. It is this aspect78* of using a class name as the constructor and a namespace for static members79* that {@code StaticClass} embodies.80* <p>81* Objects of this class are recognized by the {@link BeansLinker} as being82* special, and operations on them will be linked against the represented class'83* static members. The {@code "class"} synthetic property is additionally84* recognized and returns the Java {@link Class} object, just as in Java85* {@code System.class} evaluates to the {@code Class} object for the86* {@code} System class. Conversely, {@link Class} objects exposed through87* {@link BeansLinker} expose the {@code "static"} synthetic property which88* returns their {@code StaticClass} object (there is no equivalent to this in89* Java).90* <p>91* In summary, instances of this class act as namespaces for static members and92* as constructors for classes, much the same way as specifying a class name in93* Java language does, except that in Java this is just a syntactic element,94* while in Dynalink they are expressed as actual objects.95* <p>{@code StaticClass} objects representing Java array types will act as96* constructors taking a single int argument and create an array of the97* specified size.98* <p>99* If the class has several constructors, {@link StandardOperation#NEW} on100* {@code StaticClass} will try to select the most specific applicable101* constructor. You might want to expose a mechanism in your language for102* selecting a constructor with an explicit signature through103* {@link BeansLinker#getConstructorMethod(Class, String)}.104*/105public final class StaticClass implements Serializable {106private static final ClassValue<StaticClass> staticClasses = new ClassValue<>() {107@Override108protected StaticClass computeValue(final Class<?> type) {109return new StaticClass(type);110}111};112113private static final long serialVersionUID = 1L;114115/**116* The runtime {@code Class} object whose static members this117* {@code StaticClass} represents.118*/119private final Class<?> clazz;120121/*private*/ StaticClass(final Class<?> clazz) {122this.clazz = Objects.requireNonNull(clazz);123}124125/**126* Retrieves the {@link StaticClass} instance for the specified class.127* @param clazz the class for which the static facet is requested.128* @return the {@link StaticClass} instance representing the specified class.129*/130public static StaticClass forClass(final Class<?> clazz) {131return staticClasses.get(clazz);132}133134/**135* Returns the represented Java class.136* @return the represented Java class.137*/138public Class<?> getRepresentedClass() {139return clazz;140}141142@Override143public String toString() {144return "StaticClass[" + clazz.getName() + "]";145}146147/**148* Returns {@link #forClass(Class)} for the underlying {@code clazz} field149* ensuring that deserialization doesn't create non-canonical instances.150* @return {@link #forClass(Class)} for the underlying {@code clazz} field.151*/152private Object readResolve() {153return forClass(clazz);154}155}156157158