Friday, November 22, 2019

[Stop, Pause, Re-Start] Future Topcs on this Blog!

This Blog has been Silent for a Long Time (Actually, I Wrote a Blog on 03-Jul-2018 Titled "Future Topics on this Blog", But I Could Never Write any Entries as per the Plan Mentioned There). The Most Active Time Period on the Blog were the Year(s) 2014, 2015, 2016, 2017. 

Beyond this, I ran my Own Startup with the Name Vajrin Software, Bangalore. The Startup Could Not Succeed and I Moved to a Senior Architect (Java/Java EE) Position in a IT Products Company in Philippines via a Staffing Firm, in the Travel Domain. After a Very Short Stay, I was Back as a Head of Product Engineering in a Bangalore Software Company in the Employee Transportation Domain. From February 2017 to July 2019, I also traveled to Malaysia, Nepal, Bhutan, Thailand (2 Times), Myanmar, Singapore and Indonesia/Bali. The Above is the Career Story from February 2017 to July 2019.

Taking a Job Break and Having Lost 20 Kgs of Body Weight from June 2019 to November 2019, I have joined as a Senior Product Development Manager at Ultria Software Private Limited, Bangalore. With Last 7 Designations/Roles the String Arc  It is a Small Sized Firm with Big Customer Names, Just Like my Previous Employer. Hope to be Stable Now in this Position! Still on my Way to Lose Another 10 Kgs to be Truly Agile! :-) ;-)
 
