Cay S. Horstmann Chapter 3: Fundamental Programming Structures in Java .. The web site for this book at soundbefabnavi.tk contains all . buzzwords is at soundbefabnavi.tk pdf. Library of Congress Cataloging-in-Publication Data. Names: Horstmann, Cay S., author. Title: Core Java / Cay S. Horstmann. Description: Tenth edition. Core Java®. Volume II—Advanced Features. Tenth Edition. Cay S. Horstmann. Boston! Columbus! Indianapolis! New York! San Francisco! Amsterdam!.
|Language:||English, Spanish, German|
|Distribution:||Free* [*Registration Required]|
Core Java Volume I--Fundamentals (9th Edition): 1 (Core Series) Cay Horstmann identifies the problems experienced Java platform developers encounter. Core Java® Volume II—Advanced Features - Pearsoncmg Cay Horstmann identifies the problems experienced Java platform developers encounter most. Core Java / Cay S. Horstmann, Gary Cornell. put up a web site at http:// soundbefabnavi.tk with a FAQ, bug fixes, and PDF format, printing,
Description Copyright Dimensions: An Introduction to Java 1 1. The Java Programming Environment 17 2. Fundamental Programming Structures in Java 41 3. Objects and Classes 4.
Inheritance 5. The Cosmic Superclass 5. Interfaces, Lambda Expressions, and Inner Classes 6. Exceptions, Assertions, and Logging 7. Generic Programming 8. Collections 9. Graphics Programming Event Handling User Interface Components with Swing Deploying Java Applications Concurrency More Information.
Unlimited one-month access with your download. See Details. Request an Instructor or Media review copy. Other Things You Might Like. Python for Programmers By Paul J. Join Sign In. All rights reserved. After compilation. Annotations do not change the way in which your programs are compiled. The Java compiler generates the same virtual machine instructions with or without the annotations.
You need to use annotations that your processing tool understands, then apply the processing tool to your code. Here are some uses for annotations: We will mark methods as event listeners for AWT components, and show you an annotation processor that analyzes the annotations and hooks up the listeners.
Here is an example of a simple annotation: The annotation Test annotates the checkRandomInsertions method. The name of each annotation is preceded by an symbol, similar to Javadoc comments. By itself, the Test annotation does not do anything. It needs a tool to be useful. For example, the JUnit 4 testing tool available at http: These elements can be processed by the tools that read the annotations. The methods of the interface correspond to the elements of the annotation.
Target ElementType. The interface declaration creates an actual Java interface. Tools that process annotations receive objects that implement the annotation interface. A tool would call the timeout method to retrieve the timeout element of a particular Test annotation. The Target and Retention annotations are meta-annotations. You have now seen the basic concepts of program metadata and annotations. Annotating Event Handlers One of the more boring tasks in user interface programming is the wiring of listeners to event sources.
Many listeners are of the form. The annotation. The classes Method. That is the job of the ActionListenerInstaller class. The code is in Listing We retrieve it by calling the source method. This shows a limitation of our annotation. Chapter 8. The static processAnnotations method enumerates all methods of the object it received.
In this section. For each method. We now need a mechanism to analyze them and install action listeners. It cannot be a local variable.
In fact. The details are not important. In this example. EventHandler class tries to do just that. You can see an example in Section Figure For more information about proxies. A utility method for adding a listener could be just as convenient for the programmer as the annotation.
The key observation is that the functionality of the annotations was established by the processAnnotations method. It is also possible to process them at the source level. This sounds complex. For each annotated method. Our example was not intended as a serious tool for user interface programmers.
You could make the class truly useful by supplying a method that adds the event handler instead of just constructing it. Having seen a concrete example. AnnotatedElement 5. If no annotations are present, an array of length 0 is returned.
For example, the following annotation has two elements, assignedTo and severity: Two special shortcuts can simplify annotations. The other shortcut is the single value annotation. Defaults are not stored with the annotation. String assignedTo default "[none]". The type of an annotation element is one of the following: The methods of an annotation interface can have no parameters and no throws clauses.
Method m. See the API notes at the end of this section for the methods provided by this interface. You never supply classes that implement annotation interfaces. You cannot extend annotation interfaces. Annotation interface. In other words. The element declarations in the annotation interface are actually method declarations. That interface is a regular interface.
Reference ref default Reference. Not even a default of null is permissible. If an element value is an array.
An annotation element can never be set to null. It is an error to introduce circular dependencies in annotations. You can add annotations to the following items: BugReport has an element of the annotation type Reference. This can be rather inconvenient in practice. If this is a problem. An item can have multiple annotations.
Annotation 5. Note that calling getClass on an annotation object would return the actual class. You cannot use the same annotation type more than once when annotating a particular item. Generated All Marks an item as source code that has been generated by a tool. Override Methods Checks that this method overrides a superclass method.
PostConstruct PreDestroy Methods The marked method should be invoked immediately after construction or before removal.
Four of them are meta-annotations that describe the behavior of annotation interfaces. The others are regular annotations that you can use to annotate items in your source code. SuppressWarnings All but packages and annotations Suppresses warnings of the given type.
Resource Classes. The Generated annotation is intended for use by code generator tools. A date string in ISO format and a comment string are optional. SuppressWarnings "unchecked" The Override annotation applies only to methods.
The compiler will warn when you use a deprecated item. The Resource annotation is intended for resource injection. Any generated source code can be annotated to differentiate it from programmer-provided code. In the web application. Methods tagged with these annotations should be invoked immediately after an object has been constructed or immediately before it is being removed. The compiler checks that a method with this annotation really overrides a method from the superclass.
The SuppressWarnings annotation tells the compiler to suppress warnings of a particular type. After all.. Generated "com. This annotation has the same role as the deprecated Javadoc tag. You can specify at most one of the values in Table In Listing They belong to the enumerated type ElementType. You can specify any number of element types. In the following two sections. The compiler checks that you apply an annotation only to a permitted item. The default is RetentionPolicy. An annotation without an Target restriction can be applied to any item.
The Documented meta-annotation gives a hint to documentation tools such as Javadoc. When a class has an inherited annotation. The use of other annotations is not included in the documentation.
If an annotation is transient such as BugReport. The Inherited meta-annotation applies only to annotations for classes. This makes it easy to create annotations that work as marker interfaces. Documented Target ElementType. An annotation describes this fact better than does interface inheritance. It is legal to apply an annotation to itself.
Then the subclasses of persistent classes are automatically annotated as persistent. Modern versions of Java EE use annotations to greatly simplify the programming model. In the past. We write a program that automatically produces bean info classes.
You tag bean properties It would have made sense to declare the editor element to have type Class. To automatically generate the bean info class of a class with name BeanClass. Properties can have optional property editors. The bean info class lists all of the properties of the bean. To eliminate the drudgery of writing bean info classes. You can tag either the property getter or setter. Recall from Chapter 8 that a bean info class describes a bean more precisely than the automatic introspection process can.
We analyze the annotation at the source level only. Each annotation processor is executed in turn Write the code for returning an array of all property descriptors. In Java SE 5. If the property has an editor.
It then selects the annotation processors that should be applied. We do not discuss the annotation processing API in detail. All this is easy enough to do. PropertyDescriptor "titlePosition". To invoke annotation processing. In the process method. Note that we create the output writer as follows: Here is the outline of the code: SupportedAnnotationTypes "com. You need to specify which annotations your processor supports. The process method has two parameters: The designers of the API themselves love annotations.
The BeanInfoAnnotationProcessor has a single public method. Property] last round: In the companion code for this book. To see the annotation processing in action.
Compile the annotation processor: You will get this output: Round 1: HTML documentation. An annotation is intended as a description about a code item. It would be possible to build another tool for this purpose.
Annotation processors may choose to generate XML descriptors. Some people have suggested using annotations to remove an even bigger drudgery. PropertyDescriptor getPropertyDescriptors ". PropertyDescriptor ". Locate all methods. Aug There is a third possibility: IntrospectionException e ". Unless annotations are removed at the source level. The format is rather complex. V Inserting these bytecodes sounds tricky.
As this chapter was written. The important point is that the program in Listing Item Try running javap -c set. EntryLogger set. If you are interested in the details of bytecode engineering. You can see the inserted instructions at the beginning of the hashCode. I The SetTest program in Listing Annotations are used to add directives to a program. This example shows the power of bytecode engineering. EntryLogger classname". An attractive alternative is to defer the bytecode engineering until load time.
Instrumentation instr This method is called when the agent is loaded. Here are the steps required to build an agent: The instr parameter can be used to install various hooks.
Implement a class with a method public static void premain String arg. The agent code can carry out initializations in a premain method. The agent can get a single command-line argument. Item -classpath. EntryLoggingAgent 3. If so. The transformer must be installed before the main method of the program is called. SetTest The Item argument is the name of the class that the agent should modify.
On the other hand. In the next chapter. This chapter gave you the background for understanding the inner workings of the annotation tools you will encounter. Index Symbols and Numbers. Double classes. Float classes. Index ActivationSystem interface. Javadoc comments. Double class. Index Attribute sets. C types. Index Business logic. Java types. Index Child elements XML. The Kahn. Principles and Practice Foley. Index Colors components of. The Dumas. Index tabbed panes for.
URL prefix. Index fileName property. Index of CharBuffer. Index getImage method TrayIcon. Index getRotateInstance method AffineTransform. GetStaticXxxField functions C. The Menezes et al. Index of Annotation. Index InputStream class. BufferedImage API. Double API. Color API. Path2D API. Component API. Printable API.
Float API. Graphics API. PrinterJob API. BasicStroke API. TexturePaint API. Graphics2D API. AffineTransform API. ColorModel API. FlavorListener API. TrayIcon API. Book API. AlphaComposite API. Index J J long. RenderingHints API. Clipboard API. Transferable API. Toolkit API.
Desktop API. GeneralPath API. TextLayout API. SystemTray API. Area API. RescaleOp API. Kernel API. ClipboardOwner API. GradientPaint API. LookupOp API. SplashScreen API. Raster API. ConvolveOp API. WritableRaster API. PageFormat API. DataFlavor API. AnnotatedElement API. System API. Annotation API. Appendable API. ByteBuffer API. Charset API. InetAddress API. Readable API. Introspector API. DataInput API.
Files API. Expression API. CharBuffer API. SecurityManager API. Thread API. SocketChannel API. FileChannel API. Channels API. InputStream API. ClassLoader API. OutputStream API. Statement API. Encoder API. Socket API. PropertyEditor API. Flushable API. BeanInfo API. PropertyDescriptor API. URIs in. Customizer API. FileLock API. Closeable API. ServerSocket API. ExceptionListener API. Buffer API. BeanDescriptor API. PrintWriter API. CharSequence API. PersistenceDelegate API. DataOutput API. Class API.
File API. FeatureDescriptor API. Attribute API. PrivilegedAction API. Bindings API. ResourceBundle API. ZipEntry API. TimeZone API. Pattern API. ScriptEngine API. Collator API. ResultSet API.
KeyGenerator API. PrintService API.
DriverManager API. CompiledScript API. Paths API. DateFormat API. ImageReader API. Invocable API. Normalizer API. CodeSource API. Cipher API. ActivationDesc API. FileSystems API. MessageFormat API. NumberFormat API. ProtectionDomain API. Naming API. FileSystem API. ActivationGroup API. Activatable API. Locale API. Connection API.
InitialContext API. MessageDigest API. PreparedStatement API.
Savepoint API. Currency API. DataTruncation API. Permission API. SimpleDoc API. ActivationSystem API. Context API. AttributeSet API. ZipFile API. CallbackHandler API. PasswordCallback API.
ScriptContext API. NameCallback API. Principal API. Matcher API. Format API. Compilable API. CollationKey API. Path API. Blob API. MarshalledObject API. Clob API. ImageWriter API. TreeModel API.
DocumentListener API. JList API. DocumentFilter API. ProgressMonitor API. RowSet API. DropLocation API. Tool API. DocumentEvent API. HyperlinkEvent API. CompilationTask API. JLayer API. JavaCompiler API. TreePath API. DocumentBuilder API. AbstractFormatter API. JTree API. MaskFormatter API. LoginContext API. TableColumn API. ListModel API. Entry API. DateEditor API. Subject API. DefaultFormatter API. TreeNode API.
TransferHandler API. JSpinner API. DiagnosticCollector API. JTable API. HyperlinkListener API. TransferSupport API. JComponent API. Document API. JFrame API. CellEditor API.
LoginModule API. TableModel API. Diagnostic API. RowFilter API. TransformerFactory API. StreamSource API. StreamResult API. Index javax. Base64 class. Transformer API. XPath API. DropLocation class getIndex method. DropLocation class getColumn.
DropLocation class getChildIndex method.