This document discusses various techniques for dynamically generating and modifying Java bytecode at runtime. It begins with an overview of Java classes and classloaders, and how classes can be loaded by different classloaders. It then discusses Java proxies, Spring AOP, and AspectJ for implementing aspects and intercepting method calls. The document delves into using the bootstrap classloader to replace system classes, Java agents for instrumenting classes, and directly writing bytecode using libraries like BCEL. It provides an example of dynamically generating a class to map between two object types at runtime. In summary, the document covers advanced Java concepts like bytecode manipulation that enable dynamically modifying and generating new classes.
Lambda: A Peek Under The Hood - Brian GoetzJAX London
This is a *highly technical* session on the details of how Lambda expressions are implemented in the Java language, presented by Java Language Architect Brian Goetz
This presentation is ideal for a beginner of Java or someone who wants to brush up their Java Knowledge. It's simple to understand and well organized in a way most of the area in core Java has been covered.
Lambda: A Peek Under The Hood - Brian GoetzJAX London
This is a *highly technical* session on the details of how Lambda expressions are implemented in the Java language, presented by Java Language Architect Brian Goetz
This presentation is ideal for a beginner of Java or someone who wants to brush up their Java Knowledge. It's simple to understand and well organized in a way most of the area in core Java has been covered.
Mahika Tutorials sharing PPT slide for core java programming language. Go threw this slide and visit our YouTube page too
https://www.youtube.com/c/mahikatutorials
Spring boot is a suite, pre-configured, pre-sugared set of frameworks/technologies to reduce boilerplate configuration providing you the shortest way to have a Spring web application up and running with smallest line of code/configuration out-of-the-box.
Introduction to Java programming - Java tutorial for beginners to teach Java ...Duckademy IT courses
Check out the course: https://www.duckademy.com/course/java-programming Learn Java from scratch in an easy and entertaining way. The Easy-to-follow Java programming course on Duckademy was made for beginners. In this course we start at the very basics (from zero) and go through lots of interesting exercises and analogies that will take you to an advanced level. By the end of the course you will have all the knowledge needed to move on and specialize in Java. The course is easy to follow and things are well explained. Furthermore, to make your learning easier and more enjoyable throughout the course we will develop a nostalgic, text-based fantasy game.
The course is recommended to anyone who wants to learn Java. Ideal for those who are new to programming, but it can be useful and enjoyable for people who want to switch to Java from a different programming language.
By the end of the course you will be able to build simple, but fully functional programs. You will also gain all the knowledge needed to specialize in Java and become a well-payed Java expert later on.
Check out the course: https://www.duckademy.com/course/java-programming
Introduction to functional idioms in Java 8, language-extending functional libraries and short overview of reasons for adoption of such programming style.
Examples and snippets available here: https://github.com/lbialy/functionaljava8
A reflection-oriented program component can monitor the execution of an enclosure of code and can modify itself according to a desired goal related to that enclosure.
Reflection is one of those things like multi-threading where everyone with experience of it says “Don’t use it unless you absolutely have to”.
www.denizoguz.com-For an in company JDK 7 orientation I have prepared a presentation which summaries new features of JDK 7. I would like to make it public for everyone who needs it.
Mahika Tutorials sharing PPT slide for core java programming language. Go threw this slide and visit our YouTube page too
https://www.youtube.com/c/mahikatutorials
Spring boot is a suite, pre-configured, pre-sugared set of frameworks/technologies to reduce boilerplate configuration providing you the shortest way to have a Spring web application up and running with smallest line of code/configuration out-of-the-box.
Introduction to Java programming - Java tutorial for beginners to teach Java ...Duckademy IT courses
Check out the course: https://www.duckademy.com/course/java-programming Learn Java from scratch in an easy and entertaining way. The Easy-to-follow Java programming course on Duckademy was made for beginners. In this course we start at the very basics (from zero) and go through lots of interesting exercises and analogies that will take you to an advanced level. By the end of the course you will have all the knowledge needed to move on and specialize in Java. The course is easy to follow and things are well explained. Furthermore, to make your learning easier and more enjoyable throughout the course we will develop a nostalgic, text-based fantasy game.
The course is recommended to anyone who wants to learn Java. Ideal for those who are new to programming, but it can be useful and enjoyable for people who want to switch to Java from a different programming language.
By the end of the course you will be able to build simple, but fully functional programs. You will also gain all the knowledge needed to specialize in Java and become a well-payed Java expert later on.
Check out the course: https://www.duckademy.com/course/java-programming
Introduction to functional idioms in Java 8, language-extending functional libraries and short overview of reasons for adoption of such programming style.
Examples and snippets available here: https://github.com/lbialy/functionaljava8
A reflection-oriented program component can monitor the execution of an enclosure of code and can modify itself according to a desired goal related to that enclosure.
Reflection is one of those things like multi-threading where everyone with experience of it says “Don’t use it unless you absolutely have to”.
www.denizoguz.com-For an in company JDK 7 orientation I have prepared a presentation which summaries new features of JDK 7. I would like to make it public for everyone who needs it.
Update:
Video available from Oredev: vimeo.com/53147485
Metaprogramming is the dirty little secret behind the success of many Java frameworks such as Spring and Struts2, and constitutes the backbone of many of the most fundamental APIs across the JEE technology stack. This session aims introduce the topic and highlight, with code examples, the different mechanisms and techniques to take advantage of this underused feature of the Java Programming Language.
This session will adopt a learn-by-example approach that combines the philosophy and theory behind metaprogramming with concrete code examples. The audience will be walked real-life scenarios to highlight the benefits of this technique such as minimizing the number of lines of code, reduced development time, and greater flexibility, etc… Design patterns, and best practices will be picked up along the way.
No previous knowledge or exposure to the topic is required, but an intermediate understanding of Java SE is expected.
Infinum Android Talks #02 - How to write an annotation processor in AndroidInfinum
How to write your own annotation processor in Android Studio. Compile time and runtime annotations will be compared to reflection. To conclude, butterknife library will be presented as an example of using compile time annotation processing.
Spring Day | Spring and Scala | Eberhard WolffJAX London
2011-10-31 | 09:45 AM - 10:30 AM
Spring is widely used in the Java world - but does it make any sense to combine it with Scala? This talk gives an answer and shows how and why Spring is useful in the Scala world. All areas of Spring such as Dependency Injection, Aspect-Oriented Programming and the Portable Service Abstraction as well as Spring MVC are covered.
4 years ago, mid 2013, we have identified a gap in the cloud echo-system. The landscape of IaaS, PaaS and SaaS provides solutions for VMs, Container and Networking, platforms of different types for backend developers, Backends for mobile developers and ready made software for individuals and enterprises. What is missing in the middle is the platform for web-sites and web-apps.
4 years down the line, with the emergence of Serverless, there are still no players in this gap. We will talk about what makes a platform for web-sites and web-apps. Things frontend optimized javascript, SEO, visual builder, web methods & backend javascript as well as request time container boot.
We have built Wix Code over the last 4 years targeting this exact gap – a serverless platform for website and web applications, and so …
Wix is taking the risk of predicting the future of serverless computing and where it should be 4 years from now.
DevOps is not a Culture. It is about responsibilityYoav Avrahami
What is DevOps? Why is it so important for software organizations?
This is a short intro to the problem that DevOps tries to solve and how DevOps offers an alternative model of responsibility in an organization that solves this problem.
The JVM memory model describes how threads in the Java eco-system interact through memory. While the memory model impact on developing for the JVM may not be obvious, it is the cause for certain number of "anomalies" that are, well, by design.
In this presentation we will explore the aspects of the memory model, including things like reordering of instructions, volatile members, monitors, atomics and JIT.
Over the first 8 years of Wix, Wix infrastructure has gone a number of transformations, starting as a monolithic application server with MySQL, evolving to a service based architecture with with diverse infrastructure.
Over this 8 years journey, we have learned a thing or two - some DOs and some DON'Ts.
This presentation goes over the evolution of Wix architecture, with the different transformations we have done to support Wix at scale. We will share some of out insights about building a web infrastructure for over 50M users
Taking the role of a software architect for the last 10 years starting at a small startup moving to Amdocs OSS devision and then to Wix as chief architect, I have gained some understanding of what it makes to do architecture.
I can say today that software architecture is not about
* UML
* Those huge system box diagrams
* Writing documents
I count 4 different types of software architecture - each of the four is complex and can make a full presentation by itself.
+ System architecture - the actual layout of process on hosts - what is a service, number of instances, how services collaborate, etc.
+ Data architecture - the selection of data storage engines and their usage
+ Build architecture - the dependencies between different artifacts and their impact on development and deployment
+ Network architecture - the structure of your layer 1, 2 and 3 network with higher level services (Routers, VLANS, VPNs, etc).
I propose talking about software architecture - what is it, what practices and challenges an architect should focus on and how to bring value to an R&D organization. Resource management, self healing systems, containment of failure, architecture vs organization, etc.
Over the first 7 years of Wix, Wix infrastructure has gone a number of transformations, starting as a monolithic application server with MySQL, evolving to a service based architecture with with diverse infrastructure. During this period, we have learned a few things that we like to share, as well as gone by a number of transformations. We evolved into TDD / CI / CD and DevOps, changed our product from Flash to HTML 5 and started selling products. Let us take you on the Wix (technical) journey...
In this presentation we go over the motivations for wix.com R&D to move to a CI/CD/TDD model, how the model was implemented and the impact on Wix R&D. We will cover the tools used (developed in-house and 3rd party), change in methodologies, what we have learned during the transformation and the unexpected change in working with product and the rest of the company.
1. Playing with Java Classes
and Bytecode
What is a Class?
How it is loaded and used?
How can I write a code at runtime, using that code?
2. Java Classloading
• Why do we care?
– Because if we gonna write code at runtime, we better know how
to load it and use it…
– Because we do not really understand classes
• So… what identifies a class?
– It’s name
– It’s package
– It’s classloader
• It means that
– We can have multiple instances of a class loaded.
– Two instances of the same class from different classloaders are not compatible and not
assignable
– Static variables are static only in the context of a classloader, not globally as we are
always told.
3. Java Classloading
• So what is this classloader?
– It is a class written in Java (subclass of java.lang.ClassLoader), responsible to load other
classes for use by the JVM
– Arranged as a tree of classloaders
• Bootstrap classloader
– Loads the java system
• jre/lib/resources.jar – series of resource files
• jre/lib/rt.jar – the java.*, javax.*, etc packages
• jre/lib/sunrsasign.jar
• jre/lib/jsse.jar – secure socket extension
• jre/lib/jce.jar – java cryptography extension
• jre/lib/charsets.jar
• jre/classes
– The important stuff is in rt.jar – the base Java classes
Bootstrap
classloader
Ext
classloader
Application
/ System
classloader
4. Java Classloading
Commandline Java App Tomcat (6)
Bootstrap
classloader
Ext
classloader
Application
/ System
classloader
Application
/ System
classloader
Common
classloader
WAR 1
classloader
WAR 2
classloader
Loads the Application Jars
from the classpath
Loads just the
bootstrap.jar and
tomcat-juli.jar
Loads the tomcat
commons library jars
Loads the jars in the
webapp lib directory
6. What we gonna talk about
• Aspect Oriented Programming
– Java Proxy
– Spring Aspects
– AspectJ Aspects
• Doing the real cool stuff
– The bootstrap classloader
– The javaagent and class instrumentation
– Writing bytecode at runtime
7. The Java Proxy
• What it does
– It allows to implement one or more interfaces dynamically
• When to use it
– Generic implementation of an interface – such as in the case of client code calling a
service. The generic implementation can marshal the client method call to whatever
“on-the-wire” format, saving the developer from coding stubs.
– Simplistic aspect oriented programming – to catch method calls, perform some
pre/post/around logic and delegate the call to the real implementation. Can be used for
transaction handling, logging, etc.
• Limitations
– Can only function on Java interfaces
– Intercepts only calls done on the proxy instance. When used on
the delegate model, calls directly on the delegate will not be
intercepted.
9. Spring AOP
• What it does
– Allows to intercept method calls to Spring beans, without the requirement for an
interface
– Simplifies the code compared to the Java Proxy
– Works in the proxy model – spring beans factory returns a proxy to the bean we
intercept methods of
– Allows using AspectJ selectors to select the methods to intercept
– Aspects are written using the AspectJ Java syntax
• When to use it
– Aspect programming on Spring beans.
– Transaction handling, logging, security handling – anything AOP is good for
• Limitations
– Can only function on Spring Beans
– Intercepts only calls done on the proxy instance. Inner bean calls will
not be intercepted
– Supports only a small subset of AspectJ selectors – only the method
execution join points are supported.
10. Spring AOP
• Configuring Spring AOP
• Example Aspect
• Activating the Aspect
– Using Spring component scan, by adding the @Component annotation on the aspect
– Using Spring Beans XML
11. AspectJ
• What it does
– Allows to intercept any Join Point such as method calls, exceptions, etc
– Allows writing aspects using Java or AspectJ syntax
– Changes the actual class bytecode
– Allows using AspectJ selectors to select the methods to intercept
– Change existing classes, adding methods, members and super-interfaces to them
– Weaves aspects on load time or compile time
• When to use it
– Aspect programming on any Java object
– Transaction handling, logging, security handling – anything AOP is good for
– Introduce compiler like coding rules
• Limitations
– Requires custom Java Compiler (AspectJ compiler)
– Or requires custom definitions for load time weaving
(depending on run environment)
12. AspectJ
• Configuration options
– Compile time weaving
– Load time weaving
– Load time weaving with Spring
• Configuring AspectJ Load time weaving with Spring
– Simple, isn’t it?
• Well, there are additional requirements
• Dependencies
– aspectjrt.jar must be in the classpath
• (continues on next slide)
13. AspectJ
• META-INF/aop.xml
– declares the aspects for load time weaving and the target packages to weave into
• Class instrumentation
– Commandline & JUnit applications – using the -javaagent to load instrumentation jars
• -javaagent:spring-instrument-<version>.jar -javaagent:aspectjweaver-<version>.jar
– Maven running JUnit – using the maven-surefire-plugin, passing it the javaagent args
– Note the two javaagent params must be at the same line
– Tomcat 6 –
• The jar spring-instrument-tomcat-<version>.jar has to be copied to tomcats lib directory
• The webapp has to have a context.xml file in WEB-INFcontent.xml, with the minimum content
– Note there are alternative locations for the context file. This is the location I find easiest to use.
15. Intro to AOP (AspectJ Style)
• Aspect – a concern that cuts across multiple classes. Examples are logging,
transaction handling, security, etc.
• Join Point – a point during the execution of a program. Examples are when
– A method is executed
– A method is called
– A constructor is executed
– A constructor is called
– An exception handler is executed
– An advice is executed
– Static initialization is executed
– Initialization and pre-initialization of an object
• Advice – action taken by the aspect at a particular pointcut. Types of advice
– Before, after returning, after throwing, after finally, around
• Pointcut – a selector of pointcuts using predicates. An advice is associated with a
pointcut expression and runs on any point matched by the pointcut
16. Intro to AOP (AspectJ Style)
• More on pointcuts
– Pointcuts are query expressions on the code.
– Can be joined using the and (‘&&’), or (‘||’) or not (‘!’) operators
• Some pointcut predicates
– execution(…) – when a particular method body executes
– call(…) – when a method is called
– handler(….) – when a particular exception handler executes
– this(…) – when the currently executing object is of a certain type
– target(…) – when the target object is of type
– args(…) – when the method arguments match a certain type
– within(…) – when the executing code belongs to a certain class
– cflow(…) – when the program flow is in a certain method call (the method is a parent in
the stack)
– @annotation(…) – methods annotated with a certain annotation
– @target(…) – then the target executing object has a certain annotation
– @args(…) – when the runtime type of an argument has a certain annotation
– @within(…) – limits to pointcuts within a class that has a certain annotation
– bean(…) – a spring bean name
Not
Supported by
Spring AOP
Only Spring
AOP
17. Intro to AOP (AspectJ Style)
• Some examples
– execution (int *())
• joinpoints who are method executions for methods who return an int and do not take
parameters
– @annotation(com.example.Log) && execution(* *(..))
• joinpoints who are method executions for methods annotated with the log annotation,
regardless of the method return type, class, name or parameters
– call(public * *(..))
• Call to any public method
– !this(Point) && call(int *(..))
• any method call to an int method when the executing object is any type except Point.
– cflow(P) && cflow(Q)
• each join point that is in both the control flow of P and in the control flow of Q
18. Intro to AOP (AspectJ Style)
• Coding Aspects using AspectJ Java Syntax
• AspectJ AspectJ syntax
19. Intro to AOP (AspectJ Style)
• Adding code to existing classes using aspects (Inter-type declarations)
– Adding implemented interfaces
– Changing the superclass of a class
– Adding methods to a class
– Adding variable to a class
– Turning java into multiple inheritance system – change interfaces to include method
implementation and variables
• Using join points as compiler errors or warnings
• SoftExceptions
– catching an exception at a join point and replacing it with SoftException (subclass of
RuntimeException)
• Privileged aspects
– Can access private, package and protected members of classes, bypassing the java
member visibility constraints
20. Intro to AOP (AspectJ Style)
• Example of an aspect modifying a class
22. Bootstrap classloader
• What it does
– Allows to replace java system classes
– Bypasses all java security policy set by SecurityManager
• When to use it
– Snuff inside the core Java APIs
– Change core Java API functionality
– Terracotta uses it to replace the Java HashMap class with a distributed cache
implementation
• Limitations
– Classes loaded by the bootstrap classloader are not able to load classes not included in
the bootstrap classloader jar list
• Usage
– With the java commandline options
• –bootclasspath: - list of jars to use instead of the standard list
• –bootclasspath/a: - list of jars to append to the standard list
• –bootclasspath/p: - list of jars to prepend to the standard list
23. Javaagent and Instrument
• What it does
– Allows to instrument / transform / change a class definition
as it is loaded
• When to use it
– AspectJ uses it for load time weaving
– To modify classes as we load them
• Limitations
– It works on the class file bytes – requires intimate knowledge of the class file structure.
Not very useful without a framework like AspectJ, BCEL, cglib, etc.
• Usage
– Using the –javaagent java commandline parameter to introduce an agent jar
– The agent jar has to have a premain method
• public static void premain(String agentArguments, Instrumentation
instrumentation)
– The Instrumentation class allows to redefine a class,
add & remove transformers, and retransform a class.
– ClassFileTransformer has one method to transform a class
file bytes (as a byte[]).
24. Writing Bytecode
• What it does
– Allows to write new classes or modify existing classes at runtime
– Load the new/modified classes
– Instantiate them
– Use them as any other Java class
• When to use it
– Performance – when a mapping library (in my case XML to objects) was too slow
because of excessive use of reflections, coding simple mapping classes proved match
faster
– Implementing Proxies on any class – Spring AOP and Hibernate do just that
– Hibernate uses bytecode modification of classes to introduce mechanisms for lazy
loading of members
– Adaptor for Beans – a reporting framework used reflection on beans to read the field
definitions from in it’s Java Object datasource. When adapting a
java query interface that returns Map like objects, we had to
generate wrapper classes adapting the Map to the beans the
framework expected.
25. Writing Bytecode
• Limitations
– Hard to use
– Generated bytecode cannot be debugged normally - we do
not have the source code
• BCEL – I have found using BCEL to be easiest for me
• BCEL is included with JRE 1.6 under the package
com.sun.org.apache.bcel
– However, the JRE version did not work for me.
– I have used the apache version – 5.2
• Guidelines
– keep the generated code small. Use Helper methods / superclasses whenever you can.
Remember, you can debug helper classes or superclasses, but you cannot debug
generated bytecode.
– Classes can be generated at build time or runtime.
I have found runtime generation simpler to use
– Use class generation by example
• Good resource
– http://java.sys-con.com/node/49088
26. Writing Bytecode
• Class Generation by Example
– Write an example class of what you want to generate
– Compile it
– Run org.apache.bcel.util.BCELifier. It will create java code, that when running it, will
create a factory class that generates your example class.
– You can also use org.apache.bcel.util.Class2-HTML to get HTML documentation of your
example class bytecode.
– Modify the generated factory class to generate the bytecode for the classes you want
– Use org.apache.bcel.verifier.Verifier or
com.pavelvlasov.codegen.ClassGeneratorBase.verify() to verify that the generated
classes are valid
– Use Jad, Jadclipse or DJ Java Decompiler to decompile generated files and verify method
logic. I recommend that you set the "annotate" option so you can see the bytecode
instruction as comments.
• Class loading
– Code a new subclass of ClassLoader
– Use the defineClass (protected) method to define the new
generated class
– Remember to link your class loader to a parent class loader
27. Writing Bytecode
• Example – mapping objects from type A to type B
Using a superclass helper
–
– The example class
28. Writing Bytecode
• Running BCELifier on the class
– BCELifier has a main method, getting one parameter – the class name
• The created factory class
– Defining the class and the class members
29. Writing Bytecode
• Creating the Factory class (continued)
– Creating the constructor
– Which creates the constructor
30. Writing Bytecode
• Creating the Factory class (continued)
– Creating the mapToB method
public
Return type
Input arguments
Input argument names
Method name
Class name
31. Writing Bytecode
• Creating the Factory class (continued)
– Creating the mapToB method
Store the result as Object 2
Take Object 1 – the a parameter
(Object 0 is this)
Cast it to Person
32. Writing Bytecode
• Creating the Factory class (continued)
– Creating the mapToB method
Store the result as Object 3
Create new PersonDTO
Invoke the personDTO constructor
33. Writing Bytecode
• Creating the Factory class (continued)
– Creating the mapToB method
Pop the stack, writing the value to the name property of object 3
Load object 3 to the stack - Person
Read the name field from object 2 to the stack
Load object 2 to the stack - PersonDTO
34. Writing Bytecode
• Creating the Factory class (continued)
– Creating the mapToB method – we got another mapToB method
– The first has signature PersonDTO mapToB(Object)
– The second has signature Object mapToB(Object)
– The second overrides the superclass mapToB method and calls the first.
– Guess narrowing the return type of mapToB was not a really good idea, given generated
class loaded at runtime does not really benefit from the narrowed type constraint
–
35. Writing Bytecode
• Using the generated class
– The Class loader
– Creating and loading the new class
– Add a method returning JavaClass to the factory class
– And use it
Create the classloader
Create the class factory
Create the class bytecode
Define the new class
Load the new class
Get the new class constructor
Call the constructor