This Blog will now really have Regular Updates and Entries from abut December 2019. I wanted to give a heads-up to all, the Topics on which I plan to Blog in the next one year. These Blog Entries will arise out of my Daily Office Work, Professional Certification Preparation (Big Data and Hadoop Architect Master's), Udemy Courses and Self-Learning of Various Core Java and Jakarta EE Topics. Also, Few of my Articles may Get Re-Published in Popular Developer Sites like DZone, Ulitzer and Java Code Geek. The Crux of the Entries will be in Microservices, Spring Boot, Software Architecture, Java 9 Concurrency, Java 9 Modularity, Java 10/11/12/13. With Around 15.5+ Years of Professional Software Experience, I will start delving into User Interface with Angular 8, Node.js. Even though late into the world of Apache Kafka, NoSQL, MongoDB world, I hope that my Articles Count! I also will write articles in Machine Learning, Innovative Product Design and Development, Intelligent Agents and Data Mining. I am heavily invested into Udemy, with a Target to Complete 14 Course Titles in about 3 Months!

So, The Most Probable List (Subset of Below) of Topics in the Coming Future:
  • Microservices Architecture (Architect, Developer)
  • Java 8 : Lambdas (Developer)
  • Master Lambdas in Java 8 (Advanced Developer) 
  • Java 9 : Modular Programming (Developer)
  • Java 9 : Concurrency (Developer)
  • Java 9 : Advanced Concurrency (Advanced Developer)
  • MongoDB : NoSQL from Scratch (Developer)
  • Apache Kafka for Beginners (Developer)
  • Java 8, 9, 10, 11, 12 (Architect, Developer, Manager)
  • Angular 8 - Complete Guide (UI Initiation)
  • Model Driven Architecture (Architect)
  • AWS Fargate, ECS, Spring Boot (Developer)
  • Kubernetes for Beginner (DevOps)
  • Microservices with Spring Cloud (Developer)

Also, The Future Blog Entries (Subset of Below) will be From Below Topics: 
  • Introduction to Big Data and Hadoop Ecosystem (Architect, Developer)
  • Introduction to Docker (Developer)
  • Introduction to Apache Spark and Scala (Developer)
  • Big Data and Hadoop Administration (Architect's Point of View)
  • Introduction to Apache Storm (Developer)
  • Introduction to Apache Kafka (Developer)
  • Introduction to Apache Cassandra (Developer)
  • Are Professional Certifications worth the Effort? (Professional)
  • Introduction to Machine Learning with Java (Developer)
  • Appreciation of the Java EE 6.0 Release (Architect, Developer)
  • Appreciation of the Java EE 7.0 Release (Architect, Developer)
  • Appreciation of the Java EE 8.0 Release  (Architect, Developer)
  • Java SE 9... What's New? (Code Samples) (Architect, Developer)
  • Java SE 10... What's New? (Architect, Developer)
  • Java SE and Java EE Roadmap and Versioning Strategy (Architect)
  • Functional Programming with Java (Code Samples) (Developer)
  • NoSQL, NewSQL and OldSQL - Needs and Motivations (Developer)
  • Appreciation of Service Oriented Architecture [SOA] (Architect)
  • Reusable Software Components (Java/Jakarta EE)
  • Understanding/Detecting the OWASP Top 10 (Developer)
  • SQL and Database/RDBMS Optimization (Developer)
  • SKP's Hardware/Server Sizing Calculator for Enterprise Java (Architect)
  • Innovative Software Product Design & Development - 02/10 (Product Development)

I hope to add few more to the above list as time goes on. I hope that many of the articles are selected and re-published on DZone, Java Code Geek and Ulitzer, so that many more in the Java Development Community benefit from my knowledge. 

The first blog entry from the above list should be here by December 2019. Until then, Happy Times with Core Java and Jakarta EE! 


Monday, September 24, 2018

Java SE 9... What's New? [Code Samples - 02/02]


As I had promised, albeit very late, I am back with the Code Samples of my earlier article Java SE 9... What's New? You may refer the earlier article to understand the newly introduced features at a High Level. This article provides the Concluding Part of Code Samples for each of the Features. You may refer to the First Part of this Article at Java SE 9... What's New? [Code Samples - 01/02]

You may Download the Code Samples for the following New Features Here. (Import as Eclipse Project, Set Compiler/Environment as Java 9. Run the Main Class 'Java9Application' to see the Output/Outcome of the Code Samples.)


B. Core Library Changes in Java 9 (Continued)

09. Stack-Walking API
Prior to Java 9, The way to access Stack Trace was very limited and provided the entire dump or stack information at once. This was inefficient and did not allow any direct way of filtering data. With Java 9, a Lazy StackWalker API has been introducedThis will allow to fetch data based on filtering conditions and is more efficient. 
 package com.techilashots.java9.features;  
 import java.lang.StackWalker.Option;  
 import java.util.List;  
 import java.util.stream.Collectors;  

 /**  
  * Stack Walker API is a new feature of Java 9, aimed at Improving Performance of the predecessor Stack Track Element,  
  * as also for providing a way to filter the Stack Elements, in case of Exception or to Understand Application Behavior.   
  * Although, there have been multiple changes, I am covering only Stack Frame Attributes and also the walk() method   
  * for Walking the Stack Frame.   
  */  
 public class StackWalkingService { 
 
      private int databaseService() {  

           int x = 3;  

           // Usage 01: Walking All Stack Frames  
           System.out.println("Java 9 Stack Walker API - Showing All Stack Frames");  
           StackWalker stackWalker = StackWalker.getInstance();  
           stackWalker.forEach(System.out::println);  
           System.out.println("");  

           // Usage 02 : Filtering or Walking Stack Frames  
           System.out.println("Java 9 Stack Walker API - Walking / Filtering Stack Frames");  
           List<StackWalker.StackFrame> stackFrames;  
           stackFrames = stackWalker.walk(frames -> frames.limit(2).collect(Collectors.toList()));  
           stackFrames.forEach(System.out::println);  
           System.out.println("");  

           // Usage 03 : Show All Attributes of a Stack Frame  
           System.out.println("Java 9 Stack Walker API - Show All Attributes in Stack Frame");  
           StackWalker newWalker = StackWalker.getInstance(Option.RETAIN_CLASS_REFERENCE);  
           stackFrames = newWalker.walk(frames -> frames.limit(1).collect(Collectors.toList()));  
           stackFrames.forEach(sfRecord->   
           {  
                System.out.printf("[Bytecode Index] %d%n", sfRecord.getByteCodeIndex());  
                System.out.printf("[Class Name] %s%n", sfRecord.getClassName());  
                System.out.printf("[Declaring Class] %s%n", sfRecord.getDeclaringClass());  
                System.out.printf("[File Name] %s%n", sfRecord.getFileName());  
                System.out.printf("[Method Name] %s%n", sfRecord.getFileName());  
                System.out.printf("[Is Native] %b%n", sfRecord.isNativeMethod());  
                System.out.printf("[Line Number] %d%n", sfRecord.getLineNumber());  
           });  
           return x;  
      }  

      private float persistenceService() {  
           float x = databaseService();  
           return x;  
      }  

      private double businessService() {  
           double x = persistenceService();  
           return x;  
      }  

      private double presentationService() {  
           long x = (long) businessService();  
           return x;  
      }  

      public void uiDisplay() {  
           System.out.println("Java 9 Stack Walker API for Debugging and Application Behavior");  
           double x = presentationService();  
           System.out.println("\n[Method to Display On User Interface]");  
           System.out.println("Old MacDonald had a Farm. In that Farm, He had " + x + " Cows!");  
      }  

      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           StackWalkingService stackWalkingService = new StackWalkingService();  
           stackWalkingService.uiDisplay();  
      }  
 }  




10. Compact Strings
Although this has no external ramification to a developer in terms of syntax or semantics change - It may impact the way we design for memory and performance. The current UTF-16 representation uses 2 Bytes for Storage. Most of the string contain characters that are only Latin-1 in nature. The Latin-1 characters require only 1 Byte for Storage. With Java 9, String storage has been modified to start with an Additional Encoding Flag. This flag indicates whether it contains ISO-8859-1/Latin-1 characters or the UTF-16 characters. As per the official word, It has lead to an Improved Usage of Memory and Efficient GC, but with Some Loss in Performance at Peak Loads.

The Compact Strings are always enabled in Java 9, but it can be disabled by passing in the VM Argument +XX:-CompactStrings

It has to be noted that in Java 9,  the implementation of java.lang.String decides at runtime, whether the storage size is to be 2 Bytes or 1 Byte, as per the actual String size (UTF-16 or Latin-1 Character).


11. Spin-Wait Hints
For multi-threading applications, this brings in some performance improvements under Busy-Waiting or Spin-Waiting conditions. Usually, Busy-Waiting is done for synchronization of some state of the object between two or more invokers - Waiting for a condition to occur before processing starts or continues. Thread.onSpinWait() has been introduced as a static method in the Thread class and can be optionally called in Busy-Waiting loops - This will allow the JVM to issue processor instructions on some system architectures to improve Reaction Time in such Spin-Wait Loops and also Reduce the Power Consumed by the Core Thread or Hardware thread. This benefits the Overall Power Consumption of a Program, and possibly Allowing other Cores or Hardware Threads to execute at Faster Speeds within the Same Power Consumption Envelope.
 package com.techilashots.java9.features;  

 import java.util.List;  
 import java.util.Vector;  

 /**  
  * For a Single Line Demonstration, I wrote the Non-Pure Threaded form of Producer-Consumer. Run the Code Multiple Times  
  * On a Machine, where you can Understand how the Temperature Changes and Extra Cooling Fan Kicks Off. Even though I did  
  * not do it myself, Take it up as an Experiment, by removing the Thread.onSpinWait() [Compare Before/After and also try  
  * with Data Set of 1 Million to 10 Million]  
  *   
  * Thread.onSpinWait() will Optimize Latency and Reduce/Optimize Power Consumption  
  */  
 public class SpinWaitHints {  

      public static void main(String[] args) {  
           List<String> itemQueue = new Vector<String>();  
           Producer producer = new Producer(itemQueue);  
           Consumer consumer = new Consumer(itemQueue);  
           producer.start();  
           consumer.start();  
      }  
 }  
 class Producer extends Thread {  

      List<String> itemQueue;  
      Producer(List<String> itemQueue) {  
           this.itemQueue = itemQueue;  
      }  
      public void run() {  
           try {  
                produce();  
           } catch (InterruptedException e) {  
                e.printStackTrace();  
           }  
      }  
      public void produce() throws InterruptedException {  
           while (true) {  
                while (itemQueue.size() < 100000) // produce 1 lac items  
                {  
                     String item = "Sumith Puri " + (itemQueue.size());  
                     itemQueue.add(item);  
                     System.out.println("Item Produced: " + item);  
                }  
                while (itemQueue.size() > 0) {  
                     // spin waiting - x86 architectures will now optimize  
                     Thread.onSpinWait();  
                }  
           }  
      }  
 }  

 class Consumer extends Thread {  
      List<String> itemQueue;  
      public Consumer(List<String> itemQueue) {  
           this.itemQueue = itemQueue;  
      }  
      public void consume() throws InterruptedException {  
           while (true) {  
                while (itemQueue.size() < 100000) {  
                     // spin waiting - x86 architectures will now optimize  
                     Thread.onSpinWait();  
                }  
                int x = itemQueue.size();  
                while (x >= 1) {  
                     x = x - 1;  
                     if (x >= 0) {  
                          String item = itemQueue.remove(x);  
                          System.out.println("Item Consumed: " + item);  
                     }  
                }  
                if (itemQueue.size() > 0)  
                     itemQueue.remove(0);  
           }  
      }  

      public void run() {  
           try {  
                consume();  
           } catch (InterruptedException e) {  
                e.printStackTrace();  
           }  
      }  
 }  


12. New Version-String Scheme
From Java 9, $MAJOR.$MINOR.$SECURITY.$PATCH  is the naming scheme for releases in Java. These details are also contained in the Runtime.Version Class.
 package com.techilashots.java9.features;  
 /**  
  * $MAJOR.$MINOR.$SECURITY+$BUILD is the Naming Scheme for Version String in Java.   
  */  
 public class JavaVersionStringChanges {  
      public void printVersionInformation() {  
           Runtime.Version versionInfo = Runtime.version();  
           System.out.println("Version String Changes in Java 9");  
           System.out.println("Major Version: " + versionInfo.major());  
           System.out.println("Minor Version: " + versionInfo.minor());  
           System.out.println("Security Version: " + versionInfo.security());  
           System.out.println("Build Version: " + versionInfo.build());  
           System.out.println("\nIn Java 9, Version Naming is Major.Minor.Security.Build");  
           System.out.println("Currently Running in Java Version: " + versionInfo.toString());  
      }  
      public static void main(String[] args) {  
           new JavaVersionStringChanges().printVersionInformation();  
      }  
 }  


13. Enhanced Method Handles
A method handle is a typed, directly executable reference to an underlying method, constructor, field, or similar low-level operation, with optional transformations of arguments or return values. These transformations are quite general, and include such patterns as conversioninsertiondeletion, and substitution. In Java 9, Method Handles have been Enhanced to include static methods for creating different kind of Method Handles.

 package com.techilashots.java9.features;  
 import java.lang.invoke.MethodHandle;  
 import java.lang.invoke.MethodHandles;  

 /**  
  * MethodHandles were introduced first in Java 7. You have to think them as an alternative for Java Reflection API, but  
  * with an advantage of better performance as they are specified at creation time. Enhanced Method Handles has primarily  
  * added new static methods to better/widen the functionality provided by Method Handles.  
  *   
  * Note that Method Handles are Enhanced in Java 9, to introduce very many changes and methods. I will be covering the  
  * ones that are the most important, only to introduce this topic.   
  *   
  * arrayLength, arrayConstructor, zero, empty,   
  * loop, countedloop, iteratedloop, dowhileloop, whileloop, try/finally  
  */  
 public class EnhancedMethodHandles {  

      public void enhancedMethodHandleDemo() {  

           try { 
 
                // arrayLenth  
                MethodHandle methodHandleLength = MethodHandles.arrayLength(int[].class);  
                int[] ageArray = new int[] { 21, 28, 36 };  
                int arrayLength;  
                arrayLength = (int) methodHandleLength.invoke(ageArray);  
                System.out.println("Length of Array using Method Handle is " + arrayLength);  
                
                // arrayConstructor  
                MethodHandle methodHandleConstructor = MethodHandles.arrayConstructor(int[].class);  
                int[] newAgeArray = (int[]) methodHandleConstructor.invoke(3);  
                System.out.println("Array Constructed using Method Handle of Size " + newAgeArray.length);  
                
                // zero  
                int x = (int) MethodHandles.zero(int.class).invoke();  
                System.out.println("Default Value of Primitive Integer using Method Handles is " + x);  
                String y = (String) MethodHandles.zero(String.class).invoke();  
                System.out.println("Default Value of String using Method Handles is " + y);  
                
                System.out.println();  
                System.out.println("Reader/Developer - Left as an Exercise for You :-)");  
                System.out.println("Refer Loop, CountedLoop, DoWhileLoop, WhileLoop, IteratedLoop, TryFinally");  
           } catch (Throwable e) {  
                
                System.out.println("Was Hungry as Ever - Gulped Everything I Got!");  
           }  
           // refer to, https://goo.gl/JCyo7N (official javadoc)  
           // also use, https://goo.gl/i8wNJ8 (individual blog)  
      }  

      public static void main(String[] args) {  
           new EnhancedMethodHandles().enhancedMethodHandleDemo();  
      }  
 }  



14. Variable Handles
Java's Concurrent Package (java.util.concurrent.atomic) provide all Atomic Types for performing atomic operations. Apart from this,  Unsafe Operations (sun.misc.unsafe) such as creating objects without calling the constructor used in Java Low-Level Programming require to be hidden from the outside world as per JEP 260: Encapsulate Most Internal APIs This has led to creation of a new abstract class type named VarHandle - This will allow a developer to assign different types to the same reference (dynamically typed reference). It can also take care of performing atomic operations on the held variable, including compare and swap (set or exchange) operations. It also provides memory fencing operations, to order the in-memory representation of the object, by providing finer grain control. 

Firstly, You need to understand Memory Ordering Effects, as VarHandle are entirely based on the Understanding of Plain, Volatile, Opaque, Acquire/Release Memory Ordering Modes. You may refer to them at : https://www.baeldung.com/java-variable-handles (Under Memory Ordering Effects). You have to try and visualize this correctly, and then proceed to the Code Samples.
 package com.techilashots.java9.features;  

 import java.lang.invoke.MethodHandles;  
 import java.lang.invoke.VarHandle;  

 /**  
  * VarHandle allows developers to assign different types to the same reference (dynamically typed reference).It can also   
  * take care of performing atomic operations on the held variable, including compare and swap (set/exchange) operations.   
  * It also provides memory fencing operations, to order the in-memory representation of the object, by providing finer   
  * grain control.   
  *   
  * I am providing an Example of the Read Operations using VarHandle. Please refer to the link provided below for further  
  * info on VarHandle on Public Variable, VarHandle for Private Variables, VarHandle for Array Types   
  *   
  * https://www.baeldung.com/java-variable-handles  
  */  
 class VarHandleStore {  

      public int varIntHandle01 = 5;  
      public int varIntHandle02 = 9;  
      public byte varByteHandle03 = 21;  
 }  

 public class VariableHandles {  

      public void useVariableHandle() {  

           System.out.println("Java 9 Introduces.... Variable Handles!");  

           try { 

                VarHandleStore varHandleStore = new VarHandleStore();  
                VarHandle varHandle = MethodHandles.lookup().in(VarHandleStore.class).findVarHandle(VarHandleStore.class,  
                          "varIntHandle01", int.class);  
                // value using get() in varhandle  
                int plainValue = (int) varHandle.get(varHandleStore);  
                System.out.println("Value using get() in VarHandle: " + plainValue);  

                // value is written to using set() - plain access  
                // you can also use set(), setOpaque(), setVolatile(), setRelease()  
                varHandle.set(varHandleStore, 21);  
                plainValue = (int) varHandle.get(varHandleStore);  
                System.out.println("Set Value using set(), then get() in VarHandle: " + plainValue);  
                
                // value is written to using getandadd()  
                int oldValue = (int) varHandle.getAndAdd(varHandleStore, 51);  
                plainValue = (int) varHandle.get(varHandleStore);  
                System.out.println("Using getAndAdd() in VarHandle, Old Value: " + oldValue + ", New Value: " + plainValue);  
                varHandle = MethodHandles.lookup().in(VarHandleStore.class).findVarHandle(VarHandleStore.class,  
                          "varIntHandle02", int.class);  
                
                // please do try out the compareandset() - atomic updates  
                // have left this due to time constraints  
                // value is written to using getandbitwiseor()  
                varHandle = MethodHandles.lookup().in(VarHandleStore.class).findVarHandle(VarHandleStore.class,  
                          "varByteHandle03", byte.class);  
                byte before = (byte) varHandle.getAndBitwiseOr(varHandleStore, (byte) 127);  
                byte after = (byte) varHandle.get(varHandleStore);  
                
                System.out.println("Get Byte Value, Then Or, using getAndBitwiseOr()");  
                System.out.println("Old Byte Value: " + before + "; New Byte Value: " + after);  
           } catch (NoSuchFieldException | IllegalAccessException e) {  
 
                e.printStackTrace();  
           }  
      }  

      public static void main(String[] args) {  
           new VariableHandles().useVariableHandle();  
      }  
 }  


15. Filter Incoming Serialization Data
This feature is related to the addition of filters at the serialization incoming streams to improve security and robustness. The core mechanism is a filter interface implemented by serialization clients and set on an ObjectInputStream. The filter interface methods are called during the de-serialization process to validate the classes being de-serialized, the sizes of arrays being created, and metrics describing stream length, stream depth, and number of references as the stream is being decoded. The filter returns a status to accept, reject, or leave the status undecided. Some Helpful Information On this Feature can be Found at: https://goo.gl/bRezWt You have to remember the words Security, Vulnerabilities and Robustness as the main reasons behind creation of this Feature in Java. This will thwart possible Security Attacks like Denial Of Service.

The core mechanism is a Filter Interface implemented by serialization clients and set on an ObjectInputStream. The filter interface methods are called during the Deserialization Process to Validate the classes being Deserialized, The Sizes of Arrays being Created, The Stream Length, Graph Depth and Number of References as the Stream is being Decoded. A filter determines whether the arguments are ALLOWED or REJECTED and should return the appropriate status. If the filter cannot determine the status it should return UNDECIDED. Filters are designed for the specific use case and expected types. A filter designed for a particular use may be passed a class that is outside of the scope of the filter. If, for example, the purpose of the filter is to Black-List Classes then it can reject a candidate class that matches and report UNDECIDED for others.
 package com.techilashots.java9.features;  

 import java.io.ObjectInputFilter;  

 /**  
  * Demonstrates Java 9 Serialization/De-Serialization Filters for Incoming Data. Do Refer https://goo.gl/bRezWt for more  
  * on Filters, with more Details and Examples.  
  */  
 class ItemCatalogFilter implements ObjectInputFilter {  

      private long maxStreamBytes = 400; // Maximum allowed bytes in the stream.  
      private long maxDepth = 2; // Maximum depth of the graph allowed.  
      private long maxReferences = 5; // Maximum number of references in a graph.  

      @Override  
      public Status checkInput(FilterInfo filterInfo) {  

           if (filterInfo.references() < 0 || filterInfo.depth() < 0 || filterInfo.streamBytes() < 0  
                     || filterInfo.references() > maxReferences || filterInfo.depth() > maxDepth  
                     || filterInfo.streamBytes() > maxStreamBytes) {  
                // reject this, as it seems malicious, incorrect or tampered with  
                return Status.REJECTED;  
           }  

           Class<?> clazz = filterInfo.serialClass();  
           if (clazz != null) {  
                if (CatalogCustomer.class == filterInfo.serialClass()) {  
                     // we are expecting a customer of our product catalog  
                     System.out.println("Incoming Serialization Data Verified for Structure and Vulnerabilities");  
                     return Status.ALLOWED;  
                } else {  
                     // seems like some tampered, unexpected or malicious data here  
                     return Status.REJECTED;  
                }  
           }  

           // the status as undecided, when we cannot infer - or + for sure  
           // left for the developer to decide as per business/security process  
           return Status.UNDECIDED;  
      }  
 }  


You may Download the Code Samples for the following New Features Here. (Import as Eclipse Project, Set the Compiler/Environment as Java 9. Make Sure that you Have Java 9 / JDK 9 Installed on your System. Run the Main Class 'Java9Application' to see the Output/Outcome of the Code Samples.)

Check out the Eclipse Console Output provided Below from Running the 'Java9Application' (Refer Attached Code Samples). Please Refer to the Individual Classes, provided for Each of the Features to Understand the Features Better. Go Ahead - Add, Modify, Delete to Experiment with all of the New Java 9 Features. The Output for the Features Marked [01-08] is  Shown in the Part 1 of the Article at 
Java SE 9... What's New? [Code Samples - 01/02]


 ================================  

 09. Stack Walking API  
 ---------------------  
 Java 9 Stack Walker API for Debugging and Application Behavior  
 Java 9 Stack Walker API - Showing All Stack Frames  
 com.techilashots.java9.features.StackWalkingService.databaseService(StackWalkingService.java:23)  
 com.techilashots.java9.features.StackWalkingService.persistenceService(StackWalkingService.java:57)  
 com.techilashots.java9.features.StackWalkingService.businessService(StackWalkingService.java:63)  
 com.techilashots.java9.features.StackWalkingService.presentationService(StackWalkingService.java:69)  
 com.techilashots.java9.features.StackWalkingService.uiDisplay(StackWalkingService.java:77)  
 com.techilashots.java9.main.Java9Application.stackWalkingAPI(Java9Application.java:321)  
 com.techilashots.java9.main.Java9Application.java9CoreLibraryChanges(Java9Application.java:106)  
 com.techilashots.java9.main.Java9Application.main(Java9Application.java:48)  
 Java 9 Stack Walker API - Walking / Filtering Stack Frames  
 com.techilashots.java9.features.StackWalkingService.databaseService(StackWalkingService.java:30)  
 com.techilashots.java9.features.StackWalkingService.persistenceService(StackWalkingService.java:57)  
 Java 9 Stack Walker API - Show All Attributes in Stack Frame  
 [Bytecode Index] 112  
 [Class Name] com.techilashots.java9.features.StackWalkingService  
 [Declaring Class] class com.techilashots.java9.features.StackWalkingService  
 [File Name] StackWalkingService.java  
 [Method Name] StackWalkingService.java  
 [Is Native] false  
 [Line Number] 38  
 [Method to Display On User Interface]  
 Old MacDonald had a Farm. In that Farm, He had 3.0 Cows!  

 ================================  

 11. Spin Wait Hints  
 -------------------  
 Spin Wait Hints Makes Power Consumption Efficient  
 [Uncomment Method Call in Code, To Run (Non-Terminating) Demo]  

 ================================  

 12. Java Version Naming  
 ----------------------  
 Java 9 changes the way Java Version String Format  
 Version String Changes in Java 9  
 Major Version: 9  
 Minor Version: 0  
 Security Version: 4  
 Build Version: Optional[11]  
 In Java 9, Version Naming is Major.Minor.Security.Build  
 Currently Running in Java Version: 9.0.4+11  

 ================================  

 13. Enhanced Method Handles  
 ---------------------------  
 Java 9 has Enhanced Method Handles for Multitude of Operations  
 Length of Array using Method Handle is 3  
 Array Constructed using Method Handle of Size 3  
 Default Value of Primitive Integer using Method Handles is 0  
 Default Value of String using Method Handles is null  
 Reader/Developer - Left as an Exercise for You :-)  
 Refer Loop, CountedLoop, DoWhileLoop, WhileLoop, IteratedLoop, TryFinally  

 ================================  

 14. Variable Handles  
 --------------------  
 Java 9 Introduces Variable Handles, Different Types for Same Reference  
 Java 9 Introduces.... Variable Handles!  
 Value using get() in VarHandle: 5  
 Set Value using set(), then get() in VarHandle: 21  
 Using getAndAdd() in VarHandle, Old Value: 21, New Value: 72  
 Get Byte Value, Then Or, using getAndBitwiseOr()  
 Old Byte Value: 21; New Byte Value: 127  

 ================================  

 15. Filter Incoming Serialization Data  
 -------------------------------------  
 Java 9 Allows Filtering of Serialization Data, through Object Input Filters  
 Incoming Serialization Data Verified for Structure and Vulnerabilities  
 Customer Details String Follows (Filtered then De-Serialized)  
 skp:skp:4240123488889001:1.243223256E7  

 ================================  


