#
THE PRESIDENT: Fellow citizens: For eight years, it has been my honor to serve as your President. The first decade of this new century has been a period of consequence — a time set apart. Tonight, with a thankful heart, I have asked for a final opportunity to share some thoughts on the journey that we have traveled together, and the future of our nation.
Five days from now, the world will witness the vitality of American democracy. In a tradition dating back to our founding, the presidency will pass to a successor chosen by you, the American people. Standing on the steps of the Capitol will be a man whose history reflects the enduring promise of our land. This is a moment of hope and pride for our whole nation. And I join all Americans in offering best wishes to President-Elect Obama, his wife Michelle, and their two beautiful girls.
Tonight I am filled with gratitude — to Vice President Cheney and members of my administration; to Laura, who brought joy to this house and love to my life; to our wonderful daughters, Barbara and Jenna; to my parents, whose examples have provided strength for a lifetime. And above all, I thank the American people for the trust you have given me. I thank you for the prayers that have lifted my spirits. And I thank you for the countless acts of courage, generosity, and grace that I have witnessed these past eight years.
This evening, my thoughts return to the first night I addressed you from this house — September the 11th, 2001. That morning, terrorists took nearly 3,000 lives in the worst attack on America since Pearl Harbor. I remember standing in the rubble of the World Trade Center three days later, surrounded by rescuers who had been working around the clock. I remember talking to brave souls who charged through smoke-filled corridors at the Pentagon, and to husbands and wives whose loved ones became heroes aboard Flight 93. I remember Arlene Howard, who gave me her fallen son’s police shield as a reminder of all that was lost. And I still carry his badge.
As the years passed, most Americans were able to return to life much as it had been before 9/11. But I never did. Every morning, I received a briefing on the threats to our nation. I vowed to do everything in my power to keep us safe.
Over the past seven years, a new Department of Homeland Security has been created. The military, the intelligence community, and the FBI have been transformed. Our nation is equipped with new tools to monitor the terrorists’ movements, freeze their finances, and break up their plots. And with strong allies at our side, we have taken the fight to the terrorists and those who support them. Afghanistan has gone from a nation where the Taliban harbored al Qaeda and stoned women in the streets to a young democracy that is fighting terror and encouraging girls to go to school. Iraq has gone from a brutal dictatorship and a sworn enemy of America to an Arab democracy at the heart of the Middle East and a friend of the United States.
There is legitimate debate about many of these decisions. But there can be little debate about the results. America has gone more than seven years without another terrorist attack on our soil. This is a tribute to those who toil night and day to keep us safe — law enforcement officers, intelligence analysts, homeland security and diplomatic personnel, and the men and women of the United States Armed Forces.
Our nation is blessed to have citizens who volunteer to defend us in this time of danger. I have cherished meeting these selfless patriots and their families. And America owes you a debt of gratitude. And to all our men and women in uniform listening tonight: There has been no higher honor than serving as your Commander-in-Chief.
The battles waged by our troops are part of a broader struggle between two dramatically different systems. Under one, a small band of fanatics demands total obedience to an oppressive ideology, condemns women to subservience, and marks unbelievers for murder. The other system is based on the conviction that freedom is the universal gift of Almighty God, and that liberty and justice light the path to peace.
This is the belief that gave birth to our nation. And in the long run, advancing this belief is the only practical way to protect our citizens. When people live in freedom, they do not willingly choose leaders who pursue campaigns of terror. When people have hope in the future, they will not cede their lives to violence and extremism. So around the world, America is promoting human liberty, human rights, and human dignity. We’re standing with dissidents and young democracies, providing AIDS medicine to dying patients — to bring dying patients back to life, and sparing mothers and babies from malaria. And this great republic born alone in liberty is leading the world toward a new age when freedom belongs to all nations.
For eight years, we’ve also strived to expand opportunity and hope here at home. Across our country, students are rising to meet higher standards in public schools. A new Medicare prescription drug benefit is bringing peace of mind to seniors and the disabled. Every taxpayer pays lower income taxes. The addicted and suffering are finding new hope through faith-based programs. Vulnerable human life is better protected. Funding for our veterans has nearly doubled. America’s air and water and lands are measurably cleaner. And the federal bench includes wise new members like Justice Sam Alito and Chief Justice John Roberts.
When challenges to our prosperity emerged, we rose to meet them. Facing the prospect of a financial collapse, we took decisive measures to safeguard our economy. These are very tough times for hardworking families, but the toll would be far worse if we had not acted. All Americans are in this together. And together, with determination and hard work, we will restore our economy to the path of growth. We will show the world once again the resilience of America’s free enterprise system.
Like all who have held this office before me, I have experienced setbacks. There are things I would do differently if given the chance. Yet I’ve always acted with the best interests of our country in mind. I have followed my conscience and done what I thought was right. You may not agree with some of the tough decisions I have made. But I hope you can agree that I was willing to make the tough decisions.
The decades ahead will bring more hard choices for our country, and there are some guiding principles that should shape our course.
While our nation is safer than it was seven years ago, the gravest threat to our people remains another terrorist attack. Our enemies are patient, and determined to strike again. America did nothing to seek or deserve this conflict. But we have been given solemn responsibilities, and we must meet them. We must resist complacency. We must keep our resolve. And we must never let down our guard.
At the same time, we must continue to engage the world with confidence and clear purpose. In the face of threats from abroad, it can be tempting to seek comfort by turning inward. But we must reject isolationism and its companion, protectionism. Retreating behind our borders would only invite danger. In the 21st century, security and prosperity at home depend on the expansion of liberty abroad. If America does not lead the cause of freedom, that cause will not be led.
As we address these challenges — and others we cannot foresee tonight — America must maintain our moral clarity. I’ve often spoken to you about good and evil, and this has made some uncomfortable. But good and evil are present in this world, and between the two of them there can be no compromise. Murdering the innocent to advance an ideology is wrong every time, everywhere. Freeing people from oppression and despair is eternally right. This nation must continue to speak out for justice and truth. We must always be willing to act in their defense — and to advance the cause of peace.
President Thomas Jefferson once wrote, “I like the dreams of the future better than the history of the past.” As I leave the house he occupied two centuries ago, I share that optimism. America is a young country, full of vitality, constantly growing and renewing itself. And even in the toughest times, we lift our eyes to the broad horizon ahead.
I have confidence in the promise of America because I know the character of our people. This is a nation that inspires immigrants to risk everything for the dream of freedom. This is a nation where citizens show calm in times of danger, and compassion in the face of suffering. We see examples of America’s character all around us. And Laura and I have invited some of them to join us in the White House this evening.
We see America’s character in Dr. Tony Recasner, a principal who opened a new charter school from the ruins of Hurricane Katrina. We see it in Julio Medina, a former inmate who leads a faith-based program to help prisoners returning to society. We’ve seen it in Staff Sergeant Aubrey McDade, who charged into an ambush in Iraq and rescued three of his fellow Marines.
We see America’s character in Bill Krissoff — a surgeon from California. His son, Nathan — a Marine — gave his life in Iraq. When I met Dr. Krissoff and his family, he delivered some surprising news: He told me he wanted to join the Navy Medical Corps in honor of his son. This good man was 60 years old — 18 years above the age limit. But his petition for a waiver was granted, and for the past year he has trained in battlefield medicine. Lieutenant Commander Krissoff could not be here tonight, because he will soon deploy to Iraq, where he will help save America’s wounded warriors — and uphold the legacy of his fallen son.
In citizens like these, we see the best of our country - resilient and hopeful, caring and strong. These virtues give me an unshakable faith in America. We have faced danger and trial, and there’s more ahead. But with the courage of our people and confidence in our ideals, this great nation will never tire, never falter, and never fail.
It has been the privilege of a lifetime to serve as your President. There have been good days and tough days. But every day I have been inspired by the greatness of our country, and uplifted by the goodness of our people. I have been blessed to represent this nation we love. And I will always be honored to carry a title that means more to me than any other - citizen of the United States of America.
And so, my fellow Americans, for the final time: Good night. May God bless this house and our next President. And may God bless you and our wonderful country. Thank you. (Applause.)
2009.1.15 8:14 P.M. EST
今天在公司測試SourceView3.0發現程序,找出所有支持SNMP的設備以及它們的鏈路,
包括服務器到交換機的鏈路:
Discovery log:
2009-01-15 14:09:44 - INFO - ----[SourceView3.0]Starting ----
2009-01-15 14:09:45 - INFO - Parsing configuration file [struts-default.xml]
2009-01-15 14:09:45 - INFO - Parsing configuration file [struts-plugin.xml]
2009-01-15 14:09:45 - INFO - Parsing configuration file [struts.xml]
2009-01-15 14:09:45 - INFO - Overriding property struts.i18n.reload - old value: false new value: true
2009-01-15 14:10:45 - INFO - 新主機加入:
1.路由器:ip=192.168.0.254,
sysOid=1.3.6.1.4.1.9.1.110,
sysName=C3640,
community=cisco,
symbol=cisco
---------------------
2009-01-15 14:10:46 - INFO - 新子網加入:
Subnet.2.addr[192.168.0.0],mask[255.255.255.0]
2009-01-15 14:10:56 - INFO - 新主機加入:
3.路由交換機:ip=192.168.0.253,
sysOid=1.3.6.1.4.1.9.1.633,
sysName=3560,
community=cisco,
mac=00:19:55:d9:9c:80,
symbol=cisco
---------------------
2009-01-15 14:10:57 - INFO - 新鏈路加入:
4.C3640[Ethernet0/1]<-->3560[Fa0/23],[route link](路由器到三層交換機)
2009-01-15 14:10:57 - INFO - 新子網加入:
Subnet.5.addr[192.168.1.0],mask[255.255.255.0]
2009-01-15 14:10:57 - INFO - 新子網加入:
Subnet.6.addr[192.168.2.0],mask[255.255.255.128]
2009-01-15 14:10:57 - INFO - 新子網加入:
Subnet.7.addr[192.168.2.128],mask[255.255.255.240]
2009-01-15 14:10:57 - INFO - 新子網加入:
Subnet.8.addr[192.168.3.0],mask[255.255.255.0]
2009-01-15 14:10:57 - INFO - 新子網加入:
Subnet.9.addr[192.168.4.0],mask[255.255.255.0]
2009-01-15 14:10:57 - INFO - 新子網加入:
Subnet.10.addr[192.168.5.0],mask[255.255.255.0]
2009-01-15 14:10:57 - INFO - 新子網加入:
Subnet.11.addr[192.168.6.0],mask[255.255.255.0]
2009-01-15 14:12:08 - INFO - 新主機加入:
12.路由交換機:ip=192.168.3.252,
sysOid=1.3.6.1.4.1.2011.2.23.22,
sysName=Quidway,
community=cisco,
mac=00:e0:fc:0c:9d:1b,
symbol=huawei
---------------------
2009-01-15 14:12:58 - INFO - 新主機加入:
13.服務器:ip=192.168.2.133,
sysOid=1.3.6.1.4.1.2.3.1.2.1.1.3,
sysName=localhost,
community=public,
symbol=aix
---------------------
2009-01-15 14:15:18 - INFO - 新主機加入:
14.服務器:ip=192.168.2.18,
sysOid=1.3.6.1.4.1.311.1.1.3.1.2,
sysName=SOFTDEPT18,
community=public,
symbol=windows
---------------------
2009-01-15 14:15:58 - INFO - ----discovering Layer 2 using the common method----
2009-01-15 14:15:58 - INFO - 新鏈路加入:
15.3560[Fa0/14]<-->localhost[en0; Product: PCI Ethernet Adapter (23100020) Man],[arp link](服務器到三層交換機)
2009-01-15 14:15:58 - INFO - find link from switch to the router or server:
0.null,[arp link]
2009-01-15 14:15:58 - INFO - find link from switch to the router or server:
0.null,[arp link]
2009-01-15 14:15:58 - INFO - find link from switch to the router or server:
0.null,[arp link]
2009-01-15 14:15:58 - INFO - find link from switch to the router or server:
0.null,[arp link]
2009-01-15 14:15:58 - INFO - find link from switch to the router or server:
15.3560[Fa0/14]<-->localhost[en0; Product: PCI Ethernet Adapter (23100020) Man],[arp link]
2009-01-15 14:15:58 - INFO - find link from switch to the router or server:
0.null,[arp link]
2009-01-15 14:15:58 - INFO - 新鏈路加入:
16.3560[Fa0/18]<-->SOFTDEPT18[Attansic L2 Fast Ethernet 10/100 Base-T Controller],[arp link] (服務器到三層交換機)
2009-01-15 14:15:58 - INFO - find link from switch to the router or server:
16.3560[Fa0/18]<-->SOFTDEPT18[Attansic L2 Fast Ethernet 10/100 Base-T Controller],[arp link]
2009-01-15 14:15:58 - INFO - 新鏈路加入:
17.3560[Fa0/7]<-->Quidway[Vlan-interface7],[arp link] (交換機到三層交換機)
2009-01-15 14:15:58 - INFO - find link from switch to the router or server:
0.null,[arp link]
2009-01-15 14:15:58 - INFO - find link from switch to the router or server:
0.null,[arp link]
2009-01-15 14:15:59 - INFO - ========全部發現完成了========
完成用戶手冊,標志我們的軟件向產品化、標準化又邁進了一步。
希望2009年能把3.0做得更好。
背景:
用戶環境比較簡單,三臺路由器和一臺交換機。
問題總結:
1.長時間開著拓撲圖,內存只增不減;
2.要一直開著IE才能及時看到報警,3.0準備開發一個windows客戶端;
3.Tomcat使用了8080端口與Oracle使用的HTTP服務器端口沖突;
4.添加鏈路時,如果有一個接口是VLAN的,會有出錯。

