List of usage examples for org.objectweb.asm Opcodes PUTFIELD
int PUTFIELD
To view the source code for org.objectweb.asm Opcodes PUTFIELD.
Click Source Link
From source file:org.mutabilitydetector.checkers.CollectionTypeWrappedInUnmodifiableIdiomCheckerTest.java
License:Apache License
@Test public void findsThatAssignmentIsNotSafelyCopiedIfCallToUnmodifiableMethodIsPrecededByInvokingNonWhitelistedMethod() throws Exception { final MethodInsnNode nonWhitelistedCopyMethod = new MethodInsnNode(Opcodes.INVOKESTATIC, "some/non/whitelisted/Type", "method", ""); final MethodInsnNode wrappingMethod = new MethodInsnNode(Opcodes.INVOKESTATIC, "java/util/Collections", "unmodifiableList", "") { @Override/* w w w. j ava 2 s. co m*/ public AbstractInsnNode getPrevious() { return nonWhitelistedCopyMethod; } }; FieldInsnNode fieldInsnNode = new FieldInsnNode(Opcodes.PUTFIELD, "some/type/Name", "fieldName", "java/util/List") { @Override public AbstractInsnNode getPrevious() { return wrappingMethod; } }; CollectionTypeWrappedInUnmodifiableIdiomChecker checker = new CollectionTypeWrappedInUnmodifiableIdiomChecker( fieldInsnNode, Type.getType(List.class)); assertThat(checker.checkWrappedInUnmodifiable().safelyCopiesBeforeWrapping, is(false)); assertThat(checker.checkWrappedInUnmodifiable(), is(WRAPS_BUT_DOES_NOT_COPY)); }
From source file:org.mutabilitydetector.checkers.CollectionTypeWrappedInUnmodifiableIdiomCheckerTest.java
License:Apache License
@Test public void findsThatAssignmentIsSafelyCopiedIfCallToUnmodifiableMethodIsPrecededByInvokingWhitelistedCopyMethod() throws Exception { final MethodInsnNode whitelistedCopyMethod = new MethodInsnNode(Opcodes.INVOKESTATIC, "java/util/ArrayList", "<init>", "(Ljava/util/Collection;)V"); final MethodInsnNode wrappingMethod = new MethodInsnNode(Opcodes.INVOKESTATIC, "java/util/Collections", "unmodifiableList", "") { @Override/*from www . ja va2 s .c o m*/ public AbstractInsnNode getPrevious() { return whitelistedCopyMethod; } }; FieldInsnNode fieldInsnNode = new FieldInsnNode(Opcodes.PUTFIELD, "some/type/Name", "fieldName", "java/lang/Iterable") { @Override public AbstractInsnNode getPrevious() { return wrappingMethod; } }; CollectionTypeWrappedInUnmodifiableIdiomChecker checker = new CollectionTypeWrappedInUnmodifiableIdiomChecker( fieldInsnNode, Type.getType(List.class)); assertThat(checker.checkWrappedInUnmodifiable().canBeWrapped, is(true)); assertThat(checker.checkWrappedInUnmodifiable().safelyCopiesBeforeWrapping, is(true)); assertThat(checker.checkWrappedInUnmodifiable(), is(WRAPS_AND_COPIES_SAFELY)); }
From source file:org.mutabilitydetector.checkers.CollectionTypeWrappedInUnmodifiableIdiomCheckerTest.java
License:Apache License
@Test public void decidesIfTypeCanBeSafelyWrappedUsingTypeThatIsActuallyAssignedRatherThanDeclarationOfField() throws Exception { final MethodInsnNode whitelistedCopyMethod = new MethodInsnNode(Opcodes.INVOKESTATIC, "java/util/ArrayList", "<init>", "(Ljava/util/Collection;)V"); final MethodInsnNode wrappingMethod = new MethodInsnNode(Opcodes.INVOKESTATIC, "java/util/Collections", "unmodifiableList", "") { @Override/* www. ja v a 2 s. c o m*/ public AbstractInsnNode getPrevious() { return whitelistedCopyMethod; } }; String typeOfField = "java/lang/Iterable"; FieldInsnNode fieldInsnNode = new FieldInsnNode(Opcodes.PUTFIELD, "some/type/Name", "fieldName", typeOfField) { @Override public AbstractInsnNode getPrevious() { return wrappingMethod; } }; CollectionTypeWrappedInUnmodifiableIdiomChecker checker = new CollectionTypeWrappedInUnmodifiableIdiomChecker( fieldInsnNode, Type.getType(List.class)); assertThat(checker.checkWrappedInUnmodifiable().safelyCopiesBeforeWrapping, is(true)); assertThat(checker.checkWrappedInUnmodifiable(), is(WRAPS_AND_COPIES_SAFELY)); }
From source file:org.mutabilitydetector.checkers.CollectionTypeWrappedInUnmodifiableIdiomCheckerTest.java
License:Apache License
@Test public void ignoresNodesWhichDoNotResultInDifferentCodeSemantics() throws Exception { final MethodInsnNode whitelistedCopyMethod = new MethodInsnNode(Opcodes.INVOKESTATIC, "java/util/ArrayList", "<init>", "(Ljava/util/Collection;)V"); final LabelNode label = new LabelNode(new Label()) { @Override//from w w w . j av a 2s .c o m public AbstractInsnNode getPrevious() { return whitelistedCopyMethod; } }; final LineNumberNode lineNumber = new LineNumberNode(42, new LabelNode()) { @Override public AbstractInsnNode getPrevious() { return label; } }; final MethodInsnNode wrappingMethod = new MethodInsnNode(Opcodes.INVOKESTATIC, "java/util/Collections", "unmodifiableList", "") { @Override public AbstractInsnNode getPrevious() { return lineNumber; } }; String typeOfField = "java/util/List"; FieldInsnNode fieldInsnNode = new FieldInsnNode(Opcodes.PUTFIELD, "some/type/Name", "fieldName", typeOfField) { @Override public AbstractInsnNode getPrevious() { return wrappingMethod; } }; CollectionTypeWrappedInUnmodifiableIdiomChecker checker = new CollectionTypeWrappedInUnmodifiableIdiomChecker( fieldInsnNode, Type.getType(List.class)); UnmodifiableWrapResult result = checker.checkWrappedInUnmodifiable(); assertThat(result.invokesWhitelistedWrapperMethod, is(true)); assertThat(result.safelyCopiesBeforeWrapping, is(true)); assertThat(result, is(WRAPS_AND_COPIES_SAFELY)); }
From source file:org.mutabilitydetector.checkers.CollectionTypeWrappedInUnmodifiableIdiomCheckerTest.java
License:Apache License
private CollectionTypeWrappedInUnmodifiableIdiomChecker checkerForPutfieldPrecededByCopyAndWrapMethods( String copyMethodOwner, String copyMethodName, String copyMethodDesc, String wrappingMethodName, String fieldType) {/*from w w w .j ava2 s .c om*/ final MethodInsnNode whitelistedCopyMethod = new MethodInsnNode(Opcodes.INVOKESTATIC, copyMethodOwner, copyMethodName, copyMethodDesc); final MethodInsnNode wrappingMethod = new MethodInsnNode(Opcodes.INVOKESTATIC, "java/util/Collections", wrappingMethodName, "") { @Override public AbstractInsnNode getPrevious() { return whitelistedCopyMethod; } }; FieldInsnNode fieldInsnNode = new FieldInsnNode(Opcodes.PUTFIELD, "some/type/Name", "fieldName", fieldType) { @Override public AbstractInsnNode getPrevious() { return wrappingMethod; } }; return new CollectionTypeWrappedInUnmodifiableIdiomChecker(fieldInsnNode, Type.getType(fieldType)); }
From source file:org.mutabilitydetector.checkers.CollectionTypeWrappedInUnmodifiableIdiomCheckerTest.java
License:Apache License
private CollectionTypeWrappedInUnmodifiableIdiomChecker checkerOfPutFieldPrecededByCall(String fieldType, String methodName, String unmodifiableMethodDesc) { final MethodInsnNode wrappingMethod = new MethodInsnNode(INVOKESTATIC, "java/util/Collections", methodName, unmodifiableMethodDesc);/*from ww w . j a v a 2 s. co m*/ FieldInsnNode fieldInsnNode = new FieldInsnNode(Opcodes.PUTFIELD, "some/type/Name", "fieldName", fieldType) { @Override public AbstractInsnNode getPrevious() { return wrappingMethod; } }; return new CollectionTypeWrappedInUnmodifiableIdiomChecker(fieldInsnNode, Type.getType(fieldType)); }
From source file:org.mutabilitydetector.checkers.CollectionTypeWrappedInUnmodifiableIdiomCheckerTest.java
License:Apache License
private FieldInsnNode putFieldForType(String fieldType) { return new FieldInsnNode(Opcodes.PUTFIELD, "some/type/assigning/Field", "fieldName", fieldType); }
From source file:org.mutabilitydetector.checkers.FieldAssignmentVisitor.java
License:Apache License
@Override public void visitFieldInsn(int opcode, String fieldsOwner, String fieldName, String fieldDesc) { super.visitFieldInsn(opcode, fieldsOwner, fieldName, fieldDesc); if (opcode == Opcodes.PUTFIELD) { fieldAssignments.add((FieldInsnNode) instructions.getLast()); }/*from w ww. j av a 2s . c om*/ }
From source file:org.mutabilitydetector.checkers.settermethod.EffectiveAssignmentInsnFinder.java
License:Apache License
private static boolean isPutfieldInstruction(final AbstractInsnNode insn) { return Opcodes.PUTFIELD == insn.getOpcode(); }
From source file:org.openquark.cal.internal.javamodel.AsmJavaBytecodeGenerator.java
License:Open Source License
/** * Add the <init> method for the given constructor. This also includes initializing the non-static fields that * have initializers./*w w w .j ava 2 s. c om*/ * @param classRep * @param javaConstructor * @param cv * @throws JavaGenerationException */ private static void encodeConstructor(JavaClassRep classRep, JavaConstructor javaConstructor, ClassVisitor cv) throws JavaGenerationException { // gather info on the thrown exceptions final int nThrownExceptions = javaConstructor.getNThrownExceptions(); String[] thrownExceptions = new String[nThrownExceptions]; for (int i = 0; i < nThrownExceptions; ++i) { thrownExceptions[i] = javaConstructor.getThrownException(i).getJVMInternalName(); } MethodVisitor mv = cv.visitMethod(javaConstructor.getModifiers(), "<init>", javaConstructor.getJVMMethodDescriptor(), null, thrownExceptions); final Map<String, JavaTypeName> methodVarToTypeMap = new HashMap<String, JavaTypeName>(); final Map<String, Integer> methodVarToIndexMap = new HashMap<String, Integer>(); methodVarToTypeMap.put("this", classRep.getClassName()); methodVarToIndexMap.put("this", Integer.valueOf(0)); int indexOfNextSlot = 1; for (int i = 0, nParams = javaConstructor.getNParams(); i < nParams; ++i) { JavaTypeName varType = javaConstructor.getParamType(i); final int typeSize = getTypeSize(varType); String varName = javaConstructor.getParamName(i); methodVarToTypeMap.put(varName, varType); methodVarToIndexMap.put(varName, Integer.valueOf(indexOfNextSlot)); indexOfNextSlot += typeSize; } // Start the method's code. mv.visitCode(); // Invoke the superclass initializer. if (javaConstructor.getSuperConstructorParamTypes().length == 0) { //push the "this" reference on the stack mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, classRep.getSuperclassName().getJVMInternalName(), "<init>", "()V"); } else { GenerationContext context = new GenerationContext(methodVarToTypeMap, methodVarToIndexMap, indexOfNextSlot, mv, classRep.getClassName()); MethodInvocation mi = new MethodInvocation.Instance(null, "<init>", classRep.getSuperclassName(), javaConstructor.getSuperConstructorParamValues(), javaConstructor.getSuperConstructorParamTypes(), JavaTypeName.VOID, MethodInvocation.InvocationType.SPECIAL); encodeMethodInvocationExpr(mi, context); } // Add initializers for the non-statically-initialized fields. final int nFields = classRep.getNFieldDeclarations(); for (int i = 0; i < nFields; ++i) { JavaFieldDeclaration fieldDecl = classRep.getFieldDeclaration(i); JavaExpression initializer = fieldDecl.getInitializer(); if (initializer != null && !Modifier.isStatic(fieldDecl.getModifiers())) { GenerationContext context = new GenerationContext(methodVarToTypeMap, methodVarToIndexMap, indexOfNextSlot, mv, classRep.getClassName()); //push the "this" reference mv.visitVarInsn(Opcodes.ALOAD, 0); //evaluate the initializer encodeExpr(initializer, context); //do the assignment mv.visitFieldInsn(Opcodes.PUTFIELD, classRep.getClassName().getJVMInternalName(), fieldDecl.getFieldName(), fieldDecl.getFieldType().getJVMDescriptor()); } } // Add the body code for the constructor GenerationContext context = new GenerationContext(methodVarToTypeMap, methodVarToIndexMap, indexOfNextSlot, mv, classRep.getClassName()); boolean isTerminating = encodeStatement(javaConstructor.getBodyCode(), context); // Add any implicit "return" statement. if (!isTerminating) { mv.visitInsn(Opcodes.RETURN); } //mark the end of encoding the constructor mv.visitMaxs(0, 0); // End the method. mv.visitEnd(); }