_______________________________________________________________________________

16. More Concurrency Updates
[Will be Covered In a Separate Article, As there are Many Changes to Cover and Merits an Article on Its Own]
_________________________________________________________________________________

[Note that I will be covering 'Java 9 Modularity - Project Jigsaw' in another Article. The following Topics/Changes with Code Samples will be Covered in that Article]


JEP 261: Module System
JEP 200: The Modular JDK 
JEP 220: Modular Run-Time Images JEP 260: Encapsulate Most Internal APIs   _________________________________________________________________________________


Happy Coding with Java 9!

Tuesday, September 18, 2018

Java SE 9... What's New? [Code Samples - 01/02]

As I had promised, albeit very late, I am back with the Code Samples of my earlier article Java SE 9... What's New? You may refer the earlier article to understand the newly introduced features at a High Level. This article provides the Code Samples for each of the Features.

You may Download the Code Samples for the following New Features Here. (Import as Eclipse Project, Set Compiler/Environment as Java 9. Run the Main Class 'Java9Application' to see the Output/Outcome of the Code Samples.)

I plan to start by re-visiting a Java 8 Feature 'Default Interface Methods'


00. Default Interface Methods
Whenever there is existing or legacy code which has Interfaces that require addition of new methods - It causes breakage of existing classes that inherit/implement from this Interface unless the implementation for each of these added methods is provided in the classes. This is not very maintainable code. Even though a good practice as per SOLID and other OO paradigms to provide an interface without any implementation - we need to handle and solve the problem as mentioned above. This is where Default Interface Methods come in.

