1 Example Uses of Java Reflection Explained Simply.

Slides:



Advertisements
Similar presentations
1 Dynamic Proxies Explained Simply. 2 Dynamic Proxies License Copyright © 2008 Ciaran McHale. Permission is hereby granted, free of charge, to any person.
Advertisements

1 Copyright © 2005, Oracle. All rights reserved. Introducing the Java and Oracle Platforms.
 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
Open Source Software – Lessons Learned
© 2006 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice Open Source Software A Commercial.
28/1/2001 Seminar in Databases in the Internet Environment Introduction to J ava S erver P ages technology by Naomi Chen.
Input Validation For Free Text Fields ADD Project Members: Hagar Offer & Ran Mor Academic Advisor: Dr Gera Weiss Technical Advisors: Raffi Lipkin & Nadav.
Java Programming, 3e Concepts and Techniques Chapter 1 An Introduction to Java and Program Design.
Presented by IBM developer Works ibm.com/developerworks/ 2006 January – April © 2006 IBM Corporation. Making the most of Creating Eclipse plug-ins.
Java Server Pages B.Ramamurthy. Java Server Pages Servlets are pure Java programs. They introduce dynamism into web pages by using programmatic content.
J2EE Java 2 Enterprise Edition. Relevant Topics in The Java Tutorial Topic Web Page JDBC orial/jdbc
M.Sc. Course, Dept. of Informatics and Telecommunications, University of Athens S.Hadjiefthymiades “Web Application Servers” Basics on WAS WAS are necessary.
L EC. 01: J AVA FUNDAMENTALS Fall Java Programming.
Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.
Tomcat Celsina Bignoli History of Tomcat Tomcat is the result of the integration of two groups of developers. – JServ, an open source.
UNIT-V The MVC architecture and Struts Framework.
Chapter 9 Introduction to ActionScript 3.0. Chapter 9 Lessons 1.Understand ActionScript Work with instances of movie clip symbols 3.Use code snippets.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
Content Management Systems …mostly Umbraco ALL ABOUT.
Java Programming, 2E Introductory Concepts and Techniques Chapter 1 An Introduction to Java and Program Design.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
Lesson 13: Building Web Forms Introduction to Adobe Dreamweaver CS6 Adobe Certified Associate: Web Communication using Adobe Dreamweaver CS6.
FPGA and ASIC Technology Comparison - 1 © 2009 Xilinx, Inc. All Rights Reserved How do I Get Started with PlanAhead?
® IBM Software Group © 2012 IBM Corporation OPTIM Data Studio – Jon Sayles, IBM/Rational November, 2012.
1 Multi-threaded Performance Pitfalls Ciaran McHale.
Conditions and Terms of Use
Adding Genes This presentation gives a quick overview on how to add Genes to Osprey.
Introduction to J2EE Architecture Portions by Kunal Mehta.
Java Server Pages A JSP page is a text-based document that contains two types of text: static template data, which can be expressed in any text-based format,
© 2006 IBM Corporation IBM WebSphere Portlet Factory Architecture.
Blue Diamond Scott Auge Amduus Information Works, Inc.
Chapter 34 Java Technology for Active Web Documents methods used to provide continuous Web updates to browser – Server push – Active documents.
The Basics of Javadoc Presented By: Wes Toland. Outline  Overview  Background  Environment  Features Javadoc Comment Format Javadoc Program HTML API.
Digital Intuition Cluster, Smart Geometry 2013, Stylianos Dritsas, Mirco Becker, David Kosdruy, Juan Subercaseaux Setup Instructions Overview 1. License.
IBIS-AMI and Direction Decisions
Web Services with Apache CXF Part 2: JAXB and WSDL to Java Robert Thornton.
Object-Oriented Design Simple Program Design Third Edition A Step-by-Step Approach 11.
Copyright © 2002 ProsoftTraining. All rights reserved. JavaServer Pages.
Eclipse 24-Apr-17.
Unix Shell Basics Edited from Greg Wilson's "Software Carpentry"
Apache Jakarta Project. What is Jakarta’s mission Jakarta is a Project of the Apache Software Foundation, charged with the creation and maintenance of.
User Profiling using Semantic Web Group members: Ashwin Somaiah Asha Stephen Charlie Sudharshan Reddy.
Compiler Construction (CS-636)
Introduction Selenium IDE is a Firefox extension that allows you to record, edit, and debug tests for HTML Easy record and playback Intelligent field selection.
JAVA BEANS JSP - Standard Tag Library (JSTL) JAVA Enterprise Edition.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
CSCE 314 Programming Languages Reflection Dr. Hyunyoung Lee 1.
Build Systems Presentation December 14, 2015 Noon-1pm Kathy Lee Simunich Bldg. 203/ D120 Brought to you by: Argonne Java.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
Digital Intuition Cluster, Smart Geometry 2013, Stylianos Dritsas, Mirco Becker, David Kosdruy, Juan Subercaseaux Search Basics – Templates Tutorial Overview.
Chapter 14 Using JavaBeans Components in JSP Documents.
10 Copyright © 2004, Oracle. All rights reserved. Building ADF View Components.
Plug-In Architecture Pattern. Problem The functionality of a system needs to be extended after the software is shipped The set of possible post-shipment.
Spell Checker web service (you build a web client that interacts with the service) The client uses a servlet class and a JSP page. The user passes information.
Using Ant in Eclipse Dwight Deugo Nesa Matic
Lecture Transforming Data: Using Apache Xalan to apply XSLT transformations Marc Dumontier Blueprint Initiative Samuel Lunenfeld Research Institute.
Chapter 2 Build Your First Project A Step-by-Step Approach 2 Exploring Microsoft Visual Basic 6.0 Copyright © 1999 Prentice-Hall, Inc. By Carlotta Eaton.
Introduction to Algorithm. What is Algorithm? an algorithm is any well-defined computational procedure that takes some value, or set of values, as input.
INTRO. To I.T Razan N. AlShihabi
Chapter 13 Web Application Infrastructure
Jester – The JUnit Test Tester
Project 1 Introduction to HTML.
Open Source Software: Top 10 Myths that Every In-House, Government, & Private Practice Lawyer Should Know 2017 NAPABA Convention Washington, D.C.
Mini Robot Chassis Top View
Many-core Software Development Platforms
Automation in an XML Authoring Environment
The JSF Tools Project – WTP (internal) release review
© Healthcare Inspirations. All rights reserved
Presentation transcript:

