public ConstantExpression(Object value, boolean keepPrimitive) { this.value = value; if (value != null) { if (keepPrimitive) { if (value instanceof Integer) { setType(ClassHelper.int_TYPE); } else if (value instanceof Long) { setType(ClassHelper.long_TYPE); } else if (value instanceof Boolean) { setType(ClassHelper.boolean_TYPE); } else if (value instanceof Double) { setType(ClassHelper.double_TYPE); } else if (value instanceof Float) { setType(ClassHelper.float_TYPE); } else if (value instanceof Character) { setType(ClassHelper.char_TYPE); } else { setType(ClassHelper.make(value.getClass())); } //TODO: more cases here } else { setType(ClassHelper.make(value.getClass())); } } }
/** * When constant expressions are created, the value is always wrapped to a non primitive type. * Some constant expressions are optimized to return primitive types, but not all primitives are * handled. This method guarantees to return a similar constant expression but with a primitive type * instead of a boxed type. * <p/> * Additionally, single char strings are converted to 'char' types. * * @param constantExpression a constant expression * @return the same instance of constant expression if the type is already primitive, or a primitive * constant if possible. */ public static ConstantExpression transformToPrimitiveConstantIfPossible(ConstantExpression constantExpression) { Object value = constantExpression.getValue(); if (value == null) return constantExpression; ConstantExpression result; ClassNode type = constantExpression.getType(); if (ClassHelper.isPrimitiveType(type)) return constantExpression; if (value instanceof String && ((String) value).length() == 1) { result = new ConstantExpression(((String) value).charAt(0)); result.setType(ClassHelper.char_TYPE); } else { type = ClassHelper.getUnwrapper(type); result = new ConstantExpression(value, true); result.setType(type); } return result; }
protected void addTypeCheckingInfoAnnotation(final MethodNode node) { // TypeChecked$TypeCheckingInfo can not be applied on constructors if (node instanceof ConstructorNode) return; // if a returned inferred type is available and no @TypeCheckingInfo is on node, then add an // annotation to the method node ClassNode rtype = getInferredReturnType(node); if (rtype != null && node.getAnnotations(TYPECHECKING_INFO_NODE).isEmpty()) { AnnotationNode anno = new AnnotationNode(TYPECHECKING_INFO_NODE); anno.setMember("version", CURRENT_SIGNATURE_PROTOCOL); SignatureCodec codec = SignatureCodecFactory.getCodec(CURRENT_SIGNATURE_PROTOCOL_VERSION, getTransformLoader()); String genericsSignature = codec.encode(rtype); if (genericsSignature != null) { ConstantExpression signature = new ConstantExpression(genericsSignature); signature.setType(STRING_TYPE); anno.setMember("inferredType", signature); node.addAnnotation(anno); } } }
private static DeclarationExpression optimizeConstantInitialization( final BinaryExpression originalDeclaration, final Token operation, final ConstantExpression constant, final Expression leftExpression, final ClassNode declarationType) { ConstantExpression cexp = new ConstantExpression( convertConstant((Number) constant.getValue(), ClassHelper.getWrapper(declarationType)), true); cexp.setType(declarationType); cexp.setSourcePosition(constant); DeclarationExpression result = new DeclarationExpression( leftExpression, operation, cexp ); result.setSourcePosition(originalDeclaration); result.copyNodeMetaData(originalDeclaration); return result; }
public ConstantExpression(Object value, boolean keepPrimitive) { this.value = value; if (value != null) { if (keepPrimitive) { if (value instanceof Integer) { setType(ClassHelper.int_TYPE); } else if (value instanceof Boolean) { setType(ClassHelper.boolean_TYPE); } else if (value instanceof Double) { setType(ClassHelper.double_TYPE); } else { setType(ClassHelper.make(value.getClass())); } //TODO: more cases here } else { setType(ClassHelper.make(value.getClass())); } } }
public ConstantExpression(Object value) { this.value = value; if (this.value != null) setType(ClassHelper.make(value.getClass())); }
public ConstantExpression(Object value) { this.value = value; if (this.value != null) setType(ClassHelper.make(value.getClass())); }
public ConstantExpression(Object value) { this.value = value; if (this.value != null) setType(ClassHelper.make(value.getClass())); }
/** * When constant expressions are created, the value is always wrapped to a non primitive type. * Some constant expressions are optimized to return primitive types, but not all primitives are * handled. This method guarantees to return a similar constant expression but with a primitive type * instead of a boxed type. * * Additionaly, single char strings are converted to 'char' types. * * @param constantExpression a constant expression * @return the same instance of constant expression if the type is already primitive, or a primitive * constant if possible. */ public static ConstantExpression transformToPrimitiveConstantIfPossible(ConstantExpression constantExpression) { Object value = constantExpression.getValue(); if (value ==null) return constantExpression; ConstantExpression result; ClassNode type = constantExpression.getType(); if (ClassHelper.isPrimitiveType(type)) return constantExpression; if (value instanceof String && ((String)value).length()==1) { result = new ConstantExpression(((String)value).charAt(0)); result.setType(ClassHelper.char_TYPE); } else { type = ClassHelper.getUnwrapper(type); result = new ConstantExpression(value, true); result.setType(type); } return result; }
ConstantExpression constantExpression = (ConstantExpression) expression; if (!constantExpression.isNullExpression()) { constantExpression.setType(new ClassNode(constantExpression.getValue().getClass()));