use of org.ballerinalang.util.exceptions.ProgramFileFormatException in project ballerina by ballerina-lang.
the class ProgramFileReader method readInstructions.
private void readInstructions(DataInputStream dataInStream, PackageInfo packageInfo) throws IOException {
int codeLength = dataInStream.readInt();
byte[] code = new byte[codeLength];
// Ignore bytes read should be same as the code length.
dataInStream.read(code);
DataInputStream codeStream = new DataInputStream(new ByteArrayInputStream(code));
while (codeStream.available() > 0) {
int i, j, k, h;
int funcRefCPIndex;
FunctionRefCPEntry funcRefCPEntry;
int flags;
int[] argRegs;
int[] retRegs;
int opcode = codeStream.readUnsignedByte();
switch(opcode) {
case InstructionCodes.HALT:
case InstructionCodes.RET:
packageInfo.addInstruction(InstructionFactory.get(opcode));
break;
case InstructionCodes.ICONST_0:
case InstructionCodes.ICONST_1:
case InstructionCodes.ICONST_2:
case InstructionCodes.ICONST_3:
case InstructionCodes.ICONST_4:
case InstructionCodes.ICONST_5:
case InstructionCodes.FCONST_0:
case InstructionCodes.FCONST_1:
case InstructionCodes.FCONST_2:
case InstructionCodes.FCONST_3:
case InstructionCodes.FCONST_4:
case InstructionCodes.FCONST_5:
case InstructionCodes.BCONST_0:
case InstructionCodes.BCONST_1:
case InstructionCodes.RCONST_NULL:
case InstructionCodes.GOTO:
case InstructionCodes.THROW:
case InstructionCodes.ERRSTORE:
case InstructionCodes.NEWXMLSEQ:
i = codeStream.readInt();
packageInfo.addInstruction(InstructionFactory.get(opcode, i));
break;
case InstructionCodes.ICONST:
case InstructionCodes.FCONST:
case InstructionCodes.SCONST:
case InstructionCodes.IMOVE:
case InstructionCodes.FMOVE:
case InstructionCodes.SMOVE:
case InstructionCodes.BMOVE:
case InstructionCodes.LMOVE:
case InstructionCodes.RMOVE:
case InstructionCodes.IGLOAD:
case InstructionCodes.FGLOAD:
case InstructionCodes.SGLOAD:
case InstructionCodes.BGLOAD:
case InstructionCodes.LGLOAD:
case InstructionCodes.RGLOAD:
case InstructionCodes.IGSTORE:
case InstructionCodes.FGSTORE:
case InstructionCodes.SGSTORE:
case InstructionCodes.BGSTORE:
case InstructionCodes.LGSTORE:
case InstructionCodes.RGSTORE:
case InstructionCodes.INEG:
case InstructionCodes.FNEG:
case InstructionCodes.BNOT:
case InstructionCodes.REQ_NULL:
case InstructionCodes.RNE_NULL:
case InstructionCodes.BR_TRUE:
case InstructionCodes.BR_FALSE:
case InstructionCodes.TR_END:
case InstructionCodes.FPLOAD:
case InstructionCodes.ARRAYLEN:
case InstructionCodes.INEWARRAY:
case InstructionCodes.FNEWARRAY:
case InstructionCodes.SNEWARRAY:
case InstructionCodes.BNEWARRAY:
case InstructionCodes.LNEWARRAY:
case InstructionCodes.RNEWARRAY:
case InstructionCodes.JSONNEWARRAY:
case InstructionCodes.NEWSTRUCT:
case InstructionCodes.NEWCONNECTOR:
case InstructionCodes.ITR_NEW:
case InstructionCodes.ITR_HAS_NEXT:
case InstructionCodes.IRET:
case InstructionCodes.FRET:
case InstructionCodes.SRET:
case InstructionCodes.BRET:
case InstructionCodes.LRET:
case InstructionCodes.RRET:
case InstructionCodes.XML2XMLATTRS:
case InstructionCodes.NEWXMLCOMMENT:
case InstructionCodes.NEWXMLTEXT:
case InstructionCodes.XMLSEQSTORE:
case InstructionCodes.TYPEOF:
case InstructionCodes.TYPELOAD:
case InstructionCodes.SEQ_NULL:
case InstructionCodes.SNE_NULL:
case InstructionCodes.NEWJSON:
case InstructionCodes.NEWMAP:
case InstructionCodes.NEWTABLE:
case InstructionCodes.I2ANY:
case InstructionCodes.F2ANY:
case InstructionCodes.S2ANY:
case InstructionCodes.B2ANY:
case InstructionCodes.L2ANY:
case InstructionCodes.ANY2I:
case InstructionCodes.ANY2F:
case InstructionCodes.ANY2S:
case InstructionCodes.ANY2B:
case InstructionCodes.ANY2L:
case InstructionCodes.ANY2JSON:
case InstructionCodes.ANY2XML:
case InstructionCodes.ANY2MAP:
case InstructionCodes.ANY2TYPE:
case InstructionCodes.ANY2DT:
case InstructionCodes.NULL2JSON:
case InstructionCodes.I2F:
case InstructionCodes.I2S:
case InstructionCodes.I2B:
case InstructionCodes.I2JSON:
case InstructionCodes.F2I:
case InstructionCodes.F2S:
case InstructionCodes.F2B:
case InstructionCodes.F2JSON:
case InstructionCodes.S2I:
case InstructionCodes.S2F:
case InstructionCodes.S2B:
case InstructionCodes.S2JSON:
case InstructionCodes.B2I:
case InstructionCodes.B2F:
case InstructionCodes.B2S:
case InstructionCodes.B2JSON:
case InstructionCodes.JSON2I:
case InstructionCodes.JSON2F:
case InstructionCodes.JSON2S:
case InstructionCodes.JSON2B:
case InstructionCodes.DT2XML:
case InstructionCodes.DT2JSON:
case InstructionCodes.T2MAP:
case InstructionCodes.T2JSON:
case InstructionCodes.XML2JSON:
case InstructionCodes.JSON2XML:
case InstructionCodes.XMLATTRS2MAP:
case InstructionCodes.ANY2SCONV:
case InstructionCodes.S2XML:
case InstructionCodes.XML2S:
case InstructionCodes.S2JSONX:
case InstructionCodes.NULL2S:
case InstructionCodes.AWAIT:
case InstructionCodes.CHECK_CONVERSION:
case InstructionCodes.XMLLOADALL:
i = codeStream.readInt();
j = codeStream.readInt();
packageInfo.addInstruction(InstructionFactory.get(opcode, i, j));
break;
case InstructionCodes.IALOAD:
case InstructionCodes.FALOAD:
case InstructionCodes.SALOAD:
case InstructionCodes.BALOAD:
case InstructionCodes.LALOAD:
case InstructionCodes.RALOAD:
case InstructionCodes.JSONALOAD:
case InstructionCodes.IASTORE:
case InstructionCodes.FASTORE:
case InstructionCodes.SASTORE:
case InstructionCodes.BASTORE:
case InstructionCodes.LASTORE:
case InstructionCodes.RASTORE:
case InstructionCodes.JSONASTORE:
case InstructionCodes.IFIELDLOAD:
case InstructionCodes.FFIELDLOAD:
case InstructionCodes.SFIELDLOAD:
case InstructionCodes.BFIELDLOAD:
case InstructionCodes.LFIELDLOAD:
case InstructionCodes.RFIELDLOAD:
case InstructionCodes.IFIELDSTORE:
case InstructionCodes.FFIELDSTORE:
case InstructionCodes.SFIELDSTORE:
case InstructionCodes.BFIELDSTORE:
case InstructionCodes.LFIELDSTORE:
case InstructionCodes.RFIELDSTORE:
case InstructionCodes.MAPLOAD:
case InstructionCodes.MAPSTORE:
case InstructionCodes.JSONLOAD:
case InstructionCodes.JSONSTORE:
case InstructionCodes.ENUMERATORLOAD:
case InstructionCodes.IADD:
case InstructionCodes.FADD:
case InstructionCodes.SADD:
case InstructionCodes.XMLADD:
case InstructionCodes.ISUB:
case InstructionCodes.FSUB:
case InstructionCodes.IMUL:
case InstructionCodes.FMUL:
case InstructionCodes.IDIV:
case InstructionCodes.FDIV:
case InstructionCodes.IMOD:
case InstructionCodes.FMOD:
case InstructionCodes.IEQ:
case InstructionCodes.FEQ:
case InstructionCodes.SEQ:
case InstructionCodes.BEQ:
case InstructionCodes.REQ:
case InstructionCodes.INE:
case InstructionCodes.FNE:
case InstructionCodes.SNE:
case InstructionCodes.BNE:
case InstructionCodes.RNE:
case InstructionCodes.IGT:
case InstructionCodes.FGT:
case InstructionCodes.IGE:
case InstructionCodes.FGE:
case InstructionCodes.ILT:
case InstructionCodes.FLT:
case InstructionCodes.ILE:
case InstructionCodes.FLE:
case InstructionCodes.XMLATTRLOAD:
case InstructionCodes.XMLATTRSTORE:
case InstructionCodes.S2QNAME:
case InstructionCodes.NEWXMLPI:
case InstructionCodes.TEQ:
case InstructionCodes.TNE:
case InstructionCodes.XMLLOAD:
case InstructionCodes.NEW_INT_RANGE:
case InstructionCodes.LENGTHOF:
case InstructionCodes.NEWSTREAM:
case InstructionCodes.CHECKCAST:
case InstructionCodes.MAP2T:
case InstructionCodes.JSON2T:
case InstructionCodes.ANY2T:
case InstructionCodes.ANY2C:
case InstructionCodes.ANY2E:
case InstructionCodes.IS_ASSIGNABLE:
case InstructionCodes.TR_RETRY:
case InstructionCodes.XMLSEQLOAD:
i = codeStream.readInt();
j = codeStream.readInt();
k = codeStream.readInt();
packageInfo.addInstruction(InstructionFactory.get(opcode, i, j, k));
break;
case InstructionCodes.NEWQNAME:
case InstructionCodes.NEWXMLELEMENT:
case InstructionCodes.TR_BEGIN:
i = codeStream.readInt();
j = codeStream.readInt();
k = codeStream.readInt();
h = codeStream.readInt();
packageInfo.addInstruction(InstructionFactory.get(opcode, i, j, k, h));
break;
case InstructionCodes.CALL:
funcRefCPIndex = codeStream.readInt();
flags = codeStream.readInt();
funcRefCPEntry = (FunctionRefCPEntry) packageInfo.getCPEntry(funcRefCPIndex);
packageInfo.addInstruction(new InstructionCALL(opcode, funcRefCPIndex, funcRefCPEntry.getFunctionInfo(), flags, getArgRegs(codeStream), getArgRegs(codeStream)));
break;
case InstructionCodes.VCALL:
int receiverRegIndex = codeStream.readInt();
funcRefCPIndex = codeStream.readInt();
flags = codeStream.readInt();
funcRefCPEntry = (FunctionRefCPEntry) packageInfo.getCPEntry(funcRefCPIndex);
packageInfo.addInstruction(new InstructionVCALL(opcode, receiverRegIndex, funcRefCPIndex, funcRefCPEntry.getFunctionInfo(), flags, getArgRegs(codeStream), getArgRegs(codeStream)));
break;
case InstructionCodes.ACALL:
int actionRefCPIndex = codeStream.readInt();
flags = codeStream.readInt();
ActionRefCPEntry actionRefCPEntry = (ActionRefCPEntry) packageInfo.getCPEntry(actionRefCPIndex);
packageInfo.addInstruction(new InstructionACALL(opcode, actionRefCPIndex, actionRefCPEntry.getActionName(), flags, getArgRegs(codeStream), getArgRegs(codeStream)));
break;
case InstructionCodes.FPCALL:
funcRefCPIndex = codeStream.readInt();
flags = codeStream.readInt();
argRegs = getArgRegs(codeStream);
retRegs = getArgRegs(codeStream);
FunctionCallCPEntry funcCallCPEntry = new FunctionCallCPEntry(flags, argRegs, retRegs);
int funcCallCPIndex = packageInfo.addCPEntry(funcCallCPEntry);
packageInfo.addInstruction(InstructionFactory.get(opcode, funcRefCPIndex, funcCallCPIndex));
break;
case InstructionCodes.TCALL:
int transformCPIndex = codeStream.readInt();
flags = codeStream.readInt();
TransformerRefCPEntry transformerRefCPEntry = (TransformerRefCPEntry) packageInfo.getCPEntry(transformCPIndex);
packageInfo.addInstruction(new InstructionTCALL(opcode, transformCPIndex, transformerRefCPEntry.getTransformerInfo(), flags, getArgRegs(codeStream), getArgRegs(codeStream)));
break;
case InstructionCodes.WRKSEND:
case InstructionCodes.WRKRECEIVE:
int channelRefCPIndex = codeStream.readInt();
WorkerDataChannelRefCPEntry channelRefCPEntry = (WorkerDataChannelRefCPEntry) packageInfo.getCPEntry(channelRefCPIndex);
int sigCPIndex = codeStream.readInt();
UTF8CPEntry sigCPEntry = (UTF8CPEntry) packageInfo.getCPEntry(sigCPIndex);
BType[] bTypes = getParamTypes(sigCPEntry.getValue(), packageInfo);
packageInfo.addInstruction(new InstructionWRKSendReceive(opcode, channelRefCPIndex, channelRefCPEntry.getWorkerDataChannelInfo(), sigCPIndex, bTypes, getArgRegs(codeStream)));
break;
case InstructionCodes.FORKJOIN:
int forkJoinIndexCPIndex = codeStream.readInt();
ForkJoinCPEntry forkJoinIndexCPEntry = (ForkJoinCPEntry) packageInfo.getCPEntry(forkJoinIndexCPIndex);
int timeoutRegIndex = codeStream.readInt();
int joinVarRegIndex = codeStream.readInt();
int joinBlockAddr = codeStream.readInt();
int timeoutVarRegIndex = codeStream.readInt();
int timeoutBlockAddr = codeStream.readInt();
packageInfo.addInstruction(new InstructionFORKJOIN(opcode, forkJoinIndexCPIndex, forkJoinIndexCPEntry, timeoutRegIndex, joinVarRegIndex, joinBlockAddr, timeoutVarRegIndex, timeoutBlockAddr));
break;
case InstructionCodes.ITR_NEXT:
int iteratorIndex = codeStream.readInt();
int[] typeTags = getArgRegs(codeStream);
retRegs = getArgRegs(codeStream);
packageInfo.addInstruction(new InstructionIteratorNext(opcode, iteratorIndex, retRegs.length, typeTags, retRegs));
break;
case InstructionCodes.LOCK:
case InstructionCodes.UNLOCK:
int varCount = codeStream.readInt();
BType[] varTypes = new BType[varCount];
int[] varRegs = new int[varCount];
for (int m = 0; m < varCount; m++) {
int varSigCPIndex = codeStream.readInt();
TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) packageInfo.getCPEntry(varSigCPIndex);
varTypes[m] = typeRefCPEntry.getType();
varRegs[m] = codeStream.readInt();
}
packageInfo.addInstruction(new InstructionLock(opcode, varTypes, varRegs));
break;
default:
throw new ProgramFileFormatException("unknown opcode " + opcode + " in package " + packageInfo.getPkgPath());
}
}
}
use of org.ballerinalang.util.exceptions.ProgramFileFormatException in project ballerina by ballerina-lang.
the class ProgramFileReader method getDefaultValue.
private DefaultValue getDefaultValue(DataInputStream dataInStream, ConstantPool constantPool) throws IOException {
DefaultValue defaultValue;
int typeDescCPIndex = dataInStream.readInt();
UTF8CPEntry typeDescCPEntry = (UTF8CPEntry) constantPool.getCPEntry(typeDescCPIndex);
String typeDesc = typeDescCPEntry.getValue();
int valueCPIndex;
switch(typeDesc) {
case TypeSignature.SIG_BOOLEAN:
boolean boolValue = dataInStream.readBoolean();
defaultValue = new DefaultValue(typeDescCPIndex, typeDesc);
defaultValue.setBooleanValue(boolValue);
break;
case TypeSignature.SIG_INT:
valueCPIndex = dataInStream.readInt();
IntegerCPEntry integerCPEntry = (IntegerCPEntry) constantPool.getCPEntry(valueCPIndex);
defaultValue = new DefaultValue(typeDescCPIndex, typeDesc);
defaultValue.setIntValue(integerCPEntry.getValue());
break;
case TypeSignature.SIG_FLOAT:
valueCPIndex = dataInStream.readInt();
FloatCPEntry floatCPEntry = (FloatCPEntry) constantPool.getCPEntry(valueCPIndex);
defaultValue = new DefaultValue(typeDescCPIndex, typeDesc);
defaultValue.setFloatValue(floatCPEntry.getValue());
break;
case TypeSignature.SIG_STRING:
valueCPIndex = dataInStream.readInt();
UTF8CPEntry stringCPEntry = (UTF8CPEntry) constantPool.getCPEntry(valueCPIndex);
defaultValue = new DefaultValue(typeDescCPIndex, typeDesc);
defaultValue.setStringValue(stringCPEntry.getValue());
break;
default:
throw new ProgramFileFormatException("unknown default value type " + typeDesc);
}
return defaultValue;
}
use of org.ballerinalang.util.exceptions.ProgramFileFormatException in project ballerina by ballerina-lang.
the class ProgramFileReader method getAttributeInfo.
private AttributeInfo getAttributeInfo(DataInputStream dataInStream, ConstantPool constantPool) throws IOException {
int attribNameCPIndex = dataInStream.readInt();
UTF8CPEntry attribNameCPEntry = (UTF8CPEntry) constantPool.getCPEntry(attribNameCPIndex);
AttributeInfo.Kind attribKind = AttributeInfo.Kind.fromString(attribNameCPEntry.getValue());
if (attribKind == null) {
throw new ProgramFileFormatException("unknown attribute kind " + attribNameCPEntry.getValue());
}
switch(attribKind) {
case CODE_ATTRIBUTE:
CodeAttributeInfo codeAttributeInfo = new CodeAttributeInfo();
codeAttributeInfo.setAttributeNameIndex(attribNameCPIndex);
codeAttributeInfo.setCodeAddrs(dataInStream.readInt());
codeAttributeInfo.setMaxLongLocalVars(dataInStream.readUnsignedShort());
codeAttributeInfo.setMaxDoubleLocalVars(dataInStream.readShort());
codeAttributeInfo.setMaxStringLocalVars(dataInStream.readShort());
codeAttributeInfo.setMaxIntLocalVars(dataInStream.readShort());
codeAttributeInfo.setMaxByteLocalVars(dataInStream.readShort());
codeAttributeInfo.setMaxRefLocalVars(dataInStream.readShort());
codeAttributeInfo.setMaxLongRegs(dataInStream.readShort());
codeAttributeInfo.setMaxDoubleRegs(dataInStream.readShort());
codeAttributeInfo.setMaxStringRegs(dataInStream.readShort());
codeAttributeInfo.setMaxIntRegs(dataInStream.readShort());
codeAttributeInfo.setMaxByteRegs(dataInStream.readShort());
codeAttributeInfo.setMaxRefRegs(dataInStream.readShort());
return codeAttributeInfo;
case VARIABLE_TYPE_COUNT_ATTRIBUTE:
VarTypeCountAttributeInfo varCountAttributeInfo = new VarTypeCountAttributeInfo(attribNameCPIndex);
varCountAttributeInfo.setMaxLongVars(dataInStream.readShort());
varCountAttributeInfo.setMaxDoubleVars(dataInStream.readShort());
varCountAttributeInfo.setMaxStringVars(dataInStream.readShort());
varCountAttributeInfo.setMaxIntVars(dataInStream.readShort());
varCountAttributeInfo.setMaxByteVars(dataInStream.readShort());
varCountAttributeInfo.setMaxRefVars(dataInStream.readShort());
return varCountAttributeInfo;
case ERROR_TABLE:
ErrorTableAttributeInfo tableAttributeInfo = new ErrorTableAttributeInfo(attribNameCPIndex);
int tableEntryCount = dataInStream.readShort();
for (int i = 0; i < tableEntryCount; i++) {
int ipFrom = dataInStream.readInt();
int ipTo = dataInStream.readInt();
int ipTarget = dataInStream.readInt();
int priority = dataInStream.readInt();
int errorStructCPIndex = dataInStream.readInt();
ErrorTableEntry tableEntry = new ErrorTableEntry(ipFrom, ipTo, ipTarget, priority, errorStructCPIndex);
if (errorStructCPIndex != -1) {
StructureRefCPEntry structureRefCPEntry = (StructureRefCPEntry) constantPool.getCPEntry(errorStructCPIndex);
tableEntry.setError((StructInfo) structureRefCPEntry.getStructureTypeInfo());
}
tableAttributeInfo.addErrorTableEntry(tableEntry);
}
return tableAttributeInfo;
case LOCAL_VARIABLES_ATTRIBUTE:
LocalVariableAttributeInfo localVarAttrInfo = new LocalVariableAttributeInfo(attribNameCPIndex);
int localVarInfoCount = dataInStream.readShort();
for (int i = 0; i < localVarInfoCount; i++) {
LocalVariableInfo localVariableInfo = getLocalVariableInfo(dataInStream, constantPool);
localVarAttrInfo.addLocalVarInfo(localVariableInfo);
}
return localVarAttrInfo;
case LINE_NUMBER_TABLE_ATTRIBUTE:
LineNumberTableAttributeInfo lnNoTblAttrInfo = new LineNumberTableAttributeInfo(attribNameCPIndex);
int lineNoInfoCount = dataInStream.readShort();
for (int i = 0; i < lineNoInfoCount; i++) {
LineNumberInfo lineNumberInfo = getLineNumberInfo(dataInStream, constantPool);
lnNoTblAttrInfo.addLineNumberInfo(lineNumberInfo);
}
return lnNoTblAttrInfo;
case DEFAULT_VALUE_ATTRIBUTE:
DefaultValue defaultValue = getDefaultValue(dataInStream, constantPool);
DefaultValueAttributeInfo defaultValAttrInfo = new DefaultValueAttributeInfo(attribNameCPIndex, defaultValue);
return defaultValAttrInfo;
case PARAMETER_DEFAULTS_ATTRIBUTE:
ParamDefaultValueAttributeInfo paramDefaultValAttrInfo = new ParamDefaultValueAttributeInfo(attribNameCPIndex);
int paramDefaultsInfoCount = dataInStream.readShort();
for (int i = 0; i < paramDefaultsInfoCount; i++) {
DefaultValue paramDefaultValue = getDefaultValue(dataInStream, constantPool);
paramDefaultValAttrInfo.addParamDefaultValueInfo(paramDefaultValue);
}
return paramDefaultValAttrInfo;
default:
throw new ProgramFileFormatException("unsupported attribute kind " + attribNameCPEntry.getValue());
}
}
use of org.ballerinalang.util.exceptions.ProgramFileFormatException in project ballerina by ballerina-lang.
the class ProgramFileReader method createBTypeFromSig.
private int createBTypeFromSig(char[] chars, int index, Stack<BType> typeStack, PackageInfo packageInfo) {
int nameIndex;
char ch = chars[index];
switch(ch) {
case 'I':
typeStack.push(BTypes.typeInt);
return index + 1;
case 'F':
typeStack.push(BTypes.typeFloat);
return index + 1;
case 'S':
typeStack.push(BTypes.typeString);
return index + 1;
case 'B':
typeStack.push(BTypes.typeBoolean);
return index + 1;
case 'L':
typeStack.push(BTypes.typeBlob);
return index + 1;
case 'Y':
typeStack.push(BTypes.typeDesc);
return index + 1;
case 'A':
typeStack.push(BTypes.typeAny);
return index + 1;
case 'R':
// TODO Improve this logic
index++;
nameIndex = index;
while (chars[nameIndex] != ';') {
nameIndex++;
}
String typeName = new String(Arrays.copyOfRange(chars, index, nameIndex));
typeStack.push(BTypes.getTypeFromName(typeName));
return nameIndex + 1;
case 'C':
case 'J':
case 'T':
case 'E':
case 'D':
case 'H':
case 'Z':
char typeChar = chars[index];
// TODO Improve this logic
index++;
nameIndex = index;
int colonIndex = -1;
while (chars[nameIndex] != ';') {
if (chars[nameIndex] == ':') {
colonIndex = nameIndex;
}
nameIndex++;
}
String pkgPath;
String name;
PackageInfo packageInfoOfType;
if (colonIndex != -1) {
pkgPath = new String(Arrays.copyOfRange(chars, index, colonIndex));
name = new String(Arrays.copyOfRange(chars, colonIndex + 1, nameIndex));
packageInfoOfType = programFile.getPackageInfo(pkgPath);
} else {
name = new String(Arrays.copyOfRange(chars, index, nameIndex));
// Setting the current package;
packageInfoOfType = packageInfo;
}
if (typeChar == 'C') {
typeStack.push(packageInfoOfType.getConnectorInfo(name).getType());
} else if (typeChar == 'J') {
if (name.isEmpty()) {
typeStack.push(BTypes.typeJSON);
} else {
typeStack.push(new BJSONType(packageInfoOfType.getStructInfo(name).getType()));
}
} else if (typeChar == 'D') {
if (name.isEmpty()) {
typeStack.push(BTypes.typeTable);
} else {
typeStack.push(new BTableType(packageInfoOfType.getStructInfo(name).getType()));
}
} else if (typeChar == 'H') {
if (name.isEmpty()) {
typeStack.push(BTypes.typeStream);
} else {
typeStack.push(new BStreamType(packageInfoOfType.getStructInfo(name).getType()));
}
} else if (typeChar == 'E') {
typeStack.push(packageInfoOfType.getEnumInfo(name).getType());
} else {
// This is a struct type
typeStack.push(packageInfoOfType.getStructInfo(name).getType());
}
return nameIndex + 1;
case '[':
index = createBTypeFromSig(chars, index + 1, typeStack, packageInfo);
BType elemType = typeStack.pop();
BArrayType arrayType = new BArrayType(elemType);
typeStack.push(arrayType);
return index;
case 'M':
index = createBTypeFromSig(chars, index + 1, typeStack, packageInfo);
BType constrainedType = typeStack.pop();
BType mapType;
if (constrainedType == BTypes.typeAny) {
mapType = BTypes.typeMap;
} else {
mapType = new BMapType(constrainedType);
}
typeStack.push(mapType);
return index;
case 'U':
// TODO : Fix this for type casting.
typeStack.push(new BFunctionType());
return index + 1;
case 'O':
case 'P':
typeChar = chars[index];
index++;
nameIndex = index;
while (chars[nameIndex] != ';') {
nameIndex++;
}
List<BType> memberTypes = new ArrayList<>();
int memberCount = Integer.parseInt(new String(Arrays.copyOfRange(chars, index, nameIndex)));
index = nameIndex;
for (int i = 0; i < memberCount; i++) {
index = createBTypeFromSig(chars, index + 1, typeStack, packageInfo) - 1;
memberTypes.add(typeStack.pop());
}
if (typeChar == 'O') {
typeStack.push(new BUnionType(memberTypes));
} else if (typeChar == 'P') {
typeStack.push(new BTupleType(memberTypes));
}
return index + 1;
case 'N':
typeStack.push(BTypes.typeNull);
return index + 1;
default:
throw new ProgramFileFormatException("unsupported base type char: " + ch);
}
}
use of org.ballerinalang.util.exceptions.ProgramFileFormatException in project ballerina by ballerina-lang.
the class ProgramFileReader method readCPEntry.
private ConstantPoolEntry readCPEntry(DataInputStream dataInStream, ConstantPool constantPool, ConstantPoolEntry.EntryType cpEntryType) throws IOException {
int cpIndex;
int pkgCPIndex;
UTF8CPEntry utf8CPEntry;
PackageRefCPEntry packageRefCPEntry;
Optional<PackageInfo> packageInfoOptional;
switch(cpEntryType) {
case CP_ENTRY_UTF8:
short length = dataInStream.readShort();
String strValue = null;
// Therefore we read the UTF value only if the length >= 0.
if (length >= 0) {
strValue = dataInStream.readUTF();
}
return new UTF8CPEntry(strValue);
case CP_ENTRY_INTEGER:
long longVal = dataInStream.readLong();
return new IntegerCPEntry(longVal);
case CP_ENTRY_FLOAT:
double doubleVal = dataInStream.readDouble();
return new FloatCPEntry(doubleVal);
case CP_ENTRY_STRING:
cpIndex = dataInStream.readInt();
utf8CPEntry = (UTF8CPEntry) constantPool.getCPEntry(cpIndex);
return new StringCPEntry(cpIndex, utf8CPEntry.getValue());
case CP_ENTRY_PACKAGE:
cpIndex = dataInStream.readInt();
utf8CPEntry = (UTF8CPEntry) constantPool.getCPEntry(cpIndex);
return new PackageRefCPEntry(cpIndex, utf8CPEntry.getValue());
case CP_ENTRY_FUNCTION_REF:
pkgCPIndex = dataInStream.readInt();
packageRefCPEntry = (PackageRefCPEntry) constantPool.getCPEntry(pkgCPIndex);
cpIndex = dataInStream.readInt();
utf8CPEntry = (UTF8CPEntry) constantPool.getCPEntry(cpIndex);
String funcName = utf8CPEntry.getValue();
FunctionRefCPEntry functionRefCPEntry = new FunctionRefCPEntry(pkgCPIndex, packageRefCPEntry.getPackageName(), cpIndex, funcName);
// Find the functionInfo
packageInfoOptional = Optional.ofNullable(programFile.getPackageInfo(packageRefCPEntry.getPackageName()));
Optional<FunctionInfo> funcInfoOptional = packageInfoOptional.map(packageInfo -> packageInfo.getFunctionInfo(funcName));
if (!funcInfoOptional.isPresent()) {
// This must reference to the current package and the current package is not been read yet.
// Therefore we add this to the unresolved CP Entry list.
unresolvedCPEntries.add(functionRefCPEntry);
return functionRefCPEntry;
}
functionRefCPEntry.setFunctionInfo(funcInfoOptional.get());
return functionRefCPEntry;
case CP_ENTRY_TRANSFORMER_REF:
pkgCPIndex = dataInStream.readInt();
packageRefCPEntry = (PackageRefCPEntry) constantPool.getCPEntry(pkgCPIndex);
cpIndex = dataInStream.readInt();
utf8CPEntry = (UTF8CPEntry) constantPool.getCPEntry(cpIndex);
String transformerName = utf8CPEntry.getValue();
TransformerRefCPEntry transformerRefCPEntry = new TransformerRefCPEntry(pkgCPIndex, packageRefCPEntry.getPackageName(), cpIndex, transformerName);
// Find the transformerInfo
packageInfoOptional = Optional.ofNullable(programFile.getPackageInfo(packageRefCPEntry.getPackageName()));
Optional<TransformerInfo> transInfoOptional = packageInfoOptional.map(packageInfo -> packageInfo.getTransformerInfo(transformerName));
if (!transInfoOptional.isPresent()) {
// This must reference to the current package and the current package is not been read yet.
// Therefore we add this to the unresolved CP Entry list.
unresolvedCPEntries.add(transformerRefCPEntry);
return transformerRefCPEntry;
}
transformerRefCPEntry.setTransformerInfo(transInfoOptional.get());
return transformerRefCPEntry;
case CP_ENTRY_ACTION_REF:
pkgCPIndex = dataInStream.readInt();
packageRefCPEntry = (PackageRefCPEntry) constantPool.getCPEntry(pkgCPIndex);
cpIndex = dataInStream.readInt();
UTF8CPEntry nameCPEntry = (UTF8CPEntry) constantPool.getCPEntry(cpIndex);
String actionName = nameCPEntry.getValue();
return new ActionRefCPEntry(pkgCPIndex, packageRefCPEntry.getPackageName(), cpIndex, actionName);
case CP_ENTRY_STRUCTURE_REF:
pkgCPIndex = dataInStream.readInt();
packageRefCPEntry = (PackageRefCPEntry) constantPool.getCPEntry(pkgCPIndex);
cpIndex = dataInStream.readInt();
utf8CPEntry = (UTF8CPEntry) constantPool.getCPEntry(cpIndex);
StructureRefCPEntry structureRefCPEntry = new StructureRefCPEntry(pkgCPIndex, packageRefCPEntry.getPackageName(), cpIndex, utf8CPEntry.getValue());
packageInfoOptional = Optional.ofNullable(programFile.getPackageInfo(packageRefCPEntry.getPackageName()));
Optional<StructureTypeInfo> structInfoOptional = packageInfoOptional.map(packageInfo -> packageInfo.getStructureTypeInfo(utf8CPEntry.getValue()));
if (!structInfoOptional.isPresent()) {
// This must reference to the current package and the current package is not been read yet.
// Therefore we add this to the unresolved CP Entry list.
unresolvedCPEntries.add(structureRefCPEntry);
return structureRefCPEntry;
}
structureRefCPEntry.setStructureTypeInfo(structInfoOptional.get());
return structureRefCPEntry;
case CP_ENTRY_TYPE_REF:
int typeSigCPIndex = dataInStream.readInt();
utf8CPEntry = (UTF8CPEntry) constantPool.getCPEntry(typeSigCPIndex);
TypeRefCPEntry typeRefCPEntry = new TypeRefCPEntry(typeSigCPIndex, utf8CPEntry.getValue());
unresolvedCPEntries.add(typeRefCPEntry);
return typeRefCPEntry;
case CP_ENTRY_FORK_JOIN:
int forkJoinCPIndex = dataInStream.readInt();
return new ForkJoinCPEntry(forkJoinCPIndex);
case CP_ENTRY_WRKR_DATA_CHNL_REF:
int uniqueNameCPIndex = dataInStream.readInt();
UTF8CPEntry wrkrDtChnlTypesSigCPEntry = (UTF8CPEntry) constantPool.getCPEntry(uniqueNameCPIndex);
return new WorkerDataChannelRefCPEntry(uniqueNameCPIndex, wrkrDtChnlTypesSigCPEntry.getValue());
default:
throw new ProgramFileFormatException("invalid constant pool entry " + cpEntryType.getValue());
}
}
Aggregations