01: import java.util.List;  
02:    
03:  public interface LegacyPublicInterface {  
04:        
05:        
06:      /**  
07:       * Additional Default Method that Can be Invoked on Each Type of Invoice that Implements the LegacyPublicInterface.   
08:       * It can also be over-ridden by the Extending Invoice Types. This is an Example Usage and Benefit of the Java/JDK 8  
09:       * Default Interface feature.  
10:       * @param items  
11:       */  
12:      default void checkStatusOnEachItem (List<String>... items) {  
13:        
14:          for(String item : items) {  
15:              if(item.startsWith("OOS_")) {  
16:                  items.remove(item);  
17:              }  
18:          }  
19:          return;  
20:      }  
21:    
22:  }  
23:    


From the Example Above, the LegacyPublicInterface in an Existing Application is already extended by Multiple Invoice Types (For Example in an Inventory System). But as per changing business, Re-Engineering effort requires that each of the Invoices have a method to invalidate or remove an Item marked with "OOS". Given such a problem, Prior to Java 8, we would have to introduce a new method declaration in the interface and then require that each of the implementing classes implement their own logic for handling this. With Default Interfaces, the task becomes very simple (The code is now more maintainable and extensible and requires very less effort to change). With the introduction of thus feature of Default Methods, the following are the possibilities:

