List of usage examples for org.objectweb.asm Opcodes ACC_PRIVATE
int ACC_PRIVATE
To view the source code for org.objectweb.asm Opcodes ACC_PRIVATE.
Click Source Link
From source file:org.spongepowered.asm.mixin.transformer.MixinPreProcessor.java
License:MIT License
private void attachMethods(MixinTargetContext context) { for (MethodNode mixinMethod : this.classNode.methods) { AnnotationNode shadowAnnotation = ASMHelper.getVisibleAnnotation(mixinMethod, Shadow.class); if (shadowAnnotation == null) { continue; }// www . j a va2s .c om Method method = this.mixin.getClassInfo().findMethod(mixinMethod, true); MethodNode target = MixinPreProcessor.findMethod(context.getTargetClass(), mixinMethod, shadowAnnotation); if (target == null) { throw new InvalidMixinException(this.mixin, "Shadow method " + mixinMethod.name + " was not located in the target class"); } if (Constants.INIT.equals(target.name)) { throw new InvalidMixinException(this.mixin, "Nice try! Cannot alias a constructor!"); } if (!target.name.equals(mixinMethod.name)) { if ((target.access & Opcodes.ACC_PRIVATE) == 0) { throw new InvalidMixinException(this.mixin, "Non-private method cannot be aliased. Found " + target.name); } mixinMethod.name = target.name; method.renameTo(target.name); } } }
From source file:org.spongepowered.asm.mixin.transformer.MixinPreProcessor.java
License:MIT License
private void attachFields(MixinTargetContext context) { for (Iterator<FieldNode> iter = this.classNode.fields.iterator(); iter.hasNext();) { FieldNode mixinField = iter.next(); AnnotationNode shadow = ASMHelper.getVisibleAnnotation(mixinField, Shadow.class); if (!this.validateField(context, mixinField, shadow)) { iter.remove();/*from w ww . j a va2 s. co m*/ continue; } context.transformDescriptor(mixinField); Field field = this.mixin.getClassInfo().findField(mixinField); FieldNode target = this.findField(context.getTargetClass(), mixinField, shadow); if (target == null) { // If this field is a shadow field but is NOT found in the target class, that's bad, mmkay if (shadow != null) { throw new InvalidMixinException(this.mixin, "Shadow field " + mixinField.name + " was not located in the target class"); } } else { // Check that the shadow field has a matching descriptor if (!target.desc.equals(mixinField.desc)) { throw new InvalidMixinException(this.mixin, "The field " + mixinField.name + " in the target class has a conflicting signature"); } if (!target.name.equals(mixinField.name)) { if ((target.access & Opcodes.ACC_PRIVATE) == 0) { throw new InvalidMixinException(this.mixin, "Non-private field cannot be aliased. Found " + target.name); } mixinField.name = target.name; field.renameTo(target.name); } // Shadow fields get stripped from the mixin class iter.remove(); } } }
From source file:org.spongepowered.asm.mixin.transformer.MixinPreProcessor.java
License:MIT License
private boolean validateField(MixinTargetContext context, FieldNode field, AnnotationNode shadow) { // Public static fields will fall foul of early static binding in java, including them in a mixin is an error condition if (MixinTransformer.hasFlag(field, Opcodes.ACC_STATIC) && !MixinTransformer.hasFlag(field, Opcodes.ACC_PRIVATE)) { throw new InvalidMixinException(context, String .format("Mixin classes cannot contain visible static methods or fields, found %s", field.name)); }/* w w w. j a va 2 s .com*/ // Shadow fields can't have prefixes, it's meaningless for them anyway String prefix = ASMHelper.<String>getAnnotationValue(shadow, "prefix", Shadow.class); if (field.name.startsWith(prefix)) { throw new InvalidMixinException(context, String.format( "Shadow field %s in %s has a shadow prefix. This is not allowed.", field.name, context)); } // Imaginary super fields get stripped from the class, but first we validate them if (Constants.IMAGINARY_SUPER.equals(field.name)) { if (field.access != Opcodes.ACC_PRIVATE) { throw new InvalidMixinException(this.mixin, "Imaginary super field " + field.name + " must be private and non-final"); } if (!field.desc.equals("L" + this.mixin.getClassRef() + ";")) { throw new InvalidMixinException(this.mixin, "Imaginary super field " + field.name + " must have the same type as the parent mixin"); } return false; } return true; }
From source file:org.spongepowered.asm.mixin.transformer.MixinTargetContext.java
License:MIT License
/** * Transforms field descriptors which contain mixin types to their * appropriate target type/*from ww w. j a v a2s . c om*/ * * @param field Field to transform * @return true if the field should be processed further, false to remove it */ public boolean transformField(FieldNode field) { if (MixinTargetContext.IMAGINARY_SUPER.equals(field.name)) { if (field.access != Opcodes.ACC_PRIVATE) { throw new InvalidMixinException(this, "Imaginary super field " + field.name + " must be private and non-final"); } if (!field.desc.equals("L" + this.mixin.getClassRef() + ";")) { throw new InvalidMixinException(this, "Imaginary super field " + field.name + " must have the same type as the parent mixin"); } return false; } this.transformDescriptor(field); return true; }
From source file:org.spongepowered.asm.mixin.transformer.MixinTargetContext.java
License:MIT License
/** * Handle "imaginary super" invokations, these are invokations in * non-derived mixins for accessing methods known to exist in a supermixin * which is not directly inherited by this mixix. The method can only call * its <b>own</b> super-implmentation and the methd must also be tagged with * {@link SoftOverride} to indicate that the method must exist in a super * class.//from w w w. j av a2 s . co m * * @param method Method being processed * @param fieldInsn the GETFIELD insn which access the pseudo-field which is * used as a handle to the superclass */ private void processImaginarySuper(MethodNode method, FieldInsnNode fieldInsn) { if (fieldInsn.getOpcode() != Opcodes.GETFIELD) { if (MixinTargetContext.INIT.equals(method.name)) { throw new InvalidMixinException(this, "Illegal imaginary super declaration: field " + fieldInsn.name + " must not specify an initialiser"); } throw new InvalidMixinException(this, "Illegal imaginary super access: found " + ASMHelper.getOpcodeName(fieldInsn.getOpcode()) + " opcode in " + method.name + method.desc); } if ((method.access & Opcodes.ACC_PRIVATE) != 0 || (method.access & Opcodes.ACC_STATIC) != 0) { throw new InvalidMixinException(this, "Illegal imaginary super access: method " + method.name + method.desc + " is private or static"); } if (ASMHelper.getInvisibleAnnotation(method, SoftOverride.class) == null) { throw new InvalidMixinException(this, "Illegal imaginary super access: method " + method.name + method.desc + " is not decorated with @SoftOverride"); } for (Iterator<AbstractInsnNode> methodIter = method.instructions .iterator(method.instructions.indexOf(fieldInsn)); methodIter.hasNext();) { AbstractInsnNode insn = methodIter.next(); if (insn instanceof MethodInsnNode) { MethodInsnNode methodNode = (MethodInsnNode) insn; if (methodNode.owner.equals(this.getClassRef()) && methodNode.name.equals(method.name) && methodNode.desc.equals(method.desc)) { methodNode.setOpcode(Opcodes.INVOKESPECIAL); this.updateStaticBinding(method, methodNode); return; } } } throw new InvalidMixinException(this, "Illegal imaginary super access: could not find INVOKE for " + method.name + method.desc); }
From source file:org.spongepowered.asm.mixin.transformer.MixinTransformer.java
License:MIT License
/** * Field sanity checks/*from w w w .ja va2 s . c om*/ * @param mixin * @param field * @param shadow */ private void validateField(MixinTargetContext mixin, FieldNode field, AnnotationNode shadow) { // Public static fields will fall foul of early static binding in java, including them in a mixin is an error condition if (MixinTransformer.hasFlag(field, Opcodes.ACC_STATIC) && !MixinTransformer.hasFlag(field, Opcodes.ACC_PRIVATE)) { throw new InvalidMixinException(mixin, String .format("Mixin classes cannot contain visible static methods or fields, found %s", field.name)); } // Shadow fields can't have prefixes, it's meaningless for them anyway String prefix = ASMHelper.<String>getAnnotationValue(shadow, "prefix", Shadow.class); if (field.name.startsWith(prefix)) { throw new InvalidMixinException(mixin, String .format("Shadow field %s in %s has a shadow prefix. This is not allowed.", field.name, mixin)); } }
From source file:org.spongepowered.asm.mixin.transformer.MixinTransformer.java
License:MIT License
/** * Mixin methods from the mixin class into the target class * //w w w. j a v a 2 s. c o m * @param targetClass * @param mixin */ private void applyMixinMethods(ClassNode targetClass, MixinTargetContext mixin) { for (MethodNode mixinMethod : mixin.getClassNode().methods) { // Reparent all mixin methods into the target class mixin.transformMethod(mixinMethod); boolean isShadow = ASMHelper.getVisibleAnnotation(mixinMethod, Shadow.class) != null; boolean isOverwrite = ASMHelper.getVisibleAnnotation(mixinMethod, Overwrite.class) != null; boolean isAbstract = MixinTransformer.hasFlag(mixinMethod, Opcodes.ACC_ABSTRACT); if (isShadow || isAbstract) { // For shadow (and abstract, which can be used as a shorthand for Shadow) methods, we just check they're present MethodNode target = this.findTargetMethod(targetClass, mixinMethod); if (target == null) { throw new InvalidMixinException(mixin, String .format("Shadow method %s was not located in the target class", mixinMethod.name)); } } else if (!mixinMethod.name.startsWith("<")) { if (MixinTransformer.hasFlag(mixinMethod, Opcodes.ACC_STATIC) && !MixinTransformer.hasFlag(mixinMethod, Opcodes.ACC_PRIVATE) && !MixinTransformer.hasFlag(mixinMethod, Opcodes.ACC_SYNTHETIC) && !isOverwrite) { throw new InvalidMixinException(mixin, String.format("Mixin classes cannot contain visible static methods or fields, found %s", mixinMethod.name)); } this.mergeMethod(targetClass, mixin, mixinMethod, isOverwrite); } else if (MixinTransformer.CLINIT.equals(mixinMethod.name)) { // Class initialiser insns get appended this.appendInsns(targetClass, mixinMethod.name, mixinMethod); } } }
From source file:org.spongepowered.asm.util.SignaturePrinter.java
License:MIT License
public void setModifiers(MethodNode method) { String returnType = SignaturePrinter.getTypeName(Type.getReturnType(method.desc), false); if ((method.access & Opcodes.ACC_PUBLIC) != 0) { this.setModifiers("public " + returnType); } else if ((method.access & Opcodes.ACC_PROTECTED) != 0) { this.setModifiers("protected " + returnType); } else if ((method.access & Opcodes.ACC_PRIVATE) != 0) { this.setModifiers("private " + returnType); } else {/*from ww w .j ava 2s. com*/ this.setModifiers(returnType); } }
From source file:org.spongepowered.mod.asm.transformers.MixinTransformer.java
License:MIT License
/** * Mixin fields from mixin class into the target class. It is vital that this is done before mixinMethods because we need to compute renamed * fields so that transformMethod can rename field references in the method body * //from ww w . j a v a2 s.c om * @param targetClass * @param mixin */ private void applyMixinFields(ClassNode targetClass, MixinData mixin) { for (FieldNode field : mixin.getClassNode().fields) { // Public static fields will fall foul of early static binding in java, including them in a mixin is an error condition if (MixinTransformer.hasFlag(field, Opcodes.ACC_STATIC) && !MixinTransformer.hasFlag(field, Opcodes.ACC_PRIVATE)) { throw new InvalidMixinException(String.format( "Mixin classes cannot contain visible static methods or fields, found %s", field.name)); } FieldNode target = this.findTargetField(targetClass, field); if (target == null) { // If this field is a shadow field but is NOT found in the target class, that's bad, mmkay boolean isShadow = ASMHelper.getVisibleAnnotation(field, Shadow.class) != null; if (isShadow) { throw new InvalidMixinException( String.format("Shadow field %s was not located in the target class", field.name)); } // This is just a local field, so add it targetClass.fields.add(field); } else { // Check that the shadow field has a matching descriptor if (!target.desc.equals(field.desc)) { throw new InvalidMixinException(String .format("The field %s in the target class has a conflicting signature", field.name)); } } } }
From source file:org.spongepowered.mod.asm.transformers.MixinTransformer.java
License:MIT License
/** * Mixin methods from the mixin class into the target class * /*from w w w . ja v a 2 s .co m*/ * @param targetClass * @param mixin */ private void applyMixinMethods(ClassNode targetClass, MixinData mixin) { for (MethodNode mixinMethod : mixin.getClassNode().methods) { // Reparent all mixin methods into the target class this.transformMethod(mixinMethod, mixin.getClassNode().name, targetClass.name); boolean isShadow = ASMHelper.getVisibleAnnotation(mixinMethod, Shadow.class) != null; boolean isOverwrite = ASMHelper.getVisibleAnnotation(mixinMethod, Overwrite.class) != null; boolean isAbstract = MixinTransformer.hasFlag(mixinMethod, Opcodes.ACC_ABSTRACT); if (isShadow || isAbstract) { // For shadow (and abstract, which can be used as a shorthand for Shadow) methods, we just check they're present MethodNode target = this.findTargetMethod(targetClass, mixinMethod); if (target == null) { throw new InvalidMixinException(String .format("Shadow method %s was not located in the target class", mixinMethod.name)); } } else if (!mixinMethod.name.startsWith("<")) { if (MixinTransformer.hasFlag(mixinMethod, Opcodes.ACC_STATIC) && !MixinTransformer.hasFlag(mixinMethod, Opcodes.ACC_PRIVATE) && !isOverwrite) { throw new InvalidMixinException( String.format("Mixin classes cannot contain visible static methods or fields, found %s", mixinMethod.name)); } MethodNode target = this.findTargetMethod(targetClass, mixinMethod); if (target != null) { targetClass.methods.remove(target); } else if (isOverwrite) { throw new InvalidMixinException(String .format("Overwrite target %s was not located in the target class", mixinMethod.name)); } targetClass.methods.add(mixinMethod); } else if ("<clinit>".equals(mixinMethod.name)) { // Class initialiser insns get appended this.appendInsns(targetClass, mixinMethod.name, mixinMethod); } } }