@SuppressWarnings的使用、作用、用法

Java编译过程中会出现很多警告,有很多是安全的,但是每次编译有很多警告影响我们对error的过滤和修改,我们可以在代码中加上

@SuppressWarnings(“XXXX”) 来解决

例如:@SuppressWarnings("deprecation")表示不显示使用了不赞成使用的类或方法时的警告;

(“xxx”)里面的参数有如下几种:

  • all to suppress all warnings
  • boxing to suppress warnings relative to boxing/unboxing operations
  • cast to suppress warnings relative to cast operations
  • dep-ann to suppress warnings relative to deprecated annotation
  • deprecation to suppress warnings relative to deprecation
  • fallthrough to suppress warnings relative to missing breaks in switch statements
  • finally to suppress warnings relative to finally block that don’t return
  • hiding to suppress warnings relative to locals that hide variable
  • incomplete-switch to suppress warnings relative to missing entries in a switch statement (enum case)
  • nls to suppress warnings relative to non-nls string literals
  • null to suppress warnings relative to null analysis
  • rawtypes to suppress warnings relative to un-specific types when using generics on class params
  • restriction to suppress warnings relative to usage of discouraged or forbidden references
  • serial to suppress warnings relative to missing serialVersionUID field for a serializable class
  • static-access to suppress warnings relative to incorrect static access
  • synthetic-access to suppress warnings relative to unoptimized access from inner classes
  • unchecked to suppress warnings relative to unchecked operations
  • unqualified-field-access to suppress warnings relative to field access unqualified
  • unused to suppress warnings relative to unused code
  • 例如:
@SuppressWarnings("serial")
@WebServlet(name="ajax",urlPatterns="/servlet/ajax")
public class ajax extends HttpServlet {
	   public void init() throws ServletException {}
	   protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		   doActionPage(request, response);
	   }
	   protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		   doActionPage(request, response);
	   }
	   public static Object execMethod(String classNameAndMethod, String  inputVal)
			   throws Exception
		{
		 int lastPoint = Util.NVLL(classNameAndMethod).lastIndexOf(".");
		 inputVal = Util.NVLL(inputVal);

		 if(lastPoint < 0)
		   return inputVal;
		 try {
		 String className = classNameAndMethod.substring(0, lastPoint);
		 String methodName = classNameAndMethod.substring(lastPoint+1);
		 int firstPa = methodName.indexOf("(");
		 if(firstPa >0)
		   methodName = methodName.substring(0, firstPa);
		  Class clazzxxx = Class.forName(className);
	 	  if(inputVal.length() < 1) {
				 java.lang.reflect.Method m = clazzxxx.getDeclaredMethod(methodName,  new Class[] {});
				 return m.invoke((Object) null, new Object[0]);
		  }
		  else {
			 java.lang.reflect.Method m = clazzxxx.getDeclaredMethod(methodName, new Class[] {String.class});
			 return m.invoke(null, inputVal);
		  }
		 } catch (Exception r) {
			 Log.log("ERROR: ajax:" + classNameAndMethod + ", input value="  + inputVal);
			 r.printStackTrace();
			 throw r;
		 }
...
}




发布了15 篇原创文章 · 获赞 63 · 访问量 9万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览