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

org.directwebremoting.extend.ConstructorProperty Maven / Gradle / Ivy

Go to download

DWR is easy Ajax for Java. It makes it simple to call Java code directly from Javascript. It gets rid of almost all the boilerplate code between the web browser and your Java code. This version 4.0.2 works with Jakarta Servlet 4.0.2.

There is a newer version: 5.0.0
Show newest version
package org.directwebremoting.extend;

import java.lang.reflect.Constructor;
import java.lang.reflect.Type;

import org.directwebremoting.ConversionException;
import org.directwebremoting.util.LocalUtil;

/**
 * @author Joe Walker [joe at getahead dot ltd dot uk]
 */
public class ConstructorProperty implements Property
{
    /**
     * @param constructor ...
     * @param parameterName ...
     * @param parameterNumber ...
     */
    public ConstructorProperty(Constructor constructor, String parameterName, int parameterNumber)
    {
        this.constructor = constructor;
        this.parameterName = parameterName;
        this.parameterNumber = parameterNumber;
    }

    public String getName()
    {
        return parameterName;
    }

    public Class getPropertyType()
    {
        Type[] types = constructor.getGenericParameterTypes();
        if (parameterNumber >= types.length)
        {
            throw new IllegalArgumentException("parameterNumber=" + parameterNumber + " is too big when method=" + constructor.getName() + " returns genericParameterTypes.length=" + types.length);
        }

        Type parameterType = types[parameterNumber];
        return LocalUtil.toClass(parameterType, toString());
    }

    public Property createChild(int nextNewParameterNumber)
    {
        Type[] types = constructor.getGenericParameterTypes();
        if (parameterNumber >= types.length)
        {
            throw new IllegalArgumentException("parameterNumber=" + parameterNumber + " is too big when method=" + constructor.getName() + " returns genericParameterTypes.length=" + types.length);
        }
        Type parameterType = types[parameterNumber];
        return new NestedProperty(this, constructor, parameterType, parameterNumber, nextNewParameterNumber);
    }

    public Object getValue(Object bean) throws ConversionException
    {
        throw new UnsupportedOperationException("Can't get value from constructor parameter");
    }

    public void setValue(Object bean, Object value) throws ConversionException
    {
        throw new UnsupportedOperationException("Can't set value to constructor parameter");
    }

    @Override
    public int hashCode()
    {
        return constructor.hashCode() + parameterNumber;
    }

    @Override
    public boolean equals(Object obj)
    {
        if (obj == this)
        {
            return true;
        }

        if (obj == null || this.getClass() != obj.getClass())
        {
            return false;
        }

        ConstructorProperty that = (ConstructorProperty) obj;

        if (!this.constructor.equals(that.constructor))
        {
            return false;
        }

        return this.parameterNumber == that.parameterNumber;
    }

    @Override
    public String toString()
    {
        return "ConstructorProperty[ctor=" + constructor.getName() + ",p#=" + parameterNumber + "]";
    }

    private final Constructor constructor;

    private final String parameterName;

    private final int parameterNumber;
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy