Introspection the process by which a builder tool finds out which properties, methods, and events a bean supports. searching for classes and methods that.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 4 Writing Java Applications, Java Development Tools.
Advertisements

Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Java Virtual Machine (JVM). Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem.
Lab Information Security Using Java (Review) Lab#0 Omaima Al-Matrafi.
Java Beans & Serialization CS-328 Dick Steflik. Java Beans Java based component technology –originally developed to provide java with a component technology.
Objects and Classes First Programming Concepts. 14/10/2004Lecture 1a: Introduction 2 Fundamental Concepts object class method parameter data type.
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
Introduction to Java Programming
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 1, Lab.
Unit 061 Java Virtual Machine (JVM) What is Java Virtual Machine? The Class Loader Subsystem Linking oVerification oPreparation oResolution Class Initialization.
Introduction to Java.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects Real and Java COMP.
Programming in Java; Instructor:Moorthy Introduction, Objects, Classes, Libraries1 Programming in Java Introduction.
Introduction to Java Tonga Institute of Higher Education.
Programming Languages and Paradigms Object-Oriented Programming.
M1G Introduction to Programming 2 4. Enhancing a class:Room.
Advanced Java Programming Lecture 5 Reflection dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
Java Programming Robert Chatley William Lee
An intro to programming. The purpose of writing a program is to solve a problem or take advantage of an opportunity Consists of multiple steps:  Understanding.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A.
JAVA BASICS: Variables and References SYNTAX, ERRORS, AND DEBUGGING.
POS 406 Java Technology And Beginning Java Code
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
CS591x A very brief introduction to Java. Java Developed by Sun Microsystems was intended a language for embedded applications became a general purpose.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Java Beans. Definitions A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification) The JavaBeans.
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
Chapter 4 Software. Chapter 4: Software Generations of Languages Each computer is wired to perform certain operations in response to an instruction. An.
Overview of Java CSCI 392 Day One. Running C code vs Java code C Source Code C Compiler Object File (machine code) Library Files Linker Executable File.
CSCE 314 Programming Languages Reflection Dr. Hyunyoung Lee 1.
Java FilesOops - Mistake Java lingoSyntax
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
CSCI 212 Object-Oriented Programming in Java. Prerequisite: CSCI 111 variable assignment statement while loop for loop post-increment (i++) strong typing.
Lecture1 Instructor: Amal Hussain ALshardy. Introduce students to the basics of writing software programs including variables, types, arrays, control.
Java How to Program, 9/e Presented by: José M. Reyes Álamo © by Pearson Education, Inc. All Rights Reserved.
Reflection Mehdi Einali Advanced Programming in Java 1.
Reflections CSC207 – Software Design. Background Turing's great insight: programs are just another kind of data. Source code is text that is interpreted.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
Introduction to java (class and object). Programming languages: –Easier to understand than CPU instructions –Needs to be translated for the CPU to understand.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
CS-140 Dick Steflik Lecture 3. Java C++ Interpreted optimized for the internet Runs on virtual ized machine Derived from C++ Good object model Widely.
Lecture 1b- Introduction
The need for Programming Languages
Java Yingcai Xiao.
Programming languages
Programming Language Hierarchy, Phases of a Java Program
Java Beans Sagun Dhakhwa.
Internet and Java Foundations, Programming and Practice
CSE 413, Autumn 2002 Programming Languages
C# and the .NET Framework
Lecture 1 Runtime environments.
2.1. Compilers and Interpreters
Advanced Programming in Java
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA
CMP 131 Introduction to Computer Programming
Introduction to javadoc
(Computer fundamental Lab)
Lecture 1 Runtime environments.
Chap 1. Getting Started Objectives
Chap 4. Programming Fundamentals
CSCE 314: Programming Languages Dr. Dylan Shell
Exception Handling.
Presentation transcript:

Introspection the process by which a builder tool finds out which properties, methods, and events a bean supports. searching for classes and methods that follow certain naming conventions by querying the BeanInfo of a class BeanInfo is an interface whose methods allow the interrogation of a beans properties, methods and events associated class that implements the BeanInfo interface Homework : read about the BeanInfo interface [Not part of your exam] Good Java Programming 6/4/2018

JVM Basics Use of java compiler (javac) to generate a binary format Stored in files with a .class extension. The bytecode is machine code for a special kind of computer JVM The binary class format is defined by the JVM This is called a virtual machine because it's been designed for implementation in software, rather than hardware. Every JVM used to run Java platform applications is built around an implementation of this machine. Good Java Programming 6/4/2018

JIT: Just in Time EarlyJVMs were basically interpreters for the virtual machine bytecode. Difference between an Interpreter and Compiler? Homework Hint: Interpreting code is much slower than executing native code just-in-time (JIT) translation. The JIT technique compiles Java bytecode to native code before executing it for the first time giving much better performance for repeated executions. Current JVMs use adaptive techniques to monitor program execution and selectively optimize heavily used code. What is the 90/10 rule? What is Java HotSpot? Homework [Not included in the exam] How can the HotSpot be “warmed”? Good Java Programming 6/4/2018

Detour: linking in C/C++ Lab1.exe: lab1.o g++ lab1.o -o lab1.exe lab1.o: lab1.cpp g++ -c lab1.cpp clean: rm *.o Which line represents the linking step? Good Java Programming 6/4/2018

