1344N/A/*
1344N/A * reserved comment block
1344N/A * DO NOT REMOVE OR ALTER!
1344N/A */
1344N/A/*
1344N/A * Copyright 2001-2004 The Apache Software Foundation.
1344N/A *
1344N/A * Licensed under the Apache License, Version 2.0 (the "License");
1344N/A * you may not use this file except in compliance with the License.
1344N/A * You may obtain a copy of the License at
1344N/A *
1344N/A * http://www.apache.org/licenses/LICENSE-2.0
1344N/A *
1344N/A * Unless required by applicable law or agreed to in writing, software
1344N/A * distributed under the License is distributed on an "AS IS" BASIS,
1344N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1344N/A * See the License for the specific language governing permissions and
1344N/A * limitations under the License.
1344N/A */
1344N/A/*
1344N/A * $Id: ObjectType.java,v 1.2.4.1 2005/09/12 11:45:54 pvedula Exp $
1344N/A */
1344N/A
1344N/Apackage com.sun.org.apache.xalan.internal.xsltc.compiler.util;
3231N/A
6238N/Aimport com.sun.org.apache.bcel.internal.generic.ALOAD;
1344N/Aimport com.sun.org.apache.bcel.internal.generic.ASTORE;
1344N/Aimport com.sun.org.apache.bcel.internal.generic.BranchHandle;
1344N/Aimport com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
1344N/Aimport com.sun.org.apache.bcel.internal.generic.GOTO;
1344N/Aimport com.sun.org.apache.bcel.internal.generic.IFNULL;
1344N/Aimport com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
1344N/Aimport com.sun.org.apache.bcel.internal.generic.Instruction;
1344N/Aimport com.sun.org.apache.bcel.internal.generic.InstructionList;
6238N/Aimport com.sun.org.apache.bcel.internal.generic.PUSH;
1344N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.Constants;
6238N/Aimport com.sun.org.apache.xalan.internal.utils.ObjectFactory;
1344N/A
1344N/A/**
6238N/A * @author Todd Miller
1344N/A * @author Santiago Pericas-Geertsen
1344N/A */
1344N/Apublic final class ObjectType extends Type {
1344N/A
1344N/A private String _javaClassName = "java.lang.Object";
1344N/A private Class _clazz = java.lang.Object.class;
1344N/A
1344N/A /**
6238N/A * Used to represent a Java Class type such is required to support
6238N/A * non-static java functions.
6238N/A * @param javaClassName name of the class such as 'com.foo.Processor'
1344N/A */
1344N/A protected ObjectType(String javaClassName) {
1344N/A _javaClassName = javaClassName;
1344N/A
1344N/A try {
1344N/A _clazz = ObjectFactory.findProviderClass(javaClassName, true);
1344N/A }
6238N/A catch (ClassNotFoundException e) {
6238N/A _clazz = null;
6238N/A }
1344N/A }
1344N/A
1344N/A protected ObjectType(Class clazz) {
1344N/A _clazz = clazz;
1344N/A _javaClassName = clazz.getName();
1344N/A }
1344N/A
1344N/A /**
1344N/A * Must return the same value for all ObjectType instances. This is
1344N/A * needed in CastExpr to ensure the mapping table is used correctly.
1344N/A */
1344N/A public int hashCode() {
6290N/A return java.lang.Object.class.hashCode();
6290N/A }
6290N/A
1344N/A public boolean equals(Object obj) {
1344N/A return (obj instanceof ObjectType);
1344N/A }
6290N/A
6290N/A public String getJavaClassName() {
6290N/A return _javaClassName;
6290N/A }
6290N/A
1344N/A public Class getJavaClass() {
1344N/A return _clazz;
1344N/A }
6238N/A
1344N/A public String toString() {
1344N/A return _javaClassName;
1344N/A }
1344N/A
1344N/A public boolean identicalTo(Type other) {
6238N/A return this == other;
6238N/A }
6238N/A
6238N/A public String toSignature() {
1344N/A final StringBuffer result = new StringBuffer("L");
1344N/A result.append(_javaClassName.replace('.', '/')).append(';');
1344N/A return result.toString();
1344N/A }
public com.sun.org.apache.bcel.internal.generic.Type toJCType() {
return Util.getJCRefType(toSignature());
}
/**
* Translates a void into an object of internal type <code>type</code>.
* This translation is needed when calling external functions
* that return void.
*
* @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
*/
public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
Type type) {
if (type == Type.String) {
translateTo(classGen, methodGen, (StringType) type);
}
else {
ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
toString(), type.toString());
classGen.getParser().reportError(Constants.FATAL, err);
}
}
/**
* Expects an integer on the stack and pushes its string value by calling
* <code>Integer.toString(int i)</code>.
*
* @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
*/
public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
StringType type) {
final ConstantPoolGen cpg = classGen.getConstantPool();
final InstructionList il = methodGen.getInstructionList();
il.append(DUP);
final BranchHandle ifNull = il.append(new IFNULL(null));
il.append(new INVOKEVIRTUAL(cpg.addMethodref(_javaClassName,
"toString",
"()" + STRING_SIG)));
final BranchHandle gotobh = il.append(new GOTO(null));
ifNull.setTarget(il.append(POP));
il.append(new PUSH(cpg, ""));
gotobh.setTarget(il.append(NOP));
}
/**
* Translates an object of this type to the external (Java) type denoted
* by <code>clazz</code>. This method is used to translate parameters
* when external functions are called.
*/
public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
Class clazz) {
if (clazz.isAssignableFrom(_clazz))
methodGen.getInstructionList().append(NOP);
else {
ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
toString(), clazz.getClass().toString());
classGen.getParser().reportError(Constants.FATAL, err);
}
}
/**
* Translates an external Java type into an Object type
*/
public void translateFrom(ClassGenerator classGen,
MethodGenerator methodGen, Class clazz) {
methodGen.getInstructionList().append(NOP);
}
public Instruction LOAD(int slot) {
return new ALOAD(slot);
}
public Instruction STORE(int slot) {
return new ASTORE(slot);
}
}