private static FieldNode setMetaClassFieldIfNotExists(ClassNode node, FieldNode metaClassField) { if (metaClassField != null) return metaClassField; final String classInternalName = BytecodeHelper.getClassInternalName(node); metaClassField = node.addField("metaClass", ACC_PRIVATE | ACC_TRANSIENT | ACC_SYNTHETIC, ClassHelper.METACLASS_TYPE, new BytecodeExpression(ClassHelper.METACLASS_TYPE) { public void visit(MethodVisitor mv) { mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, classInternalName, "$getStaticMetaClass", "()Lgroovy/lang/MetaClass;", false); } }); metaClassField.setSynthetic(true); return metaClassField; }
private static void addFastPathHelperFieldsAndHelperMethod(ClassNode node, final String classInternalName, boolean knownSpecialCase) { if (node.getNodeMetaData(ClassNodeSkip.class) != null) return; FieldNode stMCB = checkFieldDoesNotExist(node, STATIC_METACLASS_BOOL); if (stMCB == null) { stMCB = node.addField( STATIC_METACLASS_BOOL, ACC_PUBLIC | ACC_STATIC | ACC_SYNTHETIC | ACC_TRANSIENT, ClassHelper.boolean_TYPE, null); stMCB.setSynthetic(true); } }
protected void addTimeStamp(ClassNode node) { if (node.getDeclaredField(Verifier.__TIMESTAMP) == null) { // in case if verifier visited the call already FieldNode timeTagField = new FieldNode( Verifier.__TIMESTAMP, ACC_PUBLIC | ACC_STATIC | ACC_SYNTHETIC, ClassHelper.long_TYPE, //"", node, new ConstantExpression(System.currentTimeMillis())); // alternatively, FieldNode timeTagField = SourceUnit.createFieldNode("public static final long __timeStamp = " + System.currentTimeMillis() + "L"); timeTagField.setSynthetic(true); node.addField(timeTagField); timeTagField = new FieldNode( Verifier.__TIMESTAMP__ + String.valueOf(System.currentTimeMillis()), ACC_PUBLIC | ACC_STATIC | ACC_SYNTHETIC, ClassHelper.long_TYPE, //"", node, new ConstantExpression((long) 0)); // alternatively, FieldNode timeTagField = SourceUnit.createFieldNode("public static final long __timeStamp = " + System.currentTimeMillis() + "L"); timeTagField.setSynthetic(true); node.addField(timeTagField); } }
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); } }
staticMetaClassField.setSynthetic(true);
private void completeEnum(ClassNode enumClass) { boolean isAic = isAnonymousInnerClass(enumClass); // create MIN_VALUE and MAX_VALUE fields FieldNode minValue = null, maxValue = null, values = null; if (!isAic) { ClassNode enumRef = enumClass.getPlainNodeReference(); // create values field values = new FieldNode("$VALUES", PRIVATE_FS | Opcodes.ACC_SYNTHETIC, enumRef.makeArray(), enumClass, null); values.setSynthetic(true); addMethods(enumClass, values); checkForAbstractMethods(enumClass); // create MIN_VALUE and MAX_VALUE fields minValue = new FieldNode("MIN_VALUE", PUBLIC_FS, enumRef, enumClass, null); maxValue = new FieldNode("MAX_VALUE", PUBLIC_FS, enumRef, enumClass, null); } addInit(enumClass, minValue, maxValue, values, isAic); }
private Variable findClassMember(ClassNode cn, String name) { if (cn == null) return null; if (cn.isScript()) { return new DynamicVariable(name, false); } for (FieldNode fn : cn.getFields()) { if (fn.getName().equals(name)) return fn; } for (MethodNode mn : cn.getMethods()) { String pName = getPropertyName(mn); if (name.equals(pName)) { PropertyNode property = new PropertyNode(name, mn.getModifiers(), ClassHelper.OBJECT_TYPE, cn, null, null, null); property.getField().setHasNoRealSourcePosition(true); property.getField().setSynthetic(true); property.getField().setDeclaringClass(cn); property.setDeclaringClass(cn); return property; } } for (PropertyNode pn : cn.getProperties()) { if (pn.getName().equals(name)) return pn; } Variable ret = findClassMember(cn.getSuperClass(), name); if (ret != null) return ret; return findClassMember(cn.getOuterClass(), name); }
fieldNode.setSynthetic(true);
private FieldNode setMetaClassFieldIfNotExists(ClassNode node, FieldNode metaClassField) { if (metaClassField != null) return metaClassField; final String classInternalName = BytecodeHelper.getClassInternalName(node); metaClassField = node.addField("metaClass", ACC_PRIVATE | ACC_TRANSIENT | ACC_SYNTHETIC, ClassHelper.METACLASS_TYPE, new BytecodeExpression(ClassHelper.METACLASS_TYPE) { public void visit(MethodVisitor mv) { mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, classInternalName, "$getStaticMetaClass", "()Lgroovy/lang/MetaClass;"); }}); metaClassField.setSynthetic(true); return metaClassField; }
private FieldNode setMetaClassFieldIfNotExists(ClassNode node, FieldNode metaClassField){ if (metaClassField != null) return metaClassField; final String classInternalName = BytecodeHelper.getClassInternalName(node); metaClassField = node.addField("metaClass", ACC_PRIVATE | ACC_TRANSIENT | ACC_SYNTHETIC, ClassHelper.METACLASS_TYPE, new BytecodeExpression() { public void visit(MethodVisitor mv) { mv.visitVarInsn(ALOAD, 0); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKEVIRTUAL, classInternalName, "$getStaticMetaClass", "()Lgroovy/lang/MetaClass;"); mv.visitFieldInsn(PUTFIELD, classInternalName, "metaClass", "Lgroovy/lang/MetaClass;"); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, classInternalName, "metaClass", "Lgroovy/lang/MetaClass;"); } public ClassNode getType() { return ClassHelper.METACLASS_TYPE; } }); metaClassField.setSynthetic(true); return metaClassField; }
private FieldNode setMetaClassFieldIfNotExists(ClassNode node, FieldNode metaClassField){ if (metaClassField != null) return metaClassField; final String classInternalName = BytecodeHelper.getClassInternalName(node); metaClassField = node.addField("metaClass", ACC_PRIVATE | ACC_TRANSIENT | ACC_SYNTHETIC, ClassHelper.METACLASS_TYPE, new BytecodeExpression() { public void visit(MethodVisitor mv) { mv.visitVarInsn(ALOAD, 0); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKEVIRTUAL, classInternalName, "$getStaticMetaClass", "()Lgroovy/lang/MetaClass;"); mv.visitFieldInsn(PUTFIELD, classInternalName, "metaClass", "Lgroovy/lang/MetaClass;"); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, classInternalName, "metaClass", "Lgroovy/lang/MetaClass;"); } public ClassNode getType() { return ClassHelper.METACLASS_TYPE; } }); metaClassField.setSynthetic(true); return metaClassField; }
protected void addTimeStamp(ClassNode node) { FieldNode timeTagField = new FieldNode( Verifier.__TIMESTAMP, Modifier.PUBLIC | Modifier.STATIC, ClassHelper.Long_TYPE, //"", node, new ConstantExpression(new Long(System.currentTimeMillis()))); // alternatively , FieldNode timeTagField = SourceUnit.createFieldNode("public static final long __timeStamp = " + System.currentTimeMillis() + "L"); timeTagField.setSynthetic(true); node.addField(timeTagField); timeTagField = new FieldNode( Verifier.__TIMESTAMP__ + String.valueOf(System.currentTimeMillis()), Modifier.PUBLIC | Modifier.STATIC, ClassHelper.Long_TYPE, //"", node, new ConstantExpression(new Long(0))); // alternatively , FieldNode timeTagField = SourceUnit.createFieldNode("public static final long __timeStamp = " + System.currentTimeMillis() + "L"); timeTagField.setSynthetic(true); node.addField(timeTagField); }
private void addFastPathHelperFieldsAndHelperMethod(ClassNode node, final String classInternalName, boolean knownSpecialCase) { if (node.getNodeMetaData(ClassNodeSkip.class)!=null) return; FieldNode stMCB = checkFieldDoesNotExist(node,STATIC_METACLASS_BOOL); if (stMCB==null) { stMCB = node.addField( STATIC_METACLASS_BOOL, ACC_PUBLIC | ACC_STATIC | ACC_SYNTHETIC | ACC_TRANSIENT, ClassHelper.boolean_TYPE, null); stMCB.setSynthetic(true); } }
protected void addTimeStamp(ClassNode node) { FieldNode timeTagField = new FieldNode( Verifier.__TIMESTAMP, ACC_PUBLIC | ACC_STATIC | ACC_SYNTHETIC, ClassHelper.Long_TYPE, //"", node, new ConstantExpression(System.currentTimeMillis())); // alternatively , FieldNode timeTagField = SourceUnit.createFieldNode("public static final long __timeStamp = " + System.currentTimeMillis() + "L"); timeTagField.setSynthetic(true); node.addField(timeTagField); timeTagField = new FieldNode( Verifier.__TIMESTAMP__ + String.valueOf(System.currentTimeMillis()), ACC_PUBLIC | ACC_STATIC | ACC_SYNTHETIC, ClassHelper.Long_TYPE, //"", node, new ConstantExpression((long) 0)); // alternatively , FieldNode timeTagField = SourceUnit.createFieldNode("public static final long __timeStamp = " + System.currentTimeMillis() + "L"); timeTagField.setSynthetic(true); node.addField(timeTagField); }
private void setConstField(ConstantExpression constantExpression) { final Object n = constantExpression.getValue(); if (!(n instanceof Number || n instanceof Character)) return; FieldNode field = (FieldNode) const2Var.get(n); if (field!=null) { constantExpression.setConstantName(field.getName()); return; } final String name = "$const$" + const2Var.size(); //TODO: this part here needs a bit of rethinking. If it can happen that the field is defined already, // then is this code still valid? field = currentClass.getDeclaredField(name); if (field==null) { field = new FieldNode(name, Opcodes.ACC_PRIVATE|Opcodes.ACC_STATIC|Opcodes.ACC_SYNTHETIC| Opcodes.ACC_FINAL, constantExpression.getType(), currentClass, constantExpression ); field.setSynthetic(true); missingFields.add(field); } constantExpression.setConstantName(field.getName()); const2Var.put(n, field); }
private void setConstField(ConstantExpression constantExpression) { final Object n = constantExpression.getValue(); if (!(n instanceof Number || n instanceof Character)) return; FieldNode field = (FieldNode) const2Var.get(n); if (field!=null) { constantExpression.setConstantName(field.getName()); return; } final String name = "$const$" + const2Var.size(); //TODO: this part here needs a bit of rethinking. If it can happen that the field is defined already, // then is this code still valid? field = currentClass.getDeclaredField(name); if (field==null) { field = new FieldNode(name, Opcodes.ACC_PRIVATE|Opcodes.ACC_STATIC|Opcodes.ACC_SYNTHETIC| Opcodes.ACC_FINAL, constantExpression.getType(), currentClass, constantExpression ); field.setSynthetic(true); missingFields.add(field); } constantExpression.setConstantName(field.getName()); const2Var.put(n, field); }
private void setConstField(ConstantExpression constantExpression) { final Object n = constantExpression.getValue(); if (!(n instanceof Number || n instanceof Character)) return; if (n instanceof Integer || n instanceof Double) return; FieldNode field = (FieldNode) const2Var.get(n); if (field!=null) { constantExpression.setConstantName(field.getName()); return; } final String name = "$const$" + const2Var.size(); //TODO: this part here needs a bit of rethinking. If it can happen that the field is defined already, // then is this code still valid? field = currentClass.getDeclaredField(name); if (field==null) { field = new FieldNode(name, Opcodes.ACC_PRIVATE|Opcodes.ACC_STATIC|Opcodes.ACC_SYNTHETIC| Opcodes.ACC_FINAL, constantExpression.getType(), currentClass, constantExpression ); field.setSynthetic(true); missingFields.add(field); } constantExpression.setConstantName(field.getName()); const2Var.put(n, field); }
/** * @param name is the full name of the class * @param modifiers the modifiers, * @param superClass the base class name - use "java.lang.Object" if no direct * base class * @see org.objectweb.asm.Opcodes */ public ClassNode(String name, int modifiers, ClassNode superClass, ClassNode[] interfaces, MixinNode[] mixins) { this.name = name; this.modifiers = modifiers; this.superClass = superClass; this.interfaces = interfaces; this.mixins = mixins; isPrimaryNode = true; if (superClass!=null) { usesGenerics = superClass.isUsingGenerics(); } if (!usesGenerics && interfaces!=null) { for (int i = 0; i < interfaces.length; i++) { usesGenerics = usesGenerics || interfaces[i].isUsingGenerics(); } } if ((modifiers & ACC_INTERFACE) == 0) addField("$ownClass", ACC_STATIC|ACC_PUBLIC|ACC_FINAL|ACC_SYNTHETIC, ClassHelper.CLASS_Type, new ClassExpression(this)).setSynthetic(true); }
private void completeEnum(ClassNode enumClass) { boolean isAic = isAnonymousInnerClass(enumClass); // create MIN_VALUE and MAX_VALUE fields FieldNode minValue = null, maxValue = null, values = null; if (!isAic) { ClassNode enumRef = enumClass.getPlainNodeReference(); // create values field values = new FieldNode("$VALUES", PRIVATE_FS | Opcodes.ACC_SYNTHETIC, enumRef.makeArray(), enumClass, null); values.setSynthetic(true); addMethods(enumClass, values); // create MIN_VALUE and MAX_VALUE fields minValue = new FieldNode("MIN_VALUE", PUBLIC_FS, enumRef, enumClass, null); maxValue = new FieldNode("MAX_VALUE", PUBLIC_FS, enumRef, enumClass, null); } addInit(enumClass, minValue, maxValue, values, isAic); }
/** * Adds an instance field which allows to control whether GContract assertions * are enabled or not. Before assertions are evaluated this field will be checked. * * @see Configurator * * @param type the current {@link ClassNode} */ public void init(final ClassNode type) { Validate.notNull(type); StaticMethodCallExpression checkAssertionsEnabledMethodCall = new StaticMethodCallExpression(ClassHelper.makeWithoutCaching(Configurator.class), "checkAssertionsEnabled", new ArgumentListExpression(new ConstantExpression(type.getName()))); final FieldNode fieldNode = type.addField(BaseVisitor.GCONTRACTS_ENABLED_VAR, Opcodes.ACC_STATIC | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_FINAL, ClassHelper.boolean_TYPE, checkAssertionsEnabledMethodCall); fieldNode.setSynthetic(true); } }