1. Use the Default Method(s) without Breaking Existing Functionality (Best Use)
2. Implementing Class can choose to override these Default Methods
3. Abstract Classes can be provided over the Interfaces to override Implementation

So, Let's get further into each of these changes in Java 9, one-by-one. 


A. Java Language Changes in JDK 9

[01] Private Interface Methods
Interfaces in Java 9 are allowed to have Private Methods. This was done to allow code sharing between non-abstract methods in the Interface. All rules related to ordinary Private modifier apply to these methods. The point to note is that a method cannot be both private and abstract. It definitely needs to have a method body.

 package com.techilashots.java9.features;  
 
/**  
  * @author sumith.puri  
  *   
  * prior to java 8, the default scope/visibilty of every method in an interface was same  
  * the attributes were all by default, [public static final] and the methods were [public]  
  * with java 8, [default] interface methods have been introduced but so has been a new scope/visibility  
  * with java 9, interface can have private visibility methods that can be used internally   
  */  
 public interface Java9PrivateMethodInterface {  

      public void preJava9BusinessMethodOne();  

      public void preJava9BusinessMethodTwo();  

      public default void newJava9BusinessMethodThree() {  
           System.out.println("Interface Methods can call Private Interface Methods!");  
           System.out.println("");  
           java9BusinessMethodThreeHelper();  
      }  

      private void java9BusinessMethodThreeHelper() {  
           System.out.println("Default Methods... Now Private Methods!");  
           System.out.println("Once Upon a Time, I was a Java Developer!");  
      }  
 }  


