/*
* Copyright (C) 2001 Mika Riekkinen, Joni Suominen
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package alt.jiapi.reflect;
import java.lang.reflect.Modifier;
import java.util.Iterator;
import java.util.List;
import alt.jiapi.file.ClassFile;
import alt.jiapi.file.ConstantPool;
import alt.jiapi.file.Field;
import alt.jiapi.file.Attribute;
/**
* This class represents a Field.
*
* @author Mika Riekkinen
* @author Joni Suominen
* @version $Revision: 1.12 $ $Date: 2004/03/28 14:11:20 $
*/
public class JiapiField {
private JiapiClass declaringClass;
private Field field;
JiapiField(Field f) {
this.field = f;
}
/**
* Get the name of this field.
*
* @return Name of this field
*/
public String getName() {
return field.getName();
}
/**
* Get the type of this field. This involves loading of the the type
* through the same loader that was used to load the declaring class.
*
* @return a JiapiClass which represents a type of this field
* @exception ClassNotFoundException is thrown, if JiapiClass could not
* be loaded
* @see #getTypeName()
*/
public JiapiClass getType() throws ClassNotFoundException {
Loader loader = getDeclaringClass().getLoader();
JiapiClass clazz = null;
try {
clazz = loader.loadClass(getTypeName());
}
catch(java.io.IOException ioe) {
throw new ClassNotFoundException(getTypeName());
}
return clazz;
}
/**
* Get the type of this field.
*
* @return a String which represents a type of this field
*/
public String getTypeName() {
String descriptor = getDescriptor();
String type = TypeHelper.descriptorToType(descriptor);
return type;
}
/**
* Get the field descriptor.
*/
String getDescriptor() {
return field.getDescriptor();
}
/**
* Get the modifiers of this field.
* @see java.lang.reflect.Modifier
*/
public int getModifiers() {
return field.getAccessFlags();
}
/**
* Sets the modifiers of this field.
*
* @param modifiers new modifiers
* @see java.lang.reflect.Modifier
*/
void setModifiers(int modifiers) {
// // NOTE: We Should update constantpool here
// this.modifiers = modifiers;
throw new RuntimeException("Operation not supported");
}
Field getField() {
return field;
}
/**
* Gets the JiapiClass, that declares this JiapiField.
*
* @return Declaring class, or null if one has not been assigned to
* this JiapiField
*/
public JiapiClass getDeclaringClass() {
return declaringClass;
}
/**
*/
public String toString() {
StringBuffer sb =
new StringBuffer(Modifier.toString(getModifiers()));
sb.append(' ');
sb.append(getTypeName());
sb.append(' ');
sb.append(getName());
return sb.toString();
}
/**
* Sets the declaring class.
*
* @param declaringClass A JiapiClass, that declares this JiapiField
*/
void setDeclaringClass(JiapiClass declaringClass) {
this.declaringClass = declaringClass;
}
/**
* Gets all the field attributes defined in ClassFile.
*/
private List getAttributes() {
return field.getAttributes();
}
/**
* Gets all the method attributes defined in ClassFile.
*/
private Attribute getAttribute(String name) {
List l = getAttributes();
Iterator i = l.iterator();
// ConstantPool cp = declaringClass.getConstantPool();
while(i.hasNext()) {
Attribute a = (Attribute)i.next();
if (a.getName().equals(name)) {
return a;
}
}
return null;
}
}