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 * http://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.commons.weaver.model;
020
021import java.lang.reflect.Member;
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.List;
025
026import org.apache.commons.weaver.utils.Args;
027
028/**
029 * Represents a {@link Weavable} "executable".
030 *
031 * @param <SELF> own type
032 * @param <TARGET> target executable type
033 * @param <T> owning type
034 * @param <P> parameter type
035 */
036public abstract class WeavableExecutable
037    <SELF extends WeavableExecutable<SELF, TARGET, T, P>,
038    TARGET extends Member,
039    T,
040    P extends WeavableParameter<P, SELF, TARGET, T>>
041    extends NestedWeavable<SELF, TARGET, WeavableClass<T>, Class<T>> {
042
043    private final List<P> parameters;
044
045    /**
046     * Create a new {@link WeavableExecutable} instance.
047     * @param target executable
048     * @param parent enclosing {@link WeavableClass}
049     */
050    protected WeavableExecutable(final TARGET target, final WeavableClass<T> parent) {
051        super(target, parent);
052        final List<P> params = new ArrayList<P>();
053        final int paramCount = getParameterTypes().length;
054        for (int i = 0; i < paramCount; i++) {
055            params.add(createParameter(i));
056        }
057        parameters = Collections.unmodifiableList(params);
058    }
059
060    /**
061     * Create an appropriate {@link WeavableParameter} object.
062     * @param index of parameter
063     * @return {@code P}
064     */
065    protected abstract P createParameter(int index);
066
067    /**
068     * Get the parameter types of {@link #getTarget()}.
069     * @return {@link Class}[]
070     */
071    protected abstract Class<?>[] getParameterTypes();
072
073    /**
074     * {@inheritDoc}
075     */
076    @Override
077    protected int localCompareTo(final SELF obj) {
078        return Args.compare(getParameterTypes(), obj.getParameterTypes());
079    }
080
081    /**
082     * Get the parameter at the specified index.
083     * @param index {@code int}
084     * @return {@code P}
085     */
086    public P getWeavableParameter(final int index) {
087        return parameters.get(index);
088    }
089
090    /**
091     * Get the parameters declared by this {@link WeavableExecutable}.
092     * @return {@link Iterable} of {@code P}
093     */
094    public Iterable<P> getParameters() {
095        return parameters;
096    }
097
098}