private static ThrowUnchecked generateImpl() { RuntimeClassFile cf = new RuntimeClassFile(null, ThrowUnchecked.class.getName()); cf.addDefaultConstructor(); CodeBuilder b = new CodeBuilder (cf.addMethod(Modifiers.PROTECTED, "doFire", null, new TypeDesc[] {TypeDesc.forClass(Throwable.class)})); b.loadLocal(b.getParameter(0)); b.throwObject(); try { return (ThrowUnchecked) cf.defineClass().newInstance(); } catch (Exception e) { throw new Error(e); } }
private static ThrowUnchecked generateImpl() { RuntimeClassFile cf = new RuntimeClassFile(null, ThrowUnchecked.class.getName()); cf.addDefaultConstructor(); CodeBuilder b = new CodeBuilder (cf.addMethod(Modifiers.PROTECTED, "doFire", null, new TypeDesc[] {TypeDesc.forClass(Throwable.class)})); b.loadLocal(b.getParameter(0)); b.throwObject(); try { return (ThrowUnchecked) cf.defineClass().newInstance(); } catch (Exception e) { throw new Error(e); } }
/** * * @param opType type of operation, Insert, Update, or Delete */ private void addExitTransaction(CodeBuilder b, String opType, LocalVariable txnVar, Label tryStart) { if (tryStart == null) { addExitTransaction(b, opType, txnVar); return; } // } catch (... e) { // txn.exit(); // throw e; // } Label tryEnd = b.createLabel().setLocation(); b.exceptionHandler(tryStart, tryEnd, null); addExitTransaction(b, opType, txnVar); b.throwObject(); }
/** * * @param opType type of operation, Insert, Update, or Delete */ private void addExitTransaction(CodeBuilder b, String opType, LocalVariable txnVar, Label tryStart) { if (tryStart == null) { addExitTransaction(b, opType, txnVar); return; } // } catch (... e) { // txn.exit(); // throw e; // } Label tryEnd = b.createLabel().setLocation(); b.exceptionHandler(tryStart, tryEnd, null); addExitTransaction(b, opType, txnVar); b.throwObject(); }
private static void throwIllegalArgException(CodeBuilder b, String message, LocalVariable concatStr) { TypeDesc exceptionType = TypeDesc.forClass(IllegalArgumentException.class); TypeDesc[] params = {TypeDesc.STRING}; b.newObject(exceptionType); b.dup(); b.loadConstant(message); b.loadLocal(concatStr); b.invokeVirtual(TypeDesc.STRING, "concat", TypeDesc.STRING, params); b.invokeConstructor(exceptionType, params); b.throwObject(); }
/** * Generate code to throw an exception with an optional message. * @param b {@link CodeBuilder} to which to add code * @param type type of the object to throw * @param message optional message to provide to the constructor */ public static void throwException(CodeBuilder b, Class type, String message) { TypeDesc desc = TypeDesc.forClass(type); b.newObject(desc); b.dup(); if (message == null) { b.invokeConstructor(desc, null); } else { b.loadConstant(message); b.invokeConstructor(desc, new TypeDesc[] {TypeDesc.STRING}); } b.throwObject(); }
private static void throwIllegalArgException(CodeBuilder b, String message, LocalVariable concatStr) { TypeDesc exceptionType = TypeDesc.forClass(IllegalArgumentException.class); TypeDesc[] params = {TypeDesc.STRING}; b.newObject(exceptionType); b.dup(); b.loadConstant(message); b.loadLocal(concatStr); b.invokeVirtual(TypeDesc.STRING, "concat", TypeDesc.STRING, params); b.invokeConstructor(exceptionType, params); b.throwObject(); }
/** * Generate code to throw an exception with an optional message. * @param b {@link CodeBuilder} to which to add code * @param type type of the object to throw * @param message optional message to provide to the constructor */ public static void throwException(CodeBuilder b, Class type, String message) { TypeDesc desc = TypeDesc.forClass(type); b.newObject(desc); b.dup(); if (message == null) { b.invokeConstructor(desc, null); } else { b.loadConstant(message); b.invokeConstructor(desc, new TypeDesc[] {TypeDesc.STRING}); } b.throwObject(); }
/** * Generate code to throw an exception with a message concatenated at runtime. * * @param b {@link CodeBuilder} to which to add code * @param type type of the object to throw * @param messages messages to concat at runtime */ public static void throwConcatException(CodeBuilder b, Class type, String... messages) { if (messages == null || messages.length == 0) { throwException(b, type, null); return; } if (messages.length == 1) { throwException(b, type, messages[0]); return; } TypeDesc desc = TypeDesc.forClass(type); b.newObject(desc); b.dup(); TypeDesc[] params = new TypeDesc[] {TypeDesc.STRING}; for (int i=0; i<messages.length; i++) { b.loadConstant(String.valueOf(messages[i])); if (i > 0) { b.invokeVirtual(TypeDesc.STRING, "concat", TypeDesc.STRING, params); } } b.invokeConstructor(desc, params); b.throwObject(); }
private void checkIfPartitionKeyPresent(CodeBuilder b) { b.loadThis(); b.invokeVirtual(StorableGenerator.IS_PARTITION_KEY_INITIALIZED_METHOD_NAME, TypeDesc.BOOLEAN, null); Label ptnkInitialized = b.createLabel(); b.ifZeroComparisonBranch(ptnkInitialized, "!="); TypeDesc exType = TypeDesc.forClass(IllegalStateException.class); b.newObject(exType); b.dup(); b.loadConstant("Partition key not fully specified"); b.invokeConstructor(exType, new TypeDesc[] {TypeDesc.STRING}); b.throwObject(); ptnkInitialized.setLocation(); }
private void checkIfPartitionKeyPresent(CodeBuilder b) { b.loadThis(); b.invokeVirtual(StorableGenerator.IS_PARTITION_KEY_INITIALIZED_METHOD_NAME, TypeDesc.BOOLEAN, null); Label ptnkInitialized = b.createLabel(); b.ifZeroComparisonBranch(ptnkInitialized, "!="); TypeDesc exType = TypeDesc.forClass(IllegalStateException.class); b.newObject(exType); b.dup(); b.loadConstant("Partition key not fully specified"); b.invokeConstructor(exType, new TypeDesc[] {TypeDesc.STRING}); b.throwObject(); ptnkInitialized.setLocation(); }
/** * Generates code which emulates this: * * ... * } finally { * JDBCConnectionCapability.yieldConnection(con); * } * * @param capVar required reference to JDBCConnectionCapability * @param conVar optional connection variable * @param tryAfterCon label right after connection acquisition */ private void yieldCon (CodeBuilder b, LocalVariable capVar, LocalVariable conVar, Label tryAfterCon) { Label endFinallyLabel = b.createLabel().setLocation(); Label contLabel = b.createLabel(); yieldConnection(b, capVar, conVar); b.branch(contLabel); b.exceptionHandler(tryAfterCon, endFinallyLabel, null); yieldConnection(b, capVar, conVar); b.throwObject(); contLabel.setLocation(); }
/** * Generates code which emulates this: * * ... * } finally { * JDBCConnectionCapability.yieldConnection(con); * } * * @param capVar required reference to JDBCConnectionCapability * @param conVar optional connection variable * @param tryAfterCon label right after connection acquisition */ private void yieldCon (CodeBuilder b, LocalVariable capVar, LocalVariable conVar, Label tryAfterCon) { Label endFinallyLabel = b.createLabel().setLocation(); Label contLabel = b.createLabel(); yieldConnection(b, capVar, conVar); b.branch(contLabel); b.exceptionHandler(tryAfterCon, endFinallyLabel, null); yieldConnection(b, capVar, conVar); b.throwObject(); contLabel.setLocation(); }
/** * Assumes a boolean is on the stack, as returned by doTryInsert or doTryUpdate. */ private void addNormalizationRollback(CodeBuilder b, Label doTryStart, List<PropertyCopy> unnormalized) { if (unnormalized == null) { return; } Label doTryEnd = b.createLabel().setLocation(); b.dup(); Label success = b.createLabel(); b.ifZeroComparisonBranch(success, "!="); for (int i=0; i<2; i++) { if (i == 0) { } else { b.exceptionHandler(doTryStart, doTryEnd, null); } // Rollback normalized properties. for (PropertyCopy copy : unnormalized) { copy.restore(b); } if (i == 0) { b.branch(success); } else { b.throwObject(); } } success.setLocation(); }
/** * Generates code that loads a Storage instance on the stack, throwing a * FetchException if Storage request fails. * * @param type type of Storage to request */ private void loadStorageForFetch(CodeBuilder b, TypeDesc type) { b.loadThis(); b.loadField(SUPPORT_FIELD_NAME, mSupportType); TypeDesc storageType = TypeDesc.forClass(Storage.class); TypeDesc repositoryType = TypeDesc.forClass(Repository.class); b.invokeInterface (mSupportType, "getRootRepository", repositoryType, null); b.loadConstant(type); // This may throw a RepositoryException. Label tryStart = b.createLabel().setLocation(); b.invokeInterface(repositoryType, STORAGE_FOR_METHOD_NAME, storageType, new TypeDesc[]{TypeDesc.forClass(Class.class)}); Label tryEnd = b.createLabel().setLocation(); Label noException = b.createLabel(); b.branch(noException); b.exceptionHandler(tryStart, tryEnd, RepositoryException.class.getName()); b.invokeVirtual (RepositoryException.class.getName(), "toFetchException", TypeDesc.forClass(FetchException.class), null); b.throwObject(); noException.setLocation(); }
/** * Generates code which emulates this: * * ... * } finally { * rs.close(); * } * * @param rsVar ResultSet variable * @param tryAfterRs label right after ResultSet acquisition */ private void closeResultSet (CodeBuilder b, LocalVariable rsVar, Label tryAfterRs) { Label contLabel = b.createLabel(); Label endFinallyLabel = b.createLabel().setLocation(); b.loadLocal(rsVar); b.invokeInterface(TypeDesc.forClass(ResultSet.class), "close", null, null); b.branch(contLabel); b.exceptionHandler(tryAfterRs, endFinallyLabel, null); b.loadLocal(rsVar); b.invokeInterface(TypeDesc.forClass(ResultSet.class), "close", null, null); b.throwObject(); contLabel.setLocation(); }
/** * Generates code which emulates this: * * ... * } finally { * statement.close(); * } * * @param statementVar Statement variable * @param tryAfterStatement label right after Statement acquisition */ private void closeStatement (CodeBuilder b, LocalVariable statementVar, Label tryAfterStatement) { Label contLabel = b.createLabel(); Label endFinallyLabel = b.createLabel().setLocation(); b.loadLocal(statementVar); b.invokeInterface(TypeDesc.forClass(Statement.class), "close", null, null); b.branch(contLabel); b.exceptionHandler(tryAfterStatement, endFinallyLabel, null); b.loadLocal(statementVar); b.invokeInterface(TypeDesc.forClass(Statement.class), "close", null, null); b.throwObject(); contLabel.setLocation(); }
/** * Generates code which emulates this: * * ... * } finally { * rs.close(); * } * * @param rsVar ResultSet variable * @param tryAfterRs label right after ResultSet acquisition */ private void closeResultSet (CodeBuilder b, LocalVariable rsVar, Label tryAfterRs) { Label contLabel = b.createLabel(); Label endFinallyLabel = b.createLabel().setLocation(); b.loadLocal(rsVar); b.invokeInterface(TypeDesc.forClass(ResultSet.class), "close", null, null); b.branch(contLabel); b.exceptionHandler(tryAfterRs, endFinallyLabel, null); b.loadLocal(rsVar); b.invokeInterface(TypeDesc.forClass(ResultSet.class), "close", null, null); b.throwObject(); contLabel.setLocation(); }
/** * Generates code which emulates this: * * ... * } finally { * statement.close(); * } * * @param statementVar Statement variable * @param tryAfterStatement label right after Statement acquisition */ private void closeStatement (CodeBuilder b, LocalVariable statementVar, Label tryAfterStatement) { Label contLabel = b.createLabel(); Label endFinallyLabel = b.createLabel().setLocation(); b.loadLocal(statementVar); b.invokeInterface(TypeDesc.forClass(Statement.class), "close", null, null); b.branch(contLabel); b.exceptionHandler(tryAfterStatement, endFinallyLabel, null); b.loadLocal(statementVar); b.invokeInterface(TypeDesc.forClass(Statement.class), "close", null, null); b.throwObject(); contLabel.setLocation(); }