Thinking in Java——筆記(20)

来源:http://www.cnblogs.com/apolloqq/archive/2016/12/27/6226534.html
-Advertisement-
Play Games

Annotations ___ They provide information that you need to fully describe your program, but that cannot be expressed in Java. In general the kind of an ...


Annotations


  • They provide information that you need to fully describe your program, but that cannot be expressed in Java.
  • In general the kind of annotations you add and what you do with them are entirely up to you.
  • Java SE5 contains three generalpurpose built-in annotations.
  • Four additional annotation types support the creation of new annotations.
  • Annotations are true language constructs and hence are structured, and are type-checked at compile time.

Basic syntax

  • Syntactically, annotations are used in much the same way as modifiers.

Defining annotations

  • Annotation definitions look a lot like interface definitions. In fact, they compile to class files like any other Java interface.
  • **@Target** defines where you can apply this annotation (a method or a field).
  • **@Retention** defines whether the annotations are available in the source code (SOURCE), in the class files (CLASS), or at run time (RUNTIME).
  • Elements look like interface methods, except that you can declare default values.

Meta-annotations

  • The meta-annotations are for annotating annotations.
  • Most of the time, you will be defining your own annotations and writing your own processors to deal with them.

Writing annotation processors

  • An important part of the process of using annotations is to create and use annotation processors.
  • Java SE5 provides extensions to the reflection API to help you create these tools.

Annotation elements

  • Note that you are not allowed to use any of the wrapper classes, but because of autoboxing this isn’t really a limitation.
  • You can also have elements that are themselves annotations.

Default value constraints

  • No element can have an unspecified value. This means that elements must either have default values or values provided by the class that uses the annotation.
  • non-primitive type elements are allowed to take null as a value, either when declared in the source code or when defined as a default value in the annotation interface.

Generating external files

  • Annotations are especially useful when working with frameworks that require some sort of additional information to accompany your source code.
  • After defining a Java class, the programmer must undergo the tedium of respecifying information like the name, package and so on—information that already exists in the original class.
  • If you define an element on an annotation with the name value, then as long as it is the only element type specified you don’t need to use the name-value pair syntax; you can just specify the value in parentheses.

Annotations don’t support inheritance

  • You cannot use the extends keyword with **@interfaces**. There doesn’t seem to be any suggestion of annotations supporting inheritance in future releases.

Implementing the processor

  • There is no inheritance of annotation interfaces, using getDeclaredAnnotations( ) is the only way you can approximate polymorphic behavior.

Using apt to process annotations

  • apt is designed to be run on Java source files rather than compiled classes. By default, apt compiles the source files when it has finished processing them.
  • The apt tool can easily group several annotation processors together. It allows you to specify multiple classes to be processed, which is a lot easier than having to iterate through File classes yourself.
  • apt works by using an AnnotationProcessorFactory to create the right kind of annotation processor for each annotation it finds.
  • When you run apt, you specify either a factory class or a classpath where it can find the factories it needs.
  • When you create an annotation processor for use with apt, you can’t use the reflection features in Java because you are working with source code, not compiled classes.

Using the Visitor pattern with apt

  • Processing annotations can become complex.
  • To prevent the complexity from scaling up badly when you have more annotations and more processors, the mirror API provides classes to support the Visitor design pattern.
  • A Visitor traverses a data structure or collection of objects, performing an operation on each one.
  • The data structure need not be ordered, and the operation that you perform on each object will be specific to its type.
  • This decouples the operations from the objects themselves, meaning that you can add new operations without adding methods to the class definitions.
  • When you use the apt tool with the Visitor pattern, you provide a Visitor class which has a method for handling each type of declaration that you visit.

Annotation-based unit testing

  • With annotations we can include the unit tests inside the class to be tested, and thus reduce the time and trouble of unit testing to a minimum.
  • This approach has the additional benefit of being able to test private methods as easily as public ones.
    

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • HP-Socket 是一套通用的高性能 TCP/UDP/HTTP 通信框架,包含服務端組件、客戶端組件和 Agent 組件,廣泛適用於各種不同應用場景的 TCP/UDP/HTTP 通信系統,提供 C/C++、C#、Delphi、E(易語言)、Java、Python 等編程語言介面。HP-Socket... ...
  • java學習這一部分其實也算是今天的重點,這一部分用來回答很多群里的朋友所問過的問題,那就是我你是如何學習Java的,能不能給點建議?今天我是打算來點乾貨,因此咱們就不說一些學習方法和技巧了,直接來談每個階段要學習的內容甚至是一些書籍。這一部分的內容,同樣適用於一些希望轉行到Java的同學。 在大家 ...
  • 題目大意: 花花山峰巒起伏,峰頂常年被雪,Memphis打算幫花花山風景區的人員開發一個滑雪項目。 我們可以把風景區看作一個n*n的地圖,每個點有它的初始高度,滑雪只能從高處往低處滑【嚴格大於】。但是由於地勢經常變動【比如雪崩、滑坡】,高度經常變化;同時,政府政策規定對於每個區域都要間歇地進行保護, ...
  • //應用於EasyUI框架 js://圖片上傳 loadPic:function(index){ $('#hrAddTabs').datagrid('selectRow',index); var row = $("#hrAddTabs").datagrid("getSelected"); var c ...
  • 你是不是在為想收集數據而不知道如何收集而著急? 你是不是在為想學習爬蟲而找不到一個專門為小白寫的教程而煩惱? Bingo! 你沒有看錯,這就是專門面向小白學習爬蟲而寫的!我會採用實例的方式,把每個部分都跟實際的例子結合起來幫助小伙伴兒們理解。最後再寫幾個實戰的例子。 我們使用Python來寫爬蟲,一 ...
  • FunDA的特點之一是以數據流方式提供逐行數據操作支持。這項功能解決了FRM如Slick數據操作以SQL批次模式為主所產生的問題。為了實現安全高效的數據行操作,我們必須把FRM產生的Query結果集轉變成一種強類型的結果集,也就是可以欄位名稱進行操作的數據行類型結果集。在前面的一篇討論中我們介紹了通 ...
  • 一個已有的Struts+Spring+Hibernate項目,以前使用MySQL資料庫,現在想把Redis也整合進去。 1. 相關Jar文件 下載並導入以下3個Jar文件: commons-pool2-2.4.2.jar、jedis-2.3.1.jar、spring-data-redis-1.3.4 ...
  • 總的來說,要使在thinkphp框架下麵HTML導入的圖片、css文件和js文件有效,只有兩種方法:(1)使用絕對路徑;(2)在項目目錄下創建新目錄Public,把所有的img文件夾、js文件夾和css文件夾放在裡面,同時把原有的HTML中的導入路徑前面統一加上“lxx_public/子目錄/”,比 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...