001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the "License"); 007 * you may not use this file except in compliance with the License. 008 * You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 /* 019 * $Id: VoidType.java 468649 2006-10-28 07:00:55Z minchau $ 020 */ 021 022 package org.apache.xalan.xsltc.compiler.util; 023 024 import org.apache.bcel.generic.Instruction; 025 import org.apache.bcel.generic.InstructionList; 026 import org.apache.bcel.generic.PUSH; 027 import org.apache.xalan.xsltc.compiler.Constants; 028 029 /** 030 * @author Jacek Ambroziak 031 * @author Santiago Pericas-Geertsen 032 */ 033 public final class VoidType extends Type { 034 protected VoidType() {} 035 036 public String toString() { 037 return "void"; 038 } 039 040 public boolean identicalTo(Type other) { 041 return this == other; 042 } 043 044 public String toSignature() { 045 return "V"; 046 } 047 048 public org.apache.bcel.generic.Type toJCType() { 049 return null; // should never be called 050 } 051 052 public Instruction POP() { 053 return NOP; 054 } 055 056 /** 057 * Translates a void into an object of internal type <code>type</code>. 058 * This translation is needed when calling external functions 059 * that return void. 060 * 061 * @see org.apache.xalan.xsltc.compiler.util.Type#translateTo 062 */ 063 public void translateTo(ClassGenerator classGen, MethodGenerator methodGen, 064 Type type) { 065 if (type == Type.String) { 066 translateTo(classGen, methodGen, (StringType) type); 067 } 068 else { 069 ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR, 070 toString(), type.toString()); 071 classGen.getParser().reportError(Constants.FATAL, err); 072 } 073 } 074 075 /** 076 * Translates a void into a string by pushing the empty string ''. 077 * 078 * @see org.apache.xalan.xsltc.compiler.util.Type#translateTo 079 */ 080 public void translateTo(ClassGenerator classGen, MethodGenerator methodGen, 081 StringType type) { 082 final InstructionList il = methodGen.getInstructionList(); 083 il.append(new PUSH(classGen.getConstantPool(), "")); 084 } 085 086 /** 087 * Translates an external (primitive) Java type into a void. 088 * Only an external "void" can be converted to this class. 089 */ 090 public void translateFrom(ClassGenerator classGen, MethodGenerator methodGen, 091 Class clazz) { 092 if (!clazz.getName().equals("void")) { 093 ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR, 094 toString(), clazz.getName()); 095 classGen.getParser().reportError(Constants.FATAL, err); 096 } 097 } 098 }