You can observe the usage of the above private method (internally by the new default method) through the following code.

      //new features of java 9  
      //private interface methods  
      private void privateInterfaceMethod() {  

           System.out.println("01. Private Interface Methods");  
           System.out.println("-----------------------------");  
           System.out.println("[Private Interface Method Invoked by Default Interface Method]");  
           Java9PrivateMethodInterface java9PrivateMethodInterface = new Java9PrivateMethodInterface() {  
                @Override  
                public void preJava9BusinessMethodTwo() {  
                     // TODO Auto-generated method stub  
                }  
                @Override  
                public void preJava9BusinessMethodOne() {  
                     // TODO Auto-generated method stub  
                }  
           };  

           java9PrivateMethodInterface.newJava9BusinessMethodThree();  
           System.out.println("");  
           System.out.println("================================");  
           System.out.println("");  
      }  


[02] Underscore as a Variable Name is not Legal Anymore
Using only the _ (underscore character) as a variable name is not legal anymore.This is because it is marked as a reserved keyword from Java 1.8 (But causing compilation failure only in Java 1.9). This may cause a some issues when compiling legacy source code, especially which had a necessity to denote some specific resource or entity using the _ (underscore).  It may have to be rewritten and may have many related ramifications.
 /**  
  * @author sumith.puri  
  *  
  */  
 public class Java9VariableNaming {  
      // pre-java 9, this was a valid variable name  
      // from java 8, _ was marked as a reserved keyword (compiler warning) 
      // from java 9, the following line of code will cause compilation failure  
      // private String _;  
      private String _socket = null;  
      /**  
       * @param args  
       */  
      public void notForDemoMethod() {  
           _socket = new String("Network Socket");  
           System.out.println("_ is no Longer a Valid Variable Name!, [Variable Value: " + _socket + "]");            
      }  
 }  


[03] Allow Effectively final variables to be used as Resources in Try with Resources
Upto Java 8, Every variable that had to be used within Try with Resources statements requires to be declared within the try statement. Only then, can it be used within the try block. This is a limitation for th developer. Hence, In Java 9, this restriction has been removed and any final variable or effectively final (local) variable can be used inside the try block. All other rules as applicable to Try with Resources continue. Effectively Final means the variable that is not changed once after it has been initialized.

 package com.techilashots.java9.features; 

 import java.io.BufferedReader;  
 import java.io.File;  
 import java.io.FileReader;  
 import java.io.IOException;  
 
 /**  
  * @author sumith.puri  
  */  
 public class EffectivelyFinalTryResources {  
 
     private static File file = new File("try_resources.j9");   

      //with java 9, you need to use either explicitly final or effectively final variables in try/resources  
      public void methodWithAnomaly() throws IOException {  

           file.createNewFile();  
           BufferedReader bufferedReader = new BufferedReader(new FileReader(file));  
       
           //prior to java 9, the usage would look like this  
           //try (final BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {  
           //this code will not compile before java 9  
           try (bufferedReader) {            
                System.out.println("Can Use Final or Effectively Final in Try with Resources!");  
           } finally {  
                System.out.println("Java 9 Gives More Flexibility to Developers.");  
           }            
      }  
 }  


[04] @SafeVarargs is Allowed on Private Instance Methods
Using non-reifiable variable argument parameters in a method can cause multiple warnings when trying to compile code, such as:

Note: LegacyPublicInterface.java uses unchecked or unsafe operations.  
Note: Recompile with -Xlint:unchecked for details.

Hence @SafeVarargs was introduced to suppress such warnings on unchecked or unsafe operations. By using this, the developer is signalling to the compiler that he has made sure that there will be no Heap Pollution (such as unsafe forEach operations) caused.

Prior to Java 9, @SafeVarargs is allowed on non-overridable methods such as in static methods, final instance methods and constructors. Note that the annotation will throw an error if it is used in fixed arity methods.  In Java 9, @SafeVarargs can be used on private instance methods.
 package com.techilashots.java9.features;  

 /**  
  * @author sumith.puri  
  */  
 public class SafeVarargsOnPrivateMethod {  

      public void demoSafeVarargsInJava9() {  
           safeVarargsInJava9(24.00f, 03, 19, 82);  
      }  

      @SafeVarargs  
      private void safeVarargsInJava9 (Float a, Integer...b) {  
           System.out.println("Invoked a Private Instance Method with " + a + ", " + b[0] + ", " + b[1] + ", " + b[2]);  
           System.out.println("With Java 9, @SafeVarargs is Allowed on Private Instance Methods!");  
      }  
 }  


