/* case_label = {constant} case minus? integer_constant | {default} default; */ public void outAConstantCaseLabel(AConstantCaseLabel node) { String s = (String) mProductions.removeLast(); int sign = 1; if (node.getMinus() != null) sign = -1; if (s.endsWith("L")) { mProductions.addLast(LongConstant.v(sign * Long.parseLong(s.substring(0, s.length() - 1)))); } else mProductions.addLast(IntConstant.v(sign * Integer.parseInt(s))); }
public void outAIntegerConstant(AIntegerConstant node) { String s = (String) mProductions.removeLast(); StringBuffer buf = new StringBuffer(); if (node.getMinus() != null) buf.append('-'); buf.append(s); s = buf.toString(); if (s.endsWith("L")) { mProductions.addLast(LongConstant.v(Long.parseLong(s.substring(0, s.length() - 1)))); } else mProductions.addLast(IntConstant.v(Integer.parseInt(s))); }
Object decodeDefaultValue() { Object value = null; // u1 tag; int tag = u1At(this.readOffset); this.readOffset++; int constValueOffset = -1; switch (tag) { case 'Z': // boolean constant constValueOffset = this.constantPoolOffsets[u2At(this.readOffset)] - this.structOffset; value = BooleanConstant.fromValue(i4At(constValueOffset + 1) == 1); this.readOffset += 2; break; case 'I': // integer constant constValueOffset = this.constantPoolOffsets[u2At(this.readOffset)] - this.structOffset; value = IntConstant.fromValue(i4At(constValueOffset + 1)); this.readOffset += 2; break; case 'C': // char constant constValueOffset = this.constantPoolOffsets[u2At(this.readOffset)] - this.structOffset; value = CharConstant.fromValue((char) i4At(constValueOffset + 1)); this.readOffset += 2; break; case 'B': // byte constant constValueOffset = this.constantPoolOffsets[u2At(this.readOffset)] - this.structOffset; value = ByteConstant.fromValue((byte) i4At(constValueOffset + 1)); this.readOffset += 2; break; case 'S': // short constant constValueOffset = this.constantPoolOffsets[u2At(this.readOffset)] - this.structOffset; value = ShortConstant.fromValue((short) i4At(constValueOffset + 1)); this.readOffset += 2; break; case 'D': // double constant constValueOffset = this.constantPoolOffsets[u2At(this.readOffset)] - this.structOffset; value = DoubleConstant.fromValue(doubleAt(constValueOffset + 1)); this.readOffset += 2; break; case 'F': // float constant constValueOffset = this.constantPoolOffsets[u2At(this.readOffset)] - this.structOffset; value = FloatConstant.fromValue(floatAt(constValueOffset + 1)); this.readOffset += 2; break; case 'J': // long constant constValueOffset = this.constantPoolOffsets[u2At(this.readOffset)] - this.structOffset; value = LongConstant.fromValue(i8At(constValueOffset + 1)); this.readOffset += 2; break; case 's': // String constValueOffset = this.constantPoolOffsets[u2At(this.readOffset)] - this.structOffset; value = StringConstant.fromValue( String.valueOf(utf8At(constValueOffset + 3, u2At(constValueOffset + 1)))); this.readOffset += 2; break; case 'e': constValueOffset = this.constantPoolOffsets[u2At(this.readOffset)] - this.structOffset; char[] typeName = utf8At(constValueOffset + 3, u2At(constValueOffset + 1)); this.readOffset += 2; constValueOffset = this.constantPoolOffsets[u2At(this.readOffset)] - this.structOffset; char[] constName = utf8At(constValueOffset + 3, u2At(constValueOffset + 1)); this.readOffset += 2; value = new EnumConstantSignature(typeName, constName); break; case 'c': constValueOffset = this.constantPoolOffsets[u2At(this.readOffset)] - this.structOffset; char[] className = utf8At(constValueOffset + 3, u2At(constValueOffset + 1)); value = new ClassSignature(className); this.readOffset += 2; break; case '@': value = new AnnotationInfo( this.reference, this.constantPoolOffsets, this.readOffset + this.structOffset, false, true); this.readOffset += ((AnnotationInfo) value).readOffset; break; case '[': int numberOfValues = u2At(this.readOffset); this.readOffset += 2; if (numberOfValues == 0) { value = EmptyValueArray; } else { Object[] arrayElements = new Object[numberOfValues]; value = arrayElements; for (int i = 0; i < numberOfValues; i++) arrayElements[i] = decodeDefaultValue(); } break; default: throw new IllegalStateException("Unrecognized tag " + (char) tag); // $NON-NLS-1$ } return value; }
/** * Returns a fixed value from any intercepted method. The fixed value is stored in the constant * pool if this is possible. Java is capable of storing any primitive value, {@link String} values * and {@link Class} references in the constant pool. Since Java 7, {@code MethodHandle} as well * as {@code MethodType} references are also supported. Alternatively, the fixed value is stored * in a static field. * * <p>When a value is stored in the class's constant pool, its identity is lost. If an object's * identity is important, the {@link FixedValue#reference(Object)} method should be used instead. * * @param fixedValue The fixed value to return from the method. * @return An implementation for the given {@code fixedValue}. */ public static AssignerConfigurable value(Object fixedValue) { Class<?> type = fixedValue.getClass(); if (type == String.class) { return new ForPoolValue( new TextConstant((String) fixedValue), TypeDescription.STRING, Assigner.DEFAULT, Assigner.Typing.STATIC); } else if (type == Class.class) { return new ForPoolValue( ClassConstant.of(new TypeDescription.ForLoadedType((Class<?>) fixedValue)), TypeDescription.CLASS, Assigner.DEFAULT, Assigner.Typing.STATIC); } else if (type == Boolean.class) { return new ForPoolValue( IntegerConstant.forValue((Boolean) fixedValue), new TypeDescription.ForLoadedType(boolean.class), Assigner.DEFAULT, Assigner.Typing.STATIC); } else if (type == Byte.class) { return new ForPoolValue( IntegerConstant.forValue((Byte) fixedValue), new TypeDescription.ForLoadedType(byte.class), Assigner.DEFAULT, Assigner.Typing.STATIC); } else if (type == Short.class) { return new ForPoolValue( IntegerConstant.forValue((Short) fixedValue), new TypeDescription.ForLoadedType(short.class), Assigner.DEFAULT, Assigner.Typing.STATIC); } else if (type == Character.class) { return new ForPoolValue( IntegerConstant.forValue((Character) fixedValue), new TypeDescription.ForLoadedType(char.class), Assigner.DEFAULT, Assigner.Typing.STATIC); } else if (type == Integer.class) { return new ForPoolValue( IntegerConstant.forValue((Integer) fixedValue), new TypeDescription.ForLoadedType(int.class), Assigner.DEFAULT, Assigner.Typing.STATIC); } else if (type == Long.class) { return new ForPoolValue( LongConstant.forValue((Long) fixedValue), new TypeDescription.ForLoadedType(long.class), Assigner.DEFAULT, Assigner.Typing.STATIC); } else if (type == Float.class) { return new ForPoolValue( FloatConstant.forValue((Float) fixedValue), new TypeDescription.ForLoadedType(float.class), Assigner.DEFAULT, Assigner.Typing.STATIC); } else if (type == Double.class) { return new ForPoolValue( DoubleConstant.forValue((Double) fixedValue), new TypeDescription.ForLoadedType(double.class), Assigner.DEFAULT, Assigner.Typing.STATIC); } else if (JavaType.METHOD_HANDLE.getTypeStub().isAssignableFrom(type)) { return new ForPoolValue( MethodHandleConstant.of(JavaInstance.MethodHandle.of(fixedValue)), new TypeDescription.ForLoadedType(type), Assigner.DEFAULT, Assigner.Typing.STATIC); } else if (JavaType.METHOD_TYPE.getTypeStub().represents(type)) { return new ForPoolValue( MethodTypeConstant.of(JavaInstance.MethodType.of(fixedValue)), new TypeDescription.ForLoadedType(type), Assigner.DEFAULT, Assigner.Typing.STATIC); } else { return reference(fixedValue); } }