All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.commons.weaver.model.WeavableClass Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.commons.weaver.model;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Comparator;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;

import org.apache.commons.weaver.utils.Args;

/**
 * {@link Weavable} {@link Class}.
 *
 * @param  type
 */
public class WeavableClass extends NestedWeavable, Class, WeavablePackage, Package> {
    private final ConcurrentNavigableMap> fields =
        new ConcurrentSkipListMap>();

    private final ConcurrentNavigableMap, WeavableConstructor> ctors =
        new ConcurrentSkipListMap, WeavableConstructor>(new Comparator>() {

            @Override
            public int compare(final Constructor ctor1, final Constructor ctor2) {
                return Args.compare(ctor1.getParameterTypes(), ctor2.getParameterTypes());
            }
        });

    private final ConcurrentNavigableMap> methods =
        new ConcurrentSkipListMap>(new Comparator() {

            @Override
            public int compare(final Method methd1, final Method methd2) {
                final int result = methd1.getName().compareTo(methd2.getName());
                return result == 0 ? Args.compare(methd1.getParameterTypes(), methd2.getParameterTypes()) : result;
            }
        });

    /**
     * Create a new {@link WeavableClass} instance.
     * @param target {@link Class}
     * @param parent {@link WeavablePackage} enclosing
     */
    public WeavableClass(final Class target, final WeavablePackage parent) {
        super(target, parent);
    }

    /**
     * Get a {@link WeavableField} representing {@code fld}.
     * @param fld to wrap
     * @return {@link WeavableField}
     */
    public WeavableField getWeavable(final Field fld) {
        final String key = fld.getName();
        if (fields.containsKey(key)) {
            final WeavableField result = fields.get(key);
            return result;
        }
        final WeavableField result = new WeavableField(fld, this);
        final WeavableField faster = fields.putIfAbsent(key, result);
        return faster == null ? result : faster;
    }

    /**
     * Get a {@link WeavableMethod} representing {@code mt}.
     * @param methd to wrap
     * @return {@link WeavableMethod}
     */
    public WeavableMethod getWeavable(final Method methd) {
        if (methods.containsKey(methd)) {
            final WeavableMethod result = methods.get(methd);
            return result;
        }
        final WeavableMethod result = new WeavableMethod(methd, this);
        final WeavableMethod faster = methods.putIfAbsent(methd, result);
        return faster == null ? result : faster;
    }

    /**
     * Get a {@link WeavableConstructor} representing {@code ctor}.
     * @param ctor to wrap
     * @return {@link WeavableConstructor}
     */
    public WeavableConstructor getWeavable(final Constructor ctor) {
        if (ctors.containsKey(ctor)) {
            final WeavableConstructor result = ctors.get(ctor);
            return result;
        }
        final WeavableConstructor result = new WeavableConstructor(ctor, this);
        final WeavableConstructor faster = ctors.putIfAbsent(ctor, result);
        return faster == null ? result : faster;
    }

    /**
     * Get {@link WeavableField}s of this {@link WeavableClass}.
     * @return {@link Iterable}
     */
    public Iterable> getFields() {
        return Collections.unmodifiableCollection(fields.values());
    }

    /**
     * Get {@link WeavableConstructor}s of this {@link WeavableClass}.
     * @return {@link Iterable}
     */
    public Iterable> getConstructors() {
        return Collections.unmodifiableCollection(ctors.values());
    }

    /**
     * Get {@link WeavableMethod}s of this {@link WeavableClass}.
     * @return {@link Iterable}
     */
    public Iterable> getMethods() {
        return Collections.unmodifiableCollection(methods.values());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected int localCompareTo(final WeavableClass obj) {
        return getTarget().getName().compareTo(obj.getTarget().getName());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy