// ino.module.Constant.8556.package 
package mycompiler.myclass;
// ino.end

// ino.module.Constant.8556.import 
import java.util.Vector;

import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import typinferenz.JavaCodeResult;
import typinferenz.ResultSet;
import typinferenz.assumptions.TypeAssumptions;
import mycompiler.mybytecode.AttributeInfo;
import mycompiler.mybytecode.ClassFile;
import mycompiler.mybytecode.JVMCode;
import mycompiler.MyCompiler;
import mycompiler.myexception.JVMCodeException;
import mycompiler.mymodifier.Modifiers;
import mycompiler.mystatement.Expr;
import mycompiler.mystatement.Literal;
import mycompiler.mytype.Type;
// ino.end
import mycompiler.mytypereconstruction.replacementlistener.CReplaceTypeEvent;

// ino.class.Constant.23212.description type=javadoc
/**
 * Enthaelt die Eigenschaften einer Konstante (Name, Typ, Modifier, Wert)
 * 
 * @author SCJU
 * 
 */
// ino.end

// ino.class.Constant.23212.declaration 
public class Constant extends FieldDecl
// ino.end
// ino.class.Constant.23212.body
{

    // ino.attribute.name.23216.declaration 
    protected String name;      // Name der Konstante
    // ino.end
    // ino.attribute.mod.23219.declaration 
    protected Modifiers mod = new Modifiers();  // Access-Flags
    // ino.end
    // ino.attribute.exp.23222.declaration 
    protected Expr exp;         // Wert der Konstante
    // ino.end
    // ino.attribute.typ.23225.declaration 
    protected Type typ;         // Typ der Konstante
    // ino.end

    // ino.method.Constant.23228.definition 
    public Constant(String name, Modifiers mod)
    // ino.end
    // ino.method.Constant.23228.body 
    {
        this.name = name;
        this.mod = mod;
    }
    // ino.end

    // ino.method.getValue.23231.defdescription type=javadoc
    /**
     * Gibt den Wert der Konstantendefinition zurueck.
     */
    // ino.end
    // ino.method.getValue.23231.definition 
    public Expr getValue()
    // ino.end
    // ino.method.getValue.23231.body 
    {
        return exp;
    }
    // ino.end

    // ino.method.setValue.23234.defdescription type=javadoc
    /**
     * Legt den Wert der Konstantendefinition fest.
     */
    // ino.end
    // ino.method.setValue.23234.definition 
    public void setValue(Expr exp)
    // ino.end
    // ino.method.setValue.23234.body 
    {
        this.exp = exp;
    }
    // ino.end

    // ino.method.getType.23237.defdescription type=javadoc
    /**
     * Gibt den Typ der Konstantendefinition zurueck.
     */
    // ino.end
    // ino.method.getType.23237.definition 
    public Type getType()
    // ino.end
    // ino.method.getType.23237.body 
    {
        return typ;
    }
    // ino.end

    // ino.method.setType.23240.defdescription type=javadoc
    /**
     * Legt den Typ der Konstantendefinition fest
     */
    // ino.end
    // ino.method.setType.23240.definition 
    public void setType(Type typ)
    // ino.end
    // ino.method.setType.23240.body 
    {
        this.typ = typ;
    }
    // ino.end

    // ino.method.getTypeName.23243.definition 
    public String getTypeName()
    // ino.end
    // ino.method.getTypeName.23243.body 
    {
        return name;
    }
    // ino.end

    // ino.method.getName.23246.definition 
    public String getName()
    // ino.end
    // ino.method.getName.23246.body 
    {
        return name;
    }
    // ino.end
    // ino.method.setName.23249.definition 
    public void setName(String name)
    // ino.end
    // ino.method.setName.23249.body 
    {
        this.name = name;
    }
    // ino.end

    // ino.method.getModifiers.23252.definition 
    public Modifiers getModifiers()
    // ino.end
    // ino.method.getModifiers.23252.body 
    {
        return mod;
    }
    // ino.end

    // ino.method.setModifiers.23255.definition 
    public void setModifiers(Modifiers modifiers)
    // ino.end
    // ino.method.setModifiers.23255.body 
    {
        this.mod = modifiers;
    }
    // ino.end
    
    // ino.method.codegen.23258.definition 
    public void codegen(ClassFile classfile, Vector paralist)
    throws JVMCodeException
    // ino.end
// ino.method.codegen.23258.body 
{

        // Zugehoerigen Typ (I, Z, C, Ljava/lang/String;) fuer den Typ ermitteln
        String bcgType = JVMCode.get_codegen_Type(typ.getName(), paralist);
        
        if (getValue() == null || !(getValue() instanceof Literal) ) {
            throw new JVMCodeException("Die Generierung der Konstante wird nicht unterstuetzt!");
        }
        
        // Attribut fuer Wertzuweisung erstellen
        short index = (short) ((Literal)getValue()).ConstantCodegen(classfile);
        AttributeInfo attrinfo = new AttributeInfo();
        attrinfo.set_attribute_name_index(classfile.getConstantValueID());
        attrinfo.set_info(JVMCode.shortToBytes(index));

        
        // Field hinzufuegen
        classfile.add_field(name, bcgType, mod.calculate_access_flags(), attrinfo);
    }
// ino.end

    // ino.method.getOffset.23261.definition 
    public int getOffset()
    // ino.end
    // ino.method.getOffset.23261.body 
    {
        if(declid.size()<=0)
            return MyCompiler.NO_LINENUMBER;
        else{
            return declid.elementAt(0).getOffset();
        }
    }
    // ino.end

    // ino.method.getLineNumber.23264.definition 
    public int getLineNumber()
    // ino.end
    // ino.method.getLineNumber.23264.body 
    {
        if(declid.size()<=0)
            return MyCompiler.NO_LINENUMBER;
        else{
            return declid.elementAt(0).getLineNumber();
        }
    }
    // ino.end

	@Override
	public JavaCodeResult printJavaCode(ResultSet resultSet) {
		throw new NotImplementedException();
	}

	@Override
	public TypeAssumptions createTypeAssumptions(Class classmember) {
		throw new NotImplementedException();
		//return null;
	}

	@Override
	public void replaceType(CReplaceTypeEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public int getTypeLineNumber() {
		// TODO Auto-generated method stub
		return 0;
	}

}
// ino.end