public void visitConstantExpression(final ConstantExpression expression) { assertExpressionAuthorized(expression); final String type = expression.getType().getName(); if (constantTypesWhiteList != null && !constantTypesWhiteList.contains(type)) { throw new SecurityException("Constant expression type [" + type + "] is not allowed"); } if (constantTypesBlackList != null && constantTypesBlackList.contains(type)) { throw new SecurityException("Constant expression type [" + type + "] is not allowed"); } }
private static Character tryCharConstant(final Expression expr) { if (expr instanceof ConstantExpression) { ConstantExpression ce = (ConstantExpression) expr; if (ClassHelper.STRING_TYPE.equals(ce.getType())) { String val = (String) ce.getValue(); if (val!=null && val.length()==1) { return val.charAt(0); } } } return null; }
ConstantExpression ce = (ConstantExpression) re; Object value = ce.getValue(); if (ClassHelper.STRING_TYPE.equals(ce.getType())) { out.print(formatString((String)value)); } else if (ClassHelper.char_TYPE.equals(ce.getType()) || ClassHelper.Character_TYPE.equals(ce.getType())) { out.print(formatChar(value.toString())); } else if (ClassHelper.long_TYPE.equals(ce.getType())) { out.print("" + value + "L"); } else if (ClassHelper.float_TYPE.equals(ce.getType())) { out.print("" + value + "f"); } else if (ClassHelper.double_TYPE.equals(ce.getType())) { out.print("" + value + "d"); } else {
private void setConstField(ConstantExpression constantExpression) { final Object n = constantExpression.getValue(); if (!(n instanceof Number)) return; if (n instanceof Integer || n instanceof Double) return; if (n instanceof Long && (0L == (Long) n || 1L == (Long) n)) return; // LCONST_0, LCONST_1 boolean isPrimitive = isPrimitiveType(constantExpression.getType()); FieldNode field = isPrimitive ? const2Prims.get(n) : const2Objects.get(n); if (field != null) { constantExpression.setConstantName(field.getName()); return; } String name; do { name = "$const$" + index++; } while (currentClass.getDeclaredField(name) != null); // TODO consider moving initcode to <clinit> and remaking field final field = new FieldNode(name, Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC | Opcodes.ACC_SYNTHETIC, constantExpression.getType(), currentClass, constantExpression); field.setSynthetic(true); missingFields.add(field); constantExpression.setConstantName(field.getName()); if (isPrimitive) { const2Prims.put(n, field); } else { const2Objects.put(n, field); } }
protected void visitConstantExpression(String attrName, ConstantExpression constExpr, ClassNode attrType) { ClassNode constType = constExpr.getType(); ClassNode wrapperType = ClassHelper.getWrapper(constType); if (!hasCompatibleType(attrType, wrapperType)) { addError("Attribute '" + attrName + "' should have type '" + attrType.getName() + "'; but found type '" + constType.getName() + "'", constExpr); } }
/** * Generate byte code for constants * * @see <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#14152">Class field types</a> */ public void visitConstantExpression(ConstantExpression expression) { final String constantName = expression.getConstantName(); if (controller.isStaticConstructor() || constantName == null) { controller.getOperandStack().pushConstant(expression); } else { controller.getMethodVisitor().visitFieldInsn(GETSTATIC, controller.getInternalClassName(),constantName, BytecodeHelper.getTypeDescription(expression.getType())); controller.getOperandStack().push(expression.getType()); } }
/** * 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; }
ConstantExpression cexp = (ConstantExpression) transformed; cexp = transformToPrimitiveConstantIfPossible(cexp); if (fieldNode.isFinal() && ClassHelper.isStaticConstantInitializerType(cexp.getType()) && cexp.getType().equals(fieldNode.getType())) { fieldNode.setInitialValueExpression(transformed); return; // GROOVY-5150: primitive type constants will be initialized directly
if (expression instanceof ConstantExpression) { ConstantExpression ce = (ConstantExpression) expression; if (ClassHelper.STRING_TYPE.equals(ce.getType())) { String val = (String) ce.getValue(); if (val!=null && val.length()==1) {
MethodVisitor mv = controller.getMethodVisitor(); Object value = expression.getValue(); ClassNode origType = expression.getType().redirect(); ClassNode type = ClassHelper.getUnwrapper(origType); boolean boxing = origType!=type;
public static boolean isOfType(ConstantExpression constantExpression, Class<?> type) { return constantExpression.getType().getName().equals(type.getName()); }
public void visitConstantExpression(final ConstantExpression expression) { assertExpressionAuthorized(expression); final String type = expression.getType().getName(); if (constantTypesWhiteList != null && !constantTypesWhiteList.contains(type)) { throw new SecurityException("Constant expression type [" + type + "] is not allowed"); } if (constantTypesBlackList != null && constantTypesBlackList.contains(type)) { throw new SecurityException("Constant expression type [" + type + "] is not allowed"); } }
protected void visitConstantExpression(String attrName, ConstantExpression constExpr, ClassNode attrType) { if(!constExpr.getType().isDerivedFrom(attrType)) { addError("Attribute '" + attrName + "' should have type '" + attrType.getName() + "'; " + "but found type '" + constExpr.getType().getName() + "'", constExpr); } }
protected void visitConstantExpression(String attrName, ConstantExpression constExpr, ClassNode attrType) { if(!constExpr.getType().isDerivedFrom(attrType)) { addError("Attribute '" + attrName + "' should have type '" + attrType.getName() + "'; " + "but found type '" + constExpr.getType().getName() + "'", constExpr); } }
protected void visitConstantExpression(String attrName, ConstantExpression constExpr, ClassNode attrType) { if(!constExpr.getType().isDerivedFrom(attrType)) { addError("Attribute '" + attrName + "' should have type '" + attrType.getName() + "'; " + "but found type '" + constExpr.getType().getName() + "'", constExpr); } }
protected void visitConstantExpression(String attrName, ConstantExpression constExpr, ClassNode attrType) { ClassNode type = ClassHelper.getWrapper(constExpr.getType()); if (!type.isDerivedFrom(attrType)) { addError("Attribute '" + attrName + "' should have type '" + attrType.getName() + "'; " + "but found type '" + constExpr.getType().getName() + "'", constExpr); } }
/** * Generate byte code for constants * * @see <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#14152">Class field types</a> */ public void visitConstantExpression(ConstantExpression expression) { final String constantName = expression.getConstantName(); if ((methodNode != null && methodNode.getName().equals("<clinit>")) || constantName == null) { Object value = expression.getValue(); helper.loadConstant(value); } else { mv.visitFieldInsn(GETSTATIC, internalClassName,constantName, BytecodeHelper.getTypeDescription(expression.getType())); } }
/** * Generate byte code for constants * * @see <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#14152">Class field types</a> */ public void visitConstantExpression(ConstantExpression expression) { final String constantName = expression.getConstantName(); if ((methodNode != null && methodNode.getName().equals("<clinit>")) || constantName == null) { Object value = expression.getValue(); helper.loadConstant(value); } else { mv.visitFieldInsn(GETSTATIC, internalClassName,constantName, BytecodeHelper.getTypeDescription(expression.getType())); } }
/** * Generate byte code for constants * * @see <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#14152">Class field types</a> */ public void visitConstantExpression(ConstantExpression expression) { final String constantName = expression.getConstantName(); if (controller.isStaticConstructor() || constantName == null) { controller.getOperandStack().pushConstant(expression); } else { controller.getMethodVisitor().visitFieldInsn(GETSTATIC, controller.getInternalClassName(),constantName, BytecodeHelper.getTypeDescription(expression.getType())); controller.getOperandStack().push(expression.getType()); } }