1 Example Uses of Java Reflection Explained Simply

2 Example Uses of Java Reflection License Copyright © 2008 Ciaran McHale. Permission is hereby granted, free of charge, to any person obtaining a copy of this training course and associated documentation files (the “Training Course"), to deal in the Training Course without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Training Course, and to permit persons to whom the Training Course is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Training Course. THE TRAINING COURSE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE TRAINING COURSE OR THE USE OR OTHER DEALINGS IN THE TRAINING COURSE.

3 1. Basic uses of Java reflection

4 Example Uses of Java Reflection Ant Ant reads build (compilation) instructions from an XML file Ant is hard-coded to know how to process top-level elements - property, target, taskdef and so on Each Ant task (used inside target elements) is a plug-in - See example Ant build file on the next slide for examples of tasks Many task plug-ins are bundled with the Ant distribution ( jar, javac, mkdir, …) - A properties file provides a mapping for task-name  class-that-implements-task Users can use taskdef to tell Ant about user-written tasks - See example on the next slide

5 Example Uses of Java Reflection Example Ant build file

6 Example Uses of Java Reflection Auto-completion in a text editor Some Java editors and IDEs provide auto-completion - Example: you type “someObj.” and a pop-up menu lists fields and methods for the object’s type The pop-up menu is populated by using Java reflection

7 Example Uses of Java Reflection JUnit JUnit 3 uses reflection to find methods whose names start with “test” The algorithm was changed in JUnit 4 - Test methods are identified by an annotation (Annotations were introduced in Java 1.5) - Reflection is used to find methods with the appropriate annotation

8 Example Uses of Java Reflection Spring Below is an extract from a Spring configuration file:...

9 Example Uses of Java Reflection Spring (cont’) Spring uses reflection to create an object for each bean - The object’s type is specified by the class attribute By default, the object is created with its default constructor - You can use constructor-arg elements (nested inside bean ) to use a non-default constructor After an object is constructed, each property is examined - Spring uses reflection to invoke obj.setXxx(value) -Where Xxx is the capitalized name of property xxx - Spring uses reflection to determine the type of the parameter passed to obj.setXxx() - Spring can support primitive types and common Collection types - The ref attribute refers to another bean identified by its id

10 2. Code generation and bytecode manipulation

11 Example Uses of Java Reflection Code generators Most compilers have the following architecture Java’s reflection metadata is conceptually similar to a parse tree You can build a Java code generation tool as follows: - Do not write a Java parser. Instead run the Java compiler - Treat generated.class files as your parse tree - Use reflection to navigate over this “parse tree” Generated files parse tree Back-end code generator parser compiler input file

12 Example Uses of Java Reflection Code generators (cont’) Compile-time code generation in a project: - Use technique described on previous slide to generate code - Then run Java compiler to compile generated code - Use Ant to automate the code generation and compilation Runtime code generation: - Use techniques described on previous slide to generate code - Then invoke a Java compiler from inside your application: -Can use (non-standard) API to Sun Java compiler -Provided in tools.jar, which is shipped with the Sun JDK -Or can use Janino (an open-source, embeddable, Java compiler) -Hosted at - Finally, use Class.forName() to load the compiled code

13 Example Uses of Java Reflection Uses for runtime code generation Runtime code generation is used… By JSP (Java Server Pages) - To generate servlets from.jsp files By IDEs and debuggers - To evaluate Java expressions entered by user

14 Example Uses of Java Reflection Uses for Java bytecode manipulation Compilers: - Write a compiler for a scripting language and generate Java bytecode -Result: out-of-the-box integration between Java and the language - Groovy (groovy.codehaus.org) uses this technique Optimization: - Read a.class file, optimize bytecode and rewrite the.class file Code analysis: - Read a.class file, analyze bytecode and generate a report Code obfuscation: - Mangle names of methods and fields in.class files Aspect-oriented programming (AOP): - Modify bytecode to insert “interception” code - Generate proxies for classes or interfaces - Spring uses this technique

15 Example Uses of Java Reflection Tools for bytecode manipulation Example open-source projects for bytecode manipulation: - ASM ( - BCEL ( - SERP (serp.sourceforge.net) CGLIB (Code Generation LIBrary): - Built on top of BCEL - Provides a higher-level API for generating dynamic proxies - Used by other tools, such as Spring and Hibernate

16 3. Summary

17 Example Uses of Java Reflection Summary A lot of tools use Java reflection: - Plugins to extend functionality of an application (Ant) - Auto-completion in Java editors and IDEs - Use naming conventions of methods to infer semantics (JUnit test methods) - Tie components together (Spring) - Compile-time code generation - Runtime code generation -Generate proxies -Generate servlets from a markup language (JSP) - Evaluate Java expressions entered interactively by a user