[05] Allow Diamond with Anonymous Classes If Inferred Type's Argument Type is Denotable
Upto Java 8, using Generics and Diamond Operators with Anonymous Classes. It was mainly because the compiler could not infer whether it can represent the type in the Argument passed to the Diamond Operator.  JSR 334 has the following to say about using diamond with anonymous classes:
"Using diamond with anonymous inner classes is not supported since doing so in general would require extensions to the class file signature attribute to represent non-denotable types, a de facto JVM Change." 

Additional information is located in the Project Coin mailing list's Diamond Operator and Anonymous Classes topic:
"Internally, a Java compiler operates over a richer set of types than those that can be written down explicitly in a Java program. The compiler-internal types which cannot be written in a Java program are called non-denotable types. Non-denotable types can occur as the result of the inference used by diamond. Therefore, using diamond with anonymous inner classes is not supported since doing so in general would require extensions to the class file signature attribute to represent non-denotable types, a de facto JVM change. It is feasible that future platform versions could allow use of diamond when creating an anonymous inner class as long as the inferred type was denotable."
With Java 9, the Java Compiler has changed its Inference Algorithm in a way that Diamond Operator (Generics) can now work simultaneously with Anonymous Classes, as long as the Argument Type of the Inferred Type is Denotable. The important point to note is that things that fall under Denotable are Primitive Types, Raw Types and Non-Generic Types. Non-Denotable means ones that cannot be written in a Java Program, like usage of Extends, Super along with Wildcard Types in Generics. These are usually inferred by the compiler. So, as long as the compiler identifies that the Argument Type of Inferred Type is Denotable - You can use the Diamond Operator in Conjunction with Anonymous Inner Classes.


 package com.techilashots.java9.features;  
 /**  
  * @author sumith.puri  
  */  
 public class DiamondOperatorOnAnonymous {  
      public void diamondForInferredDenotable() {  
           //prior to java 9, anonymous inner classes were not allowed to use diamond operator  
           Java5ToJava8Coder<? extends Number> java9SyntaxGuru = new Java5ToJava8Coder<>() {  
                     @Override  
                     public void java9SyntaxMagic() {  
                          System.out.println("Introduced in Java 5, Generics was Intriguing and Complex!");  
                          System.out.println("With Java 9, Diamond Operator On Anonymous Classes is Allowed...");  
                          System.out.println("As Long as Java 9 Infers the Type (Inferred) to be Denotable.");  
                     }  
           };  
           java9SyntaxGuru.java9SyntaxMagic();  
      }  
 }  
 abstract class Java5ToJava8Coder<T extends Number> {  
       public abstract void java9SyntaxMagic();  
 }


B. Core Library Changes in Java 9

[06] JEP 102: Process API Updates
With Java 9, one can retrieve the PID of the process through a native call. This is achievable through the ProcessHandle. Also, we can retrieve information about the currently running Java Process (JVM) and Info (Inner Class of ProcessHandle) Class/Object that contains details on the process. We can also enlist or return a snapshot of all currently running processes in the system.
 package com.techilashots.java9.features;  
 import java.lang.ProcessHandle.Info;  

 /**  
  * @author sumith.puri  
  */  
 public class ProcessAPIChanges {  
      
      public void detailedAPIInfo(ProcessHandle processHandle) {  
           
           Info processInfo = processHandle.info();  
           System.out.println("[Java 9 Developers... Check this Out!]");  
           System.out.println("[Detailed Process Info is Provided Below]");  
           System.out.println("[Executable Name] " + processInfo.command().get());  
           System.out.println("[User Name] " + processInfo.user().get());  
           System.out.println("[Start Time] " + processInfo.startInstant().get().toString());  
           System.out.println("+++++");  
      }
  
      public static void main(String[] args) {  
           
           System.out.println("06. Process API Changes (Core Library) ");  
           System.out.println("--------------------------------------");  
           System.out.println("Check Out the Detailed Process Information in Java 9");  
           
           ProcessAPIChanges processAPIChanges = new ProcessAPIChanges();  
           ProcessHandle processHandle = ProcessHandle.current();  
           
           System.out.println("With Java 9, Process Id is Available");  
           System.out.println("[Current Process Id] " + processHandle.pid());  
           System.out.println("-------------------------------------");  
           
           processAPIChanges.detailedAPIInfo(processHandle);  
           
           System.out.println("-------------------------------------");  
           System.out.println("With Java, You can View all Processes..");  
           System.out.println("That are Visible to the Current Process!");  
           
           ProcessHandle.allProcesses()  
            .filter(ph -> ph.info().command().isPresent())  
            .limit(4)  
            .forEach((process) -> processAPIChanges.detailedAPIInfo(process));  
           
           System.out.println("");  
           System.out.println("================================");  
           System.out.println("");  
      }  
 }  


[07] JEP 277: Enhanced Deprecation
With an eye on maintainable and more informative code, the Developer defined Deprecation now allows us to mark deprecation with additional elements of information like forRemoval and since. The forRemoval allows allows to mark that this item may be removed in the future versions of Java and since provides information about when it was first introduced.


 package com.techilashots.java9.features;  
 
 /**  
  * @author sumith.puri  
  */  
 public class EnhancedDeprecation {  
      
      @Deprecated(forRemoval=true)  
      public void methodMarkedForRemoval() {  
           System.out.println("Java 9 Allows Enhanced Method Deprecation");  
           System.out.println("Invoked Method is Deprecated and Marked [For Removal]");  
           this.methodDeprecatedSince();  
      }  
      
      @Deprecated(since="12.2")  
      public void methodDeprecatedSince() {  
           System.out.println("Invoked Method is Deprecated and Marked [Since]");  
      }  
 }  
 
 

