001/*
002 * Trident - A Multithreaded Server Alternative
003 * Copyright 2014 The TridentSDK Team
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * 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 */
017
018package net.tridentsdk.reflect;
019
020import com.esotericsoftware.reflectasm.ConstructorAccess;
021import com.esotericsoftware.reflectasm.FieldAccess;
022import com.esotericsoftware.reflectasm.MethodAccess;
023
024import java.lang.reflect.Field;
025
026/**
027 * Accessors to the members of a class, does not actually have a ReflectASM equivalent
028 *
029 * @author The TridentSDK Team
030 * @since 0.3-alpha-DP
031 */
032public class FastClass {
033    private final Class<?> cls;
034
035    private final FieldAccess fieldAccess;
036    private final MethodAccess methodAccess;
037    private final ConstructorAccess constructorAccess;
038
039    private FastClass(Class<?> cls) {
040        this.cls = cls;
041        this.fieldAccess = FieldAccess.get(cls);
042        this.methodAccess = MethodAccess.get(cls);
043        this.constructorAccess = ConstructorAccess.get(cls);
044    }
045
046    /**
047     * Creates a new FastClass from a Java class
048     *
049     * @param cls the class to use
050     * @return the member accessors for the class
051     */
052    public static FastClass get(Class<?> cls) {
053        return new FastClass(cls);
054    }
055
056    /**
057     * Creates a new FastClass from the class object of the object
058     *
059     * @param obj the object's class to use
060     * @return the member accessors for the class
061     */
062    public static FastClass get(Object obj) {
063        return get(obj.getClass());
064    }
065
066    /**
067     * Get a field from the class
068     *
069     * @param name Name of the field
070     * @return FastField instance
071     */
072    public FastField fieldBy(String name) {
073        return new FastField(this, this.fieldAccess, name);
074    }
075
076    /**
077     * Get a method from the class
078     *
079     * @param name Name of the method
080     * @return FastMethod instance
081     */
082    public FastMethod methodBy(Object object, String name) {
083        return new FastMethod(object, this.methodAccess, name);
084    }
085
086    /**
087     * Get all fields from the class
088     *
089     * @return the fast field representation of field members from the class
090     */
091    public FastField[] fields() {
092        Field[] fields = this.cls.getDeclaredFields();
093        FastField[] fastFields = new FastField[fields.length];
094
095        for (int i = 0; i < fields.length; i += 1) {
096            fastFields[i] = new FastField(this, this.fieldAccess, fields[i].getName());
097        }
098
099        return fastFields;
100    }
101
102    /**
103     * Get the default constructor found
104     *
105     * @return the default FastConstructor
106     */
107    public FastConstructor constructor() {
108        return new FastConstructor(this.constructorAccess);
109    }
110
111    /**
112     * As the vanilla reflection class form
113     *
114     * @return the class that is used to derive all access from
115     */
116    public Class<?> asClass() {
117        return this.cls;
118    }
119}