寶寶三個月,終于會爬了。
背景:
用戶環境比較簡單,一臺路由器、3臺windows2003服務器和一個Tomcat。
問題總結:
1.報表的數據存貯結構設計不好,導致程序復雜,容易出bug,而出了bug也不容易調試;
2.在這種相對簡單的網絡環境中,拓撲再發現還是很有用的,在3.0要實現這個功能;
3.應該支持在輪詢時的發現,而不是要手動更改配置文件,這個在3.0中要改進;
4.報警配置經過上次的修改,現在已經比較好了,這是進步。
opennms的發現流程,基本看明白了:

基于fdb表的發現,是二層發現最關鍵的部分。opennms這部分寫得很好,給我很多啟發。
另,1) 對backBoneBirdgePort的判定:對已經確定有鏈路的port為backBoneBirdgePort
2) 對bridgeIdentifier的判定:bridgeMac和fdb表中status=4的為bridgeIdentifier
很可惜,沒看明白opennms關于stp發現這塊。我覺得沒這么復雜吧?
還好,我自己也能把基于STP的發現做出來。
opennms 對CDP表的探測流程:

opennms對鏈路的發現全部在
DiscoveryLink這個類里。由于opennms對節點的發現沒有確定節點
的設備類型,導致發現鏈路更加復雜。
opennms拓撲發現模塊中,對路由表的探測流程如下:
背景:
客戶的WebSphere經常出問題,部署網管主要用于WebSphere的監控。
問題總結:
1. 對中間件的監控做得還不夠細,可能我們以前的重點都放在對設備的監控。
2. 對中間件的監控可能對中間件性能造成影響,是否考慮改變監控方式?
2008.10.30 女兒滿月
http://www.oracle.com/technology/global/cn/pub/articles/hunter_meta.html
了解如何使用 J2SE 5.0 中提供的元數據批注
最新的 J2SE 5.0 版本(也以代號 "Tiger" 著稱)為 Java 語言引進了許多變化,這些變化旨在使 Java 編程更有表現力、更加開發人員友好和更安全。我在 2003 年 9 月的一篇題目為“Java 即將發生巨大變化”的文章中介紹了許多 Java 新特性。我沒有介紹的一個重大的變化 — 那時還沒有完整概述它 — 是 Java 的元數據工具。從本文開始,在一個新的分為四個部分的文章系列中,我將從一年前離開的地方繼續向您顯示如何充分利用 Java 的元數據。
在第一篇文章中,我將說明元數據的用途并演示如何使用在核心的 J2SE 資料庫中提供的元數據批注。
在第二篇文章中,我將顯示如何編寫您自己的批注(首先編寫類似 @Copyright 的簡單的批注,然后看看與核心語言中內置的那些批注類似的更高級的批注)。
在第三篇文章中,我將演示工具如何在構建時使用批注(創建新的源文件或支持文件)以及程序如何在運行時使用批注(以改變代碼的行為)。
在最后的第四篇文章中,我將介紹如何利用在 JSR-181 下開發的標準元數據批注的幫助使編輯和部署 Web 服務在未來變得更容易(Oracle 是 JSR-181 的專家組的成員,并且是在開發工具中增加對設計時元數據的支持的一個積極的擁護者。)
元數據
我承認當我第一次看到 JSR-175 的提案“用于 Java 編程語言的元數據工具”(在 2004 年 9 月發布;Oracle 也是該專家組的成員)時,我預測它將創建必須放在 JAR 的 META-INF 目錄下的另一個屬性文件,或者必須與 JAR 捆綁的另一個 XML 部署描述符。幸運的是,這不是元數據要做的。事實上,它正好相反。Java 的新的元數據工具提供了從 Java 代碼內部批注 Java 代碼的一種標準方式。它使您能夠在要說明的元素的旁邊放置描述性的元數據。
當討論元數據時,您將經常看到幾個類似的術語,因此下面提供了一個小術語表來幫助您了解它們的差異:
術語 |
定義 |
元數據 |
關于數據的數據。JSR-175 的目標是在 Java 語言中提供元數據工具。 |
批注 |
一種特殊的 Java 結構,用來修飾類、方法、字段、參數、變量、構造器或包。它是 JSR-175 選擇用來提供元數據的工具。 |
批注類型 |
具有特殊實施的各種命名批注 |
屬性 |
由批注指定的一個特殊的元數據項目。有時可以和批注交替使用 |
例如:富士蘋果有一個屬性:它是紅色的。假定有一個 FujiApple 類,您可以使用 @Color 批注類型的一個批注來指定它的顏色。通過這么做,您就提供了關于蘋果的元數據。
自 1.0 版以來在 Java 中一直存在對元數據的需求。Java 從來沒有提供記錄元數據的標準機制,因而我們編程人員找到了各種技巧和竅門使用任意的工具來添加元數據。您看到在 J2SE 1.4 和更低版本中使用元數據的一些地方有:
- transient 關鍵字
- Serializable 標記接口
- SingleThreadModel servlet 接口
- web.xml 部署描述符內部的元素
- META-INF/MANIFEST.MF 文件
- BeanInfo 接口
- @deprecated Javadoc 注釋
- 所有的 XDoclet Javadoc 標記。
當使用這些技巧時,您可能沒有想到您正在添加元數據,但事實上您的確在添加元數據。上述方法存在的問題是它們都是解決同一問題的不同方法,但通用性不好。每一種方法都至少有一個缺點在新的元數據工具中得到了解決。
對于這個列表中的一些方法,局限很明顯。使用關鍵字不能伸縮;您不能使用用戶自己定義的關鍵字。標記接口沒有提供除它們的存在性之外的任何信息(即,它們沒有帶參數),并且它們只能處理類,而不能處理字段或方法或包。
列表中的其他一些方法可能看起來合理。使用 XML 支持文件似乎是個好主意,而事實上在許多情況下仍是個好主意。但對于我們使用 XML 文件的許多用途,例如指示類的哪一個方法應當看作是 web 服務,在 Java 代碼內部將規則直接放在方法的旁邊將更加高效。利用元數據,您可以使 XML 描述符文件僅包含與部署相關的決策。
該列表中可能最高效的元數據的用法是 @deprecated Javadoc 注釋和在其鏡像中創建的 XDoclet 標記。這可能是 JSR-175 語法為什么看起來與 @deprecated 標記非常類似的原因(正如我們將在下一部分中看到的那樣)。
批注
批注可以很容易地附加到代碼結構上。您可以寫一個 "at" 符號 (@),然后是批注類型名稱,并將批注直接放在要批注的項目前面。下面是一個簡單的例子:
import javax.jws.WebService;
import javax.jws.WebMethod;
@WebService
public class HelloWorldService {
@WebMethod
public String helloWorld() {
return "Hello World!";
}
}
當部署在正確的環境中時,增加 @WebService 和 @WebMethod 批注將指示 web 服務環境將該類變為 web 服務。
您可以批注方法、類、字段、參數、變量、構造器甚至整個包(利用一個特殊的外部 package-info.java 文件)。批注可以在括號內帶任意數量的命名參數。下面是使用批注進行修飾以創建 web 服務的一個更高級的示例類。它包含了一個理論上的 JNDI 環境變量查找:
@WebService(
name = "PingService",
targetNamespace="http://acme.com/ping"
)
@SOAPBinding(
style=SOAPBinding.Style.RPC,
use=SOAPBinding.Use.LITERAL
)
public class Ping {
public @env double level = 500.0; // JNDI lookup
public @WebMethod(operationName = "Foo") {
void foo() { }
}
}
這個例子顯示了附加到類、變量和方法(在類上實際上有兩個方法)上的批注。@env 批注沒有任何參數,因此它不需要括號。其他批注有一個或更多的命名參數。
當創建新的批注類型時,您將限定允許哪些參數名以及它們的類型。批注接受的類型是嚴格限定的;它們只可以是基本類型、String、Class、枚舉類型、批注類型和前面這些類型的數組。傳遞的參數必須始終是非空的編譯時常量。
要了解本示例中顯示的批注有什么效果必須等到本系列的第四篇文章。讓我們開始看看 J2SE 5.0 提供的簡單的批注類型:@Override、@Deprecated 和 @SuppressWarnings。
內置的批注
當我們看這三種標準的用戶級批注時,必須考慮:在可以提供的所有可能的批注類型中,為什么 Tiger 恰恰提供三種?原因是提供大量的標準批注并不是目標所在。
JSR-175 的宗旨嚴格規定了它是要定義一個元數據工具。編寫自定義批注類型的任務留給了編程人員,而編寫一組標準的批注類型的任務留給了其他 JSR。例如,有一個新的名稱為“Java 平臺的通用批注”的 JSR-250,其宗旨是“為 J2SE 和 J2EE 平臺中的通用的語義概念開發適用于各種技術的批注”。JSR-250 計劃在 2005 年春天的某個時候在 javax.annotations 程序包中提供它的標準的批注集。還有之前提到的 JSR-181,它將使得在 J2EE 容器中編寫 Web 服務變得更容易(我們將在本系列中的第四篇文章中進行介紹)。事實上,大多數新的企業 JSR(從 Servlets 2.5 到 EJB 3.0 到 JDBC 4.0)都在考慮批注可以提供哪些優點。
@Override
第一個 J2SE 標準批注 @Override 使您能夠在代碼中增加新的可選的編譯器檢查。它在方法中存在表示該方法用于覆蓋父類中的方法。如果編譯器檢測到該方法實際上沒有覆蓋任何東西,那么將出現編譯錯誤。經常使用,@Override 可以幫助您避免當方法標記沒有完全匹配時 — 當多態變為(您可以稱之為)“單態” ("unimorphism") 時 — 將得到的細微的 bug。
例如,以下代碼可能看起來很合理:
public class OverrideExample {
@Override
public boolean equals(OverrideExample obj) {
return false;
}
}
然而,當您編譯 OverrideExample.java 時,您將得到一個錯誤,該錯誤指示一個細微的問題。
% javac OverrideExample.java
javac OverrideExample.java
OverrideExample.java:3: method does not override a method from its superclass
@Override
^
1 error
通過提示編譯器您希望進行覆蓋,使編譯器能夠捕獲到 equals() 方法帶 Object 類型參數的細微 bug。
@Override 批注在實際中有用嗎?只有當您是一個愿意用 @Override 來標記每一個覆蓋方法的非常嚴謹的編程人員時才有用。我們中有多少人能聲稱可以達到這種嚴謹程度?我認為我不能。可能 IDE 將找到一種方式來鼓勵或強制使用 @Override。
@Deprecated
第二種標準批注是 @Deprecated,它與 @deprecated Javadoc 標記有幾乎相同的行為。您可以用類似以下的方式來使用它:
public class DeprecatedExample {
@Deprecated
public static void badMethod() {
}
}
public class DeprecatedUser {
public static void main(String[] args) {
DeprecatedExample.badMethod();
}
}
The @Deprecated 批注看起來非常像 @deprecated 標記,除了它出現在注釋外面的方法或類聲明的前面,并且有一個大寫字母 "D"。如果您試圖編譯上面的代碼,javac 將產生警告:
% javac Deprecated*.java
Note:DeprecatedUser.java uses or overrides a deprecated API.
Note:Recompile with -Xlint:deprecation for details.
1 error
如果您遵循警告的建議并用 -Xlint:deprecation 進行編譯,那么您將得到關于警告的詳細信息:
% javac -Xlint:deprecation
DeprecatedUser.java:3: warning: [deprecation] badMethod() in DeprecatedExample
has been deprecated
DeprecatedExample.badMethod();
@Deprecated 批注比 @Override 更有用嗎?我不這么認為。該批注不支持任何參數,因此與 Javadoc 標記不同,您不能提供一個字符串來說明不贊成使用該方法并推薦一個替代的方法進行使用。@Deprecated 批注提供的價值實際上比 @deprecated 標記少。該批注唯一的優勢是您可以通過編程的方式在運行時檢測不贊成使用的項目。因此,傳統觀點認為應當同時使用 @deprecated 標記和 @Deprecated 標記,一個用于文檔,另一個用于運行時反射。
我覺得很不幸 JSR-175 沒有選擇對 @Deprecated 做更多的工作。至少該批注應當復制 @deprecated 標記的功能,包含一個字符串說明,從而編譯器可以將其與“不贊成使用” (Deprecation) 警告一起輸出。利用額外的參數,@Deprecated 還可以接收 "isError" 布爾類型參數,以指示是否完全不鼓勵使用該方法或者使用它將被認為是編譯錯誤(利用解釋錯誤原因的清楚的自定義說明來進行完善)。查看 C# 的示例 1 找到屬性 [Obsolete],該屬性正好實現了這一點,它被證明非常有用。
@SuppressWarnings
J2SE 提供的最后一個批注是 @SuppressWarnings。該批注的作用是給編譯器一條指令,告訴它對被批注的代碼元素內部的某些警告保持靜默。
一點背景:J2SE 5.0 為 Java 語言增加了幾個新的特性,并且和它們一起增加了許多新的警告并承諾在將來增加更多的警告。您可以為 "javac" 增加 -Xlint 參數來控制是否報告這些警告(如上面的 @Deprecated 部分所示)。
默認情況下,Sun 編譯器以簡單的兩行的形式輸出警告。通過添加 -Xlint:keyword 標記(例如 -Xlint:finally),您可以獲得關鍵字類型錯誤的完整說明。通過在關鍵字前面添加一個破折號,寫為 -Xlint:-keyword,您可以取消警告。(-Xlint 支持的關鍵字的完整列表可以在 javac 文檔頁面上找到。)下面是一個清單:
關鍵字 |
用途 |
deprecation |
使用了不贊成使用的類或方法時的警告 |
unchecked |
執行了未檢查的轉換時的警告,例如當使用集合時沒有用泛型 (Generics) 來指定集合保存的類型。 |
fallthrough |
當 Switch 程序塊直接通往下一種情況而沒有 Break 時的警告。 |
path |
在類路徑、源文件路徑等中有不存在的路徑時的警告。 |
serial |
當在可序列化的類上缺少 serialVersionUID 定義時的警告。 |
finally |
任何 finally 子句不能正常完成時的警告。 |
all |
關于以上所有情況的警告。 |
@SuppressWarnings 批注允許您選擇性地取消特定代碼段(即,類或方法)中的警告。其中的想法是當您看到警告時,您將調查它,如果您確定它不是問題,您就可以添加一個 @SuppressWarnings 批注,以使您不會再看到警告。雖然它聽起來似乎會屏蔽潛在的錯誤,但實際上它將提高代碼安全性,因為它將防止您對警告無動于衷 — 您看到的每一個警告都將值得注意。
下面是使用 @SuppressWarnings 來取消 deprecation 警告的一個例子:
public class DeprecatedExample2 {
@Deprecated
public static void foo() {
}
}
public class DeprecatedUser2 {
@SuppressWarnings(value={"deprecation"})
public static void main(String[] args) {
DeprecatedExample2.foo();
}
}
@SuppressWarnings 批注接收一個 "value" 變量,該變量是一個字符串數組,它指示將取消的警告。合法字符串的集合隨編譯器而變化,但在 JDK 上,可以傳遞給 -Xlint 的是相同的關鍵字集合(非常方便)。并且要求編譯器忽略任何它們不能識別的關鍵字,這在您使用一些不同的編譯器時非常方便。
因為 @SuppressWarnings 批注僅接收一個參數,并為該參數使用了特殊的名稱 "value",所以您可以選擇省略 value=,作為一種方便的縮寫:
public class DeprecatedUser2 {
@SuppressWarnings({"deprecation"})
public static void main(String[] args) {
DeprecatedExample2.foo();
}
}
您可以將單個數組參數中的任意數量的字符串值傳遞給批注,并在任何級別上放置批注。例如,以下示例代碼指示將取消整個類的 deprecation 警告,而僅在 main() 方法代碼內取消 unchecked 和 fallthrough 警告:
import java.util.*;
@SuppressWarnings({"deprecation"})
public class NonGenerics {
@SuppressWarnings({"unchecked","fallthrough"})
public static void main(String[] args) {
Runtime.runFinalizersOnExit();
List list = new ArrayList();
list.add("foo");
}
public static void foo() {
List list = new ArrayList();
list.add("foo");
}
}
@SuppressWarnings 是否比前兩個批注更有用?絕對是這樣。不過,在 JDK 1.5.0 版本中還沒有完全支持該批注,如果您用 1.5.0 來嘗試它,那么它將類似無操作指令。調用 -Xlint:-deprecation 也沒有任何效果。Sun 沒有聲明什么時候將增加支持,但它暗示這將在即將推出的一個 dot 版本中實現。