[08] JEP 269: Convenience Factory Methods for Collections
This addition makes it convenient for the Developer to create Immutable Collections out of existing interfaces, be it Set, Map or List. A static factory method of() added to each of the interfaces - Set, Map and List. It is important you understand the following (even though consistent with previous versions of Java):
  • They are Structurally Immutable
  • They Disallow Null elements or null keys. 
  • They are Serializable if all elements are serializable.
  • They Reject Duplicate Elements/Keys at creation time. 
  • The Iteration Order of set elements is Unspecified and is Subject to Change.
  • They are Value-Based. Factories are free to create new instances or reuse existing ones. Therefore, Identity-Sensitive Operations on these instances (Reference Equality (==), Identity Hash Code, and Synchronization) are Unreliable and should be Avoided.They are serialized as specified on the Serialized Form page.
 package com.techilashots.java9.features;  
 import java.util.Set;  
 /**  
  * @author sumith.puri  
  */  
 public class ConvenienceCollections {  
      public void checkItOut() {  
           System.out.println("Java 9 Introduced a Static [of()] Factory Method");  
           System.out.println("This allows Creation of Immutable Collections");  
           Set<String> immutableCountrySet = Set.of("America", "Russia", "China", "India");  
           try {  
                immutableCountrySet.add("England");  
           } catch (Exception e) {  
                System.out.println("Caught Exception, Adding Entry to Immutable Collection!");   
           }  
      }   
 }  


You may Download the Code Samples for the following New Features Here. (Import as Eclipse Project, Set the Compiler/Environment as Java 9. Make Sure that you Have Java 9 / JDK 9 Installed on your System. Run the Main Class 'Java9Application' to see the Output/Outcome of the Code Samples.)

Check out the Eclipse Console Output provided Below from Running the 'Java9Application' (Refer Attached Code Samples). Please Refer to the Individual Classes, provided for Each of the Features to Understand the Features Better. Go Ahead - Add, Modify, Delete to Experiment with all of the New Java 9 Features.

 Sample of New Features of Java 9  
 ================================  
   
 00. Default Interface Methods  
 -----------------------------  
 [Already Integrated Java Service - Default Implementation]  
 Core Business Method One  
 Core Business Method Two  
 Java 8 Changed the Definition of Interface Forever!  
 Thou shall not clear the Basic Core Java Interview  
   
 [Newly Integrated Java Service - Override Default Implementation]  
 Core Business Method One  
 Core Business Method Two  
 Attention - All Java Developers - Default Interfaces  
 The World is now a Newer Place from Java 8!  
   
 ================================  
   
 01. Private Interface Methods  
 -----------------------------  
 [Private Interface Method Invoked by Default Interface Method]  
 Interface Methods can call Private Interface Methods!  
   
 Default Methods... Now Private Methods!  
 Once Upon a Time, I was a Java Developer!  
   
 ================================  
   
 02. Underscore Variable Naming  
 -----------------------------  
 [Nothing to Demonstrate - Refer Code for New Rule]  
 _ is no Longer a Valid Variable Name!, [Variable Value: Network Socket]  
   
 ================================  
   
 03. Effectively Final Try with Resources  
 ----------------------------------------  
 [Nothing to Demonstrate - Refer Code for New Rule]  
 Can Use Final or Effectively Final in Try with Resources!  
 Java 9 Gives More Flexibility to Developers.  
   
 ================================  
   
 04. Safe Varargs on Private Method  
 ----------------------------------  
 [Nothing to Demonstrate - Refer Code for New Rule]  
 Invoked a Private Instance Method with 24.0, 3, 19, 82  
 With Java 9, @SafeVarargs is Allowed on Private Instance Methods!  
   
 ================================  
   
 05. Diamond on Anonymous Classes   
 --------------------------------  
 [Nothing to Demonstrate - Refer Code for New Rule]  
 Introduced in Java 5, Generics was Intriguing and Complex!  
 With Java 9, Diamond Operator On Anonymous Classes is Allowed...  
 As Long as Java 9 Infers the Type (Inferred) to be Denotable.  
   
 ================================  
   
 06. Process API Changes (Core Library)   
 --------------------------------------  
 Check Out the Detailed Process Information in Java 9  
 With Java 9, Process Id is Available  
 [Current Process Id] 1380  
 -------------------------------------  
 [Java 9 Developers... Check this Out!]  
 [Detailed Process Info is Provided Below]  
 [Executable Name] C:\Users\U074SXP\Documents\My Installed\jdk-9.0.4\bin\javaw.exe  
 [User Name] CWT\U074SXP  
 [Start Time] 2018-09-18T00:21:57.354Z  
 +++++  
 -------------------------------------  
 With Java, You can View all Processes..  
 That are Visible to the Current Process!  
 [Java 9 Developers... Check this Out!]  
 [Detailed Process Info is Provided Below]  
 [Executable Name] C:\Windows\System32\taskhost.exe  
 [User Name] CWT\U074SXP  
 [Start Time] 2018-09-17T22:39:11.949Z  
 +++++  
 [Java 9 Developers... Check this Out!]  
 [Detailed Process Info is Provided Below]  
 [Executable Name] C:\Program Files\Microsoft Application Virtualization\Client\AppVStreamingUX.exe  
 [User Name] CWT\U074SXP  
 [Start Time] 2018-09-17T22:39:11.996Z  
 +++++  
 [Java 9 Developers... Check this Out!]  
 [Detailed Process Info is Provided Below]  
 [Executable Name] C:\Program Files (x86)\Symantec\Symantec Endpoint Protection\12.1.7004.6500.105\Bin\ccSvcHst.exe  
 [User Name] CWT\U074SXP  
 [Start Time] 2018-09-17T22:39:12.074Z  
 +++++  
 [Java 9 Developers... Check this Out!]  
 [Detailed Process Info is Provided Below]  
 [Executable Name] C:\Program Files\Synaptics\SynTP\SynTPEnh.exe  
 [User Name] CWT\U074SXP  
 [Start Time] 2018-09-17T22:39:12.214Z  
 +++++  
   
 ================================  
   
 07. Enhanced Deprecation  
 ------------------------  
 [Nothing to Demonstrate - Refer Code for New Rule]  
 Java 9 Allows Enhanced Method Deprecation  
 Invoked Method is Deprecated and Marked [For Removal]  
 Invoked Method is Deprecated and Marked [Since]  
   
 ================================  
   
 08. Convenience Collection Method  
 ---------------------------------  
 [Nothing to Demonstrate - Refer Code for New Rule]  
 Java 9 Introduced a Static [of()] Factory Method  
 This allows Creation of Immutable Collections  
 Caught Exception, Adding Entry to Immutable Collection!  
   
 ================================  


Happy Coding with Java 9!