JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics...

49
JVM Survival Guide Hadi Hariri

Transcript of JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics...

Page 1: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

JVM Survival Guide Hadi Hariri

Page 2: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

This talk

❖ For What

❖ For Who

Page 3: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

20 years of Java

❖ The Sun and The Oracle

❖ Java The Language

❖ Java The Virtual Machine

❖ Java The Ecosystem

Page 4: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

The Community

❖ Community Driven

❖ Not Vendor-Driven

❖ Large and Open Ecosystem

Page 5: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

It’s an Open Platform

❖ Languages

❖ Java, Clojure, Scala, Kotlin, Frege, Groovy, JRuby, Ceylon

❖ Frameworks

❖ JavaEE, Spring, Spark, Play Framework

Page 6: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

What to Install

Java SE

Java FX

Java EE

Java ME

JDK

JRE

Page 7: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Versioning

❖ Language - 1.X where X is the version

❖ Java XY - X where X is the version

❖ Usually aligned major versioning in Language and VM

Page 8: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Installing Java

❖ Oracle Web Site (includes Ask Toolbar)

❖ Package Managers

❖ Brew, Chocolatey

Page 9: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

JAVA_HOME

❖ Defines where Java Virtual Machine is located

❖ On Linux/OSX if undefined

export JAVA_HOME=$(/usr/libexec/java_home)

Page 10: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

A First Java App

Create a Java AppUse javac to compile itUse java to run itExample - firstApp

Page 11: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Decompiling

Decompiling exampleshow byte code and also decompiling

Page 12: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Multiple Classes and Files

usingClass example

Page 13: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Java ARchives

❖ Archive classes files into single jar file

❖ Provide metadata with METAINF file

❖ jar command line to create jar files

❖ war is the web equivalent of jar

Page 14: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Creating Jar Files

createJar.sh samples

Page 15: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Conventions

❖ In Java

❖ Virtual by default

❖ Open by default

❖ In other languages

❖ It depends

Page 16: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Conventions

❖ Packages and Modules

❖ Accessibility and Visibility Modifiers

❖ In Java, single class per file is enforced

Page 17: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Conventions

❖ Java and Java-inspired languages

lowerCamelCaseNotation

Page 18: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Namespaces

❖ Or death by a hundred folders

ext.vendor.module.subsystem

❖ Individual imports has been widely adopted

Page 19: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Namespaces

namespaces demo

Page 20: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

The Class Path

❖ Defines where Java Classes are located

❖ Set via command line java -cp

❖ Most common reason for NoClassDefFoundError

Page 21: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Class Loaders

❖ Responsible for loading classes

❖ Each class loader has a parent

❖ Delegates what it can to its parent

Page 22: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Class Loaders

Parent

Bootstrap Class Loader

Load java.lang and other key classes

Parent

ExtensionClass Loader

Load java.ext.dirs

Parent

SystemClass Loader

Load classpath

Page 23: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Other Common Errors

❖ NoSuchMethodError - wrong version of class loaded

❖ ClassCastException - casting to wrong type

❖ IllegalAccessError - wrong access modifier

Page 24: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Using ClassPath

usingClass Path example Compile and remove to show exception

Page 25: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Loading Classes

loadingClasses Example

Page 26: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Build Systems

❖ Ant

❖ IntelliJ IDEA

❖ Eclipse

Page 27: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Build and Dependency Management

❖ Maven

❖ Gradle

Page 28: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Maven

❖ XML-Based

❖ Extensible

❖ Supports templates

❖ Maven Central Repository for Packages (and custom)

Page 29: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Maven

mavenSamplemavenDependencymavenZipmavenTeamCity

Page 30: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Gradle

❖ Not XML a.k.a. a better DSL

❖ Based on Groovy

❖ Extensible

❖ Somewhat template support (gradle --init)

❖ Maven Central Repository for Packages (and custom)

Page 31: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Gradle

gradleSamplesampleApp - app plugin

Page 32: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Working with Resources

❖ Resource folder to ship contents along with your app

❖ Ability to define in Maven/Gradle

Page 33: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Resources

resourcesSampleresourcesSampleMaven

Page 34: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Garbage Collection

❖ Generational (Young, Old, Permanent)

❖ The “IDisposable” is Closeable

❖ The “using” is “try-with-resources”

Page 35: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Asynchronous Programming

❖ CompletableFuture

❖ RxJava and adapters

Page 36: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Generics

❖ Most languages support Generics

❖ Reified Generics on JVM

Page 37: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Genericsexample of genericsexample of call site and covarianceexample of reflection

Page 38: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Testing Frameworks

❖ JUnit - The Standard

❖ TestNg

❖ Spek

Page 39: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Testing

junitjunit from Java and Kotlinspek

Page 40: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

JavaEE

❖ The Dreaded Java 2 EE Glorious Enterprise

❖ Application Servers

Page 41: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Web Frameworks

❖ Servlets and Application Containers

❖ Jetty, TomCat

Page 42: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Spring

Page 43: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Spring

❖ Application Framework

❖ Web, Security, Templating, SQL, AWS, Social

❖ start.spring.io as generator

Page 44: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Spring

springRestSamplestart.spring.io

Page 45: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Vert.x

Page 46: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Vert.x

vertx example

Page 47: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Other Interesting Libraries

❖ Mockito for mocking

❖ GSon and Jackson for JSON Serialisation

❖ JodaTime for Calendar and Time Management

❖ Netty a low-level network client and server library

Page 48: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Summary

❖ Java is a very vibrant ecosystem

❖ Many languages, many frameworks

❖ There is no single vendor driven strategy as such

Page 49: JVM Survival Guidesddconf.com/brands/sdd/library/jvm.pdf · Reified Generics on JVM. Generics example of generics example of call site and covariance example of reflection. Testing

Thank you@hhariri - [email protected]