List of usage examples for org.objectweb.asm Opcodes ASTORE
int ASTORE
To view the source code for org.objectweb.asm Opcodes ASTORE.
Click Source Link
From source file:org.mutabilitydetector.checkers.CollectionTypeWrappedInUnmodifiableIdiomCheckerTest.java
License:Apache License
@Test public void findsThatAssignmentDoesNotIfCallsNonWhitelistedMethodIfPreviousInstructionWasNotAMethodInvocation() throws Exception { final VarInsnNode varInsn = new VarInsnNode(Opcodes.ASTORE, 2); FieldInsnNode fieldInsnNode = new FieldInsnNode(Opcodes.PUTFIELD, "some/type/Name", "fieldName", "java/util/List") { @Override//from w w w . j a va 2 s . co m public AbstractInsnNode getPrevious() { return varInsn; } }; CollectionTypeWrappedInUnmodifiableIdiomChecker checker = new CollectionTypeWrappedInUnmodifiableIdiomChecker( fieldInsnNode, Type.getType(List.class)); assertThat(checker.checkWrappedInUnmodifiable().invokesWhitelistedWrapperMethod, is(false)); assertThat(checker.checkWrappedInUnmodifiable(), is(DOES_NOT_WRAP_USING_WHITELISTED_METHOD)); }
From source file:org.mutabilitydetector.checkers.CollectionTypeWrappedInUnmodifiableIdiomCheckerTest.java
License:Apache License
@Test public void findsThatAssignmentIsNotSafelyCopiedIfCallToUnmodifiableMethodIsNotPrecededByMethodInstruction() throws Exception { final VarInsnNode varInsn = new VarInsnNode(Opcodes.ASTORE, 2); final MethodInsnNode wrappingMethod = new MethodInsnNode(Opcodes.INVOKESTATIC, "java/util/Collections", "unmodifiableList", "") { @Override/*from w ww . j a v a2 s . c o m*/ public AbstractInsnNode getPrevious() { return varInsn; } }; 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.settermethod.EffectiveAssignmentInsnVerifier.java
License:Apache License
private static boolean isAliasStoreInstruction(final AbstractInsnNode insn, final int aliasLocalVariable) { switch (insn.getOpcode()) { case Opcodes.ISTORE: case Opcodes.LSTORE: case Opcodes.FSTORE: case Opcodes.DSTORE: case Opcodes.ASTORE: final VarInsnNode varInsnNode = (VarInsnNode) insn; return aliasLocalVariable == varInsnNode.var; default:/*from ww w . j a v a 2 s . com*/ return false; } }
From source file:org.openquark.cal.internal.javamodel.AsmJavaBytecodeGenerator.java
License:Open Source License
private static boolean encodeTryCatchStatement(JavaStatement.Block block, GenerationContext context) throws JavaGenerationException { MethodVisitor mv = context.getMethodVisitor(); List<JavaExceptionHandler> exceptionHandlers = block.getExceptionHandlers(); if (exceptionHandlers.isEmpty()) { throw new IllegalStateException(); }/*from w w w .java 2 s . c o m*/ // Spawn a child context for the try block. GenerationContext tryBlockContext = new GenerationContext(context); boolean isTerminating = false; // starts out false (for the case with no statements..) Label tryStartLabel = new Label(); mv.visitLabel(tryStartLabel); // Append the instructions comprising the block's component statements. int nStatements = block.getNBlockStatements(); for (int i = 0; i < nStatements; i++) { JavaStatement blockStatement = block.getNthBlockStatement(i); // skip comments. if (blockStatement instanceof Comment) { continue; } // is terminating if the last statement is terminating. isTerminating = encodeStatement(blockStatement, tryBlockContext); context.addJumpReturnLabelInfo(tryBlockContext); } // Add an instruction to jump to after the exception handlers, if the statement block isn't terminating. Label tryCatchEndLabel = new Label(); context.addStatementJumpLabel(tryCatchEndLabel); if (!isTerminating) { mv.visitJumpInsn(Opcodes.GOTO, tryCatchEndLabel); } Label tryEndLabel = new Label(); mv.visitLabel(tryEndLabel); // Add exception handlers as necessary. // Now set handlers to handle any exceptions. for (int i = 0, nExceptionHandlers = exceptionHandlers.size(); i < nExceptionHandlers; ++i) { JavaExceptionHandler eh = exceptionHandlers.get(i); JavaTypeName exceptionType = JavaTypeName.make(eh.getExceptionClass()); // exception class is a non-array reference type. //encode the start of the catch block. Label catchLabel = new Label(); mv.visitLabel(catchLabel); // Create a child context for the catch block. GenerationContext catchContext = new GenerationContext(context); // The thrown exception object will be the only item on the stack. // Store it into a new local variable. String exceptionVarName = eh.getExceptionVarName(); int exceptionVarIndex = catchContext.addLocalVar(exceptionVarName, exceptionType); mv.visitVarInsn(Opcodes.ASTORE, exceptionVarIndex); boolean catchIsTerminating = encodeStatement(eh.getHandlerCode(), catchContext); context.addJumpReturnLabelInfo(catchContext); if (!catchIsTerminating) { mv.visitJumpInsn(Opcodes.GOTO, tryCatchEndLabel); } // In the end, we're only terminating if all the catch blocks are terminating. isTerminating &= catchIsTerminating; //encode the try/catch block. This can be done in any order, any time after all labels passed as arguments have been visited, // between visitCode() and visitMaxs(). encodeTryCatchBlock(tryStartLabel, tryEndLabel, catchLabel, exceptionType, tryBlockContext); } //mark the end of the whole try/catch code mv.visitLabel(tryCatchEndLabel); return isTerminating; }
From source file:org.openquark.cal.internal.javamodel.AsmJavaBytecodeGenerator.java
License:Open Source License
/** * Encode a method invocation that is wrapped in a synchronized block. * See Sun bug id #4414101 for a discussion of this generated code. * /* ww w .ja v a2 s. co m*/ * @param smi - the SynchronizedMethodInvocation object. * @param context - the context of the code generation. * @return - true if the SynchronizedMethodInvocation is terminating. * @throws JavaGenerationException */ private static boolean encodeSynchronizedMethodInvocation(JavaStatement.SynchronizedMethodInvocation smi, GenerationContext context) throws JavaGenerationException { MethodVisitor mv = context.getMethodVisitor(); // Get the JavaExpression for the object to synchronize on and generate the bytecode. JavaExpression objectToSynchOn = smi.getSynchronizingObject(); encodeExpr(objectToSynchOn, context); // The object to synchronize on is now on the stack. Duplicate it, // move one to storage as a local variable, and to MONITORENTER on the other. mv.visitInsn(Opcodes.DUP); int mutexIndex = context.addLocalVar("$mutex", JavaTypeName.OBJECT); mv.visitVarInsn(Opcodes.ASTORE, mutexIndex); mv.visitInsn(Opcodes.MONITORENTER); // We need to wrap the method invocation in a try/catch block so // the monitor can be exited properly if the method invocation throws // an exception. Label tryCatchStart = new Label(); mv.visitLabel(tryCatchStart); // Note: if this is generalized to handle any synchronized statement (for example, a synchronized block), // then the scope of entries in the exception table here will have to be modified to exclude return instructions. // See encodeTryCatchStatement() for how to do this. // Here, the only statement in the try block is a single expressionStatement, which has no return instructions, // so we don't have to worry about handling this case. // Get the method invocation and generate the corresponding bytecode. MethodInvocation methodInvocation = smi.getMethodInvocation(); encodeExpr(methodInvocation, context); // Load the mutex object back onto the stack and do MonitorExit. mv.visitVarInsn(Opcodes.ALOAD, mutexIndex); mv.visitInsn(Opcodes.MONITOREXIT); // Label the end of the try block around the method invocation. Label tryEnd = new Label(); mv.visitLabel(tryEnd); // At this point we want to code an instruction to jump past the exception handling // code. Label tryCatchEnd = new Label(); mv.visitJumpInsn(Opcodes.GOTO, tryCatchEnd); // Label the start of the exception handling code. Label catchStart = new Label(); mv.visitLabel(catchStart); // The exception is on the stack. Store it as a local. int exceptionIndex = context.addLocalVar("exception", JavaTypeName.OBJECT); mv.visitVarInsn(Opcodes.ASTORE, exceptionIndex); // Retrieve the mutex object and do MONITOREXIT. mv.visitVarInsn(Opcodes.ALOAD, mutexIndex); mv.visitInsn(Opcodes.MONITOREXIT); // Label the end of the exception handling code that deals with the monitor. Label exceptionMonitorExitEnd = new Label(); mv.visitLabel(exceptionMonitorExitEnd); // Retrieve the exception and throw it. mv.visitVarInsn(Opcodes.ALOAD, exceptionIndex); mv.visitInsn(Opcodes.ATHROW); // Vist the label to mark the end of the try/catch. mv.visitLabel(tryCatchEnd); // Set up the try/catch block to catch exceptions thrown by the method invocation // and handle exiting the monitor. mv.visitTryCatchBlock(tryCatchStart, tryEnd, catchStart, null); // Set up a try catch block so that if an exception is thrown by trying to exit // the monitor in the case of an exception from the method invocation we will keep trying to // exit the monitor. mv.visitTryCatchBlock(catchStart, exceptionMonitorExitEnd, catchStart, null); return false; }
From source file:org.openquark.cal.internal.javamodel.AsmJavaBytecodeGenerator.java
License:Open Source License
private static int getStoreOpCode(JavaTypeName varType) { //ASM automatically handles replacing ISTORE 0, ISTORE 1, ISTORE 2 and ISTORE 3 by the special //0 argument op codes ISTORE_0, ISTORE_1, ISTORE_2, and ISTORE_3 and similarly for the other //types./*from w w w. j a v a 2 s . c om*/ switch (varType.getTag()) { case JavaTypeName.VOID_TAG: throw new IllegalArgumentException("Cannot load a local variable of void type."); case JavaTypeName.BOOLEAN_TAG: case JavaTypeName.BYTE_TAG: case JavaTypeName.SHORT_TAG: case JavaTypeName.CHAR_TAG: case JavaTypeName.INT_TAG: return Opcodes.ISTORE; case JavaTypeName.LONG_TAG: return Opcodes.LSTORE; case JavaTypeName.DOUBLE_TAG: return Opcodes.DSTORE; case JavaTypeName.FLOAT_TAG: return Opcodes.FSTORE; case JavaTypeName.ARRAY_TAG: case JavaTypeName.OBJECT_TAG: return Opcodes.ASTORE; default: { throw new IllegalArgumentException("Cannot load a local variable of type " + varType); } } }
From source file:org.sonar.java.bytecode.se.BytecodeEGWalkerExecuteTest.java
License:Open Source License
@Test public void test_store() throws Exception { int[] storeOpcodes = new int[] { Opcodes.ISTORE, Opcodes.LSTORE, Opcodes.FSTORE, Opcodes.DSTORE, Opcodes.ASTORE }; SymbolicValue sv = new SymbolicValue(); ProgramState startState = ProgramState.EMPTY_STATE.stackValue(sv); for (int opcode : storeOpcodes) { ProgramState programState = execute(new Instruction(opcode, 67), startState); assertThat(programState.getValue(67)).isEqualTo(sv); }//from w w w .ja v a 2 s .co m }
From source file:org.sonar.plugins.monitor.agent.transform.OpenInterceptionAdapter.java
License:Open Source License
@Override public void visitMethodInsn(int opcode, String owner, String name, String desc) { if (!toIntercept(owner, name)) { // no processing super.visitMethodInsn(opcode, owner, name, desc); return;//from w w w . j av a 2 s .c om } Type exceptionType = Type.getType(getExpectedException()); CodeGenerator g = new CodeGenerator(mv); Label s = new Label(); // start of the try block Label e = new Label(); // end of the try block Label h = new Label(); // handler entry point Label tail = new Label(); // where the execution continue g.visitTryCatchBlock(s, e, h, exceptionType.getInternalName()); g.visitLabel(s); super.visitMethodInsn(opcode, owner, name, desc); g._goto(tail); g.visitLabel(e); g.visitLabel(h); // [RESULT] // catch(E ex) { // boolean b = ex.getMessage().contains("Too many open files"); int ex = lvs.newLocal(exceptionType); g.dup(); base.visitVarInsn(Opcodes.ASTORE, ex); g.invokeVirtual(exceptionType.getInternalName(), "getMessage", "()Ljava/lang/String;"); g.ldc("Too many open files"); g.invokeVirtual("java/lang/String", "contains", "(Ljava/lang/CharSequence;)Z"); // too many open files detected // if (b) { Listener.outOfDescriptors() } Label rethrow = new Label(); g.ifFalse(rethrow); g.invokeAppStatic(Listener.class, "outOfDescriptors", new Class[0], new int[0]); // rethrow the FileNotFoundException g.visitLabel(rethrow); base.visitVarInsn(Opcodes.ALOAD, ex); g.athrow(); // normal execution continues here g.visitLabel(tail); }
From source file:org.spongepowered.api.util.generator.dummy.DummyClassGenerator.java
License:MIT License
private void generateMethods(ClassWriter cw, String internalName, List<Method> methods, Class<?> exceptionType) { for (Method method : methods) { MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, method.getName(), Type.getMethodDescriptor(method), null, null);//from w w w . j a v a2 s .c o m mv.visitCode(); String internalException = Type.getInternalName(exceptionType); mv.visitTypeInsn(NEW, internalException); mv.visitInsn(DUP); // Load first argument to String.format mv.visitLdcInsn( "A method was invoked on a dummy class, due to the static field %s not being initialized " + "(most likely in a CatalogType-related class).\n" + "Method: " + method); // Create new array mv.visitLdcInsn(1); mv.visitTypeInsn(ANEWARRAY, "java/lang/String"); // Store in local var 2 mv.visitVarInsn(Opcodes.ASTORE, 2); // AASTORE part 1 - array reference mv.visitVarInsn(Opcodes.ALOAD, 2); // AASTORE part 2 - array index mv.visitLdcInsn(0); // AASTORE part 3 = the actual value // Load formatter argument (the field FIELD_NAME) mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, internalName, FIELD_NAME, Type.getDescriptor(String.class)); // Store in array mv.visitInsn(AASTORE); // Load array reference mv.visitVarInsn(ALOAD, 2); // Call String.format mv.visitMethodInsn(INVOKESTATIC, Type.getInternalName(String.class), "format", "(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;", false); // Invoke throwable constructor mv.visitMethodInsn(INVOKESPECIAL, internalException, "<init>", "(Ljava/lang/String;)V", false); mv.visitInsn(ATHROW); mv.visitMaxs(0, 0); mv.visitEnd(); } }
From source file:org.spongepowered.asm.mixin.injection.callback.CallbackInjector.java
License:MIT License
/** * @param callback callback handle/* w w w. j a v a 2s. c om*/ */ private void createCallbackInfo(final Callback callback) { this.dupReturnValue(callback); callback.add(new TypeInsnNode(Opcodes.NEW, callback.target.callbackInfoClass), true, false); callback.add(new InsnNode(Opcodes.DUP), true, true); this.invokeCallbackInfoCtor(callback); callback.add(new VarInsnNode(Opcodes.ASTORE, callback.marshallVar)); }