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

ch.lambdaj.function.argument.ProxyArgument Maven / Gradle / Ivy

package ch.lambdaj.function.argument;

import static ch.lambdaj.function.argument.ArgumentsFactory.*;

import java.lang.ref.*;
import java.lang.reflect.*;

import ch.lambdaj.proxy.*;

public class ProxyArgument extends InvocationInterceptor {
	
	private Class proxiedClass;
	
	private WeakReference invocationSequence;
	
	private int proxyId;
	
	ProxyArgument(Class proxiedClass, InvocationSequence invocationSequence) {
		this.proxiedClass = proxiedClass;
		this.invocationSequence = new WeakReference(invocationSequence);
		proxyId = getNextPlaceholderId();
	}

	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		String methodName = method.getName(); 
		if (methodName.equals("hashCode")) return invocationSequence.hashCode();
		if (methodName.equals("equals")) return invocationSequence.equals(args[0]);
		
		// Add this invocation to the current invocation sequence
		InvocationSequence currentInvocationSequence = new InvocationSequence(invocationSequence.get(), new Invocation(proxiedClass, method, args));
		Class returnClass = method.getReturnType();
		
		// Creates a new proxy propagating the invocation sequence
		return createArgument(returnClass, currentInvocationSequence);
	}
	
	@Override
	public boolean equals(Object other) {
		return other instanceof ProxyArgument ? proxyId == ((ProxyArgument)other).proxyId : false;
	}
	
	@Override
	public int hashCode() {
		return proxyId;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy