package net.sourceforge.javautil.bytecode.api;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
import net.sourceforge.javautil.bytecode.api.type.AbstractType;
import net.sourceforge.javautil.bytecode.api.type.method.BytecodeContextMethod;
import net.sourceforge.javautil.bytecode.api.type.method.invocation.MethodInvocation;
/**
* The declaration of a field in an {@link AbstractType}.
*
* @author elponderador
* @author $Author$
* @version $Id$
*/
public class BytecodeFieldDeclared extends BytecodeMemberDeclared implements IBytecodeAssignable<BytecodeContextMethod>, IBytecodeMember, IBytecodeField {
protected final Field field;
protected final IBytecodeResolvable declaringType;
public BytecodeFieldDeclared(BytecodeResolutionPool pool, IBytecodeResolvable declaringType, Field field) {
super(pool, field);
this.declaringType = declaringType;
this.field = field;
}
public String getName() { return field.getName(); }
public IBytecodeResolvable getDeclaringType() { return this.declaringType; }
public TypeMemberAccess getAccess() { return TypeMemberAccess.getFor(field.getModifiers()); }
public MethodInvocation createInvocation(BytecodeContextMethod context, String name, IBytecodeReferenceable... parameters) {
return new MethodInvocation(this, getType(context).findMethod(context.getResolutionPool(), name, context.getTypes(parameters)), parameters);
}
public TypeDescriptor getType() { return TypeDescriptor.getFor(field.getType()); }
public IBytecodeResolvable getType(BytecodeContextMethod ctx) {
return ctx.getResolutionPool().resolve(getType().getClassName());
}
public void load(BytecodeContextMethod context) {
if (this.getAccess().isStatic()) {
context.getWriter().loadStaticField(context, this.declaringType, field.getName());
} else {
context.getWriter().loadField(context, this.declaringType, field.getName());
}
}
public void store(BytecodeContextMethod context) {
if (this.getAccess().isStatic()) {
context.getWriter().storeStaticField(context, this.declaringType, field.getName());
} else {
context.getWriter().storeField(context, this.declaringType, field.getName());
}
}
}