Linking: Java vs C/C++ C/C++ require linking step compile + link: to get an executable This linking process merges code from separately compiled source files, along with shared library code, to form an executable program. In Java, the classes generated by the compiler generally remain just as they are until they're loaded into a JVM. A JAR is just a container for the class files. Good Java Programming 6/4/2018

Loading a Java class java -verbose:class AnyClassName Example: java –verbose:class Driver prints a trace of the class loading process Homework run this command on bingsuns for any Java class generated in the assignment so far java –verbose:class Driver NOT included in the Exam [Not included in the exam] Good Java Programming 6/4/2018

Java Reflection Provides user code access to internal information for classes loaded into the JVM Allows design of user code that works with classes selected during execution, not in the source code. Great tool for building flexible applications. High performance penalty Reflection is used by JavaBeans Introspector Package: java.lang.reflect Good Java Programming 6/4/2018

Getting Started with Reflection Given the following code, write code in any programming language to invoke a method, setSenderName, on an object of type stored in the string “className” String className = “ChatMessage”; Good Java Programming 6/4/2018

Class in Java The Class object provides hooks for reflection. It has metadata information on interfaces implemented by the class. details of the constructors fields methods defined by the class. Good Java Programming 6/4/2018

Getting Started with Reflection Get a hold of java.lang.Class instance Class class = ChatMessage.class; The class is automatically loaded with this statement Good Java Programming 6/4/2018

Reading a Class at run-time Suppose just the name of the class is available as a string Class javaClass = null; String nameOfClass = “ChatMessage”; try { javaClass = Class.forName(nameOfClass); } catch (ClassNotFoundException ex) { // take care of the exception } Good Java Programming 6/4/2018

Get a list of methods in a Class getDeclaredMethods(Class className); examples taken from java.sun.com try { Class c = Class.forName(“ChatMessage”); Method m[] = c.getDeclaredMethods(); for (int i = 0; i < m.length; i++) System.out.println(m[i].toString()); } } catch (Throwable e) { System.err.println(e); // take care of the exception http://java.sun.com/developer/technicalArticles/ALT/Reflection/index.html#4 Good Java Programming 6/4/2018

Parameters of a Method Class paramList[] = m.getParameterTypes(); getParameterTypes() on Method Class paramList[] = m.getParameterTypes(); http://java.sun.com/developer/technicalArticles/ALT/Reflection/index.html#4 Good Java Programming 6/4/2018

Fields getDeclaredFields() on Class try { Class cls = Class.forName(“ChatMessage"); Field fieldlist[] = cls.getDeclaredFields(); for (int i = 0; i < fieldlist.length; i++) { Field fld = fieldlist[i]; System.out.println("name = " + fld.getName()); System.out.println("type = " + fld.getType()); } http://java.sun.com/developer/technicalArticles/ALT/Reflection/index.html#4 Good Java Programming 6/4/2018

Invoking a method // need to get the Class information Object obj = someClass.newInstance(); // need to get the correct “Method” // need to get the correct parameters Object result = meth.invoke(obj, params); Good Java Programming 6/4/2018

Invoking a method String clsName = “ChatMessage”; Class cls = Class.forName(clsName); Class[] signature = new Class[1]; signature[0] = Integer.class; Method meth = cls.getMethod(methodName, signature); Object obj = cls.newInstance(); Object[] params = new Object[1]; params[0] = new Integer(7); Object result = meth.invoke(obj, params); Good Java Programming 6/4/2018

Invoking a method: new version // to avoid the warning that says using the –Xlint flag String clsName = “ChatMessage”; Class<?> cls = Class.forName(clsName); Class<?>[] signature = new Class<?>[1]; signature[0] = Integer.class; Method meth = cls.getMethod(methodName, signature); Object obj = cls.newInstance(); Object[] params = new Object[1]; params[0] = new Integer(7); Object result = meth.invoke(obj, params); Good Java Programming 6/4/2018

Invoking a method Given the following String clsName = “ServerDetails”; use reflection to invoke a method that is equivalent to: ServerDetails sd = new ServerDetails(); sd.setHostName(“bingsuns”); Good Java Programming 6/4/2018

Java Reflection: calling a constructor Class[] types = new Class[] { String.class, String.class }; Constructor cons = ChatMessage.class.getConstructor(types); Object[] args = new Object[] { "a", "b" }; ChatMessage ts = (ChatMessage)cons.newInstance(args); http://www.ibm.com/developerworks/library/j-dyn0603/ Good Java Programming 6/4/2018

Details of a Fields // given objectInstance, an instance of “ChatMessage” Class chatMessageClass= objectInstance.getClass(); Field[] fieldList = chatMessageClass.getFields(); for (int j=0; j<fieldList.length; j++) { Class fieldClass = fieldList[j].getType(); String fieldName = fieldList[j].getName(); Object fieldObject = fieldList[j].get(objectInstance); } Good Java Programming 6/4/2018

Classwork Given the following information about a class invoke the following: one setX method (takes a string and returns void) on getX method (takes void and returns string) for every data-member “x” in the class, there is a setX and getX method You are given the following: String className = “TestObject”; Good Java Programming 6/4/2018