Compiling and throwing simple dynamic excepitons at runtime for JVM

I’ve been using my Dynamic Exception with C# for quite some time already and it saved me a lot of time. This means, I don’t have to create a new exception class for each and every case. I wanted to have the same functionality on Android and in kotlin/java so I can do this:

fun main() {     throw dynamicException("My", "Hallo exception!") // throws MyException } 

The DynamicException.kt file contains most of the code where the dynamicException function first initializes the source-code for the new exception by formatting a String then it uses the JavaCompiler to build the class and call the appropriate construtor. Either with or without the inner exception.

import import java.lang.reflect.Constructor import import import import java.util.* import import import import  fun dynamicException(name: String, message: String, inner: Throwable? = null): java.lang.Exception {     val javaCompiler = ToolProvider.getSystemJavaCompiler()     val diagnosticCollector = DiagnosticCollector<JavaFileObject>()      val values = TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER)     values["name"] = name     var sourceCode = SourceCodeJavaFileObject(         "com.he-dev.$  {name}Exception",         dynamicExceptionSourceCode.smartFormat(values)     )     javaCompiler.getTask(         null,         null,         diagnosticCollector,         null,         null,         arrayListOf(sourceCode)     ).call()      val classLoader = URLClassLoader.newInstance(arrayOf<URL>(File("").toURI().toURL()))      var getCtor: () -> Constructor<out Any> = {         val cls = Class.forName("$  {name}Exception", true, classLoader)         val ctor = if (inner == null) {             cls.getConstructor(         } else {             cls.getConstructor(,         }         ctor.makeAccessible()     }      return if (inner == null) {         getCtor().newInstance(message) as java.lang.Exception     } else {         getCtor().newInstance(message, inner) as java.lang.Exception     } }  fun Constructor<out Any>.makeAccessible(): Constructor<out Any> {     this.isAccessible = true     return this }   val dynamicExceptionSourceCode: String = """ public class {Name}Exception extends java.lang.Exception {     public {Name}Exception(java.lang.String message) {         super(message);     }     public {Name}Exception(java.lang.String message, java.lang.Throwable inner) {         super(message, inner);     } } """.trimIndent()  class SourceCodeJavaFileObject : SimpleJavaFileObject {     private val sourceCode: CharSequence      constructor(className: String, sourceCode: CharSequence) :             super(                 URI.create("string:///" + className.replace('.', '/') + JavaFileObject.Kind.SOURCE.extension),                 JavaFileObject.Kind.SOURCE             ) {         this.sourceCode = sourceCode     }      override fun getCharContent(ignoreEncodingErrors: Boolean): CharSequence {         return sourceCode     } } 

The string formatting is done with a string extension that can replace patterns. I based it on my C# formatter. However, it’s simpler because it doesn’t not support value formatting.

import java.util.*  fun String.smartFormat(values: TreeMap<String, String>): String {     val regex = Regex("""\{(?<name>[a-z][a-z0-9_.-]*)\}""", RegexOption.IGNORE_CASE)     return regex.replace(this) {         var key = it.groups["name"]?.value         if (values.containsKey(key)) values[key]!! else it.value     } } 

Is there anything that can be simplified or made even cleaner?

What is the best architecture in a microservices ecosystem for a dynamic page that its data is provided by other services?

How do we achieve loose coupling in the below scenario:

We have a web store which sells finite types of products (e.g. Movie, Music and Book) which we adopted a microservices architecture for it. The web has a different page for each type of products (One for movies, one for musics and one for books). We want to have separate teams which each focuses on one of these products and has all authority on its business concerns. The problem lays in the designing of home page. Our home page is a dynamic page which has different rows that each may contain one of these products. For example in a day we may have a “New Movies” row at top and then a “Most Selling Books” under it and another day first row is about musics and so on. Which service and which team should have control over the home page? Should we have a separate service (and probably a team by Conway’s Law) for it?


Is there a collision free Hash Function for dynamic data that preserves already created hashes?

I am familiar with the concept of hash functions and fingerprints but I am new to the implementation of functions and the specific characteristics of all those hash functions out there.

What I need

Say I have a table of employee data {FirstName, LastName, Birthday, …} which is dynamic. New employees join the the company, old employees leave. I want to hash the employee data. I need to make shure that the data of new employees is not hashed to the same value as any other employee that has ever been in the database.

In this sense the data set is append only. While deleting the employee data of an retiring employee is mandatory, I can store the hash, that was once linked to that employee. (Which most likely is of no use because the hash of past employees data will not evaluate to itself once hashed again 🙁 )

Hashing does not need to be cryptographic. Yet hashes must not easily be tracked back to employee data. By this I mean you are not supposed to use an efficient method to calculate employee data from hash values. Since information is lost in the process of hashing I would assume that this requirement is easy to match.

The Goal

I want to reduce the size of the hash (meaning bit size) as much as possible.

I would assume that, without collision resistance, I need to pick a fairly large hash (2^32 or bigger) to assure a tolerable risk of collision. Avoiding this is the main interest behind the question.

I must guarantee that never ever a new employees data is hashed to the same value as one of the prior employees data was hashed to. I can make assumption like “Given infinite time there will in total never be more then 1.000.000 people in the company or retired from the company.” So the total number space of hashes is fixed.

Is this solvable? If not, what would be the best hashing procedure that assures maximum collision resistance (Rabin’s Fingerprinting?)

How to store dynamic “select” values and names in a object for onchange event

I have a table of dynamic data which is echo’ed out on the webpage in a table format as shown in the picture


so the table will have a lot of select tags with different names and values depending upon how much (applications are there ) iteration takes place!

     foreach($  data['applicationList'] as $  applications)                 {                       echo '<td><select name='.$  applications['aid'].'>                         <option value=000>Reject All<option>                         <option value=111>Accept All<option>                         <option value=100>Accept 1st Choise<option>                         <option value=010>Accept 2nd Choise<option>                         <option value=001>Accept 3rd Choise<option>                         <option value=110>Accept 1st and 2nd Choise<option>                         <option value=011>Accept 2nd and 3rd Choise<option>                         <option value=101>Accept 1st and 3rd Choise<option>                         </select></td> 


Is there any way i could capture all the different selected values and names which are changed and bind them to an object of key value pair to send it to the controller using javascript or jquery ajax? Could someone help me on this!

Reference dynamic text from another document within Google Docs

I want to create a document that has ten pages in it (ten pages, each page a new set of unique information). I want that document to pull from ten unique, single page documents that users can update. I want the updates in the unique documents to populate in the conglomerate document.

Is this possible with Google Docs?

I have seen that dynamic text can be linked between Google Sheets, or between Google Sheets and Google Docs but I want to do dynamic text from Google Doc to Google Doc.

Given an array of size n, create a sub array with given cinditions using dynamic programming

We are given an array of integers of size n, which is not necessarily sorted, and we want to create a sub array. We are allowed to do one of 3 tasks below in each level: 1. skip the number in original array and go to the next 2. insert the number in the start of the sub array 3. insert the number in the end of the sub array.
And at last we want the sub array to be the longest one that can be created with the conditions above and sorted. For example we have A={10,3,13} now we can insert 10 in the beginning, skip 3 and insert 13 in the end. so the size of sub array is 2.

I know that I must solve this using dynamic programming but I do not know how to do it exactly.

Dynamic table not rendered in Firefox but rendered in Chrome

In my ASP.NET CORE 2 website, I have a table with dynamic table rows (tr elements). I noticed that my table was not rendered at all when I launched my page in Firefox (I was using Chrome to develop). Can someone point out what I need to do to fix my rendering issue in Firefox?

This is my Page:

<div class="workbook-container">     <table class="table">         <tbody>         @{             foreach (var workbook in Model.Workbooks)             {                 <tr>                     <td>@Html.ActionLink(workbook.Name, "Open", "OpenAnalytics", new {id = Model.Id, workbook = workbook.Name})</td>                     <td>                         <button title="Share" class="share-button" onclick='showSharingView("@workbook.Name")'>&nbsp;</button>                     </td>                 </tr>             }         }         </tbody>     </table> </div> 

Generated HTML: enter image description here

This is how the page looks in Chrome: enter image description here