zoukankan      html  css  js  c++  java
  • Java Conditional Compile Java条件编译

    The conditional compilation practice is used to optionally remove chunks of code from the compiled version of a class. It uses the fact that compilers will ignore any unreachable branches of code. 

    To implement conditional compilation, 

    * define a static final boolean value as a non-private member of some class 
    * place code which is to be conditionally compiled in an if block which evaluates the boolean 
    * set the value of the boolean to false to cause the compiler to ignore the branch; otherwise, keep its value as true 

    This practice is used mostly for implementing debugging statements, logging statements, and for assertions. With the advent of assert and the Logging API in JDK 1.4, the utility of conditional compilation is probably reduced. 

    Example 
    public final class Debug {
      
    //set to false to allow compiler to identify and eliminate
      
    //unreachable code
      public static final boolean ON = true;




    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;

    /**
    * Uses conditional compilation to optionally print debugging statements.
    */public final class GenericServlet extends HttpServlet {

      
    public void init(ServletConfig aConfig) throws ServletException {
        
    super.init(aConfig);

        
    //print out a message, but only if Debug.ON evaluates to true
        
    //if Debug.ON evaluates to false, this entire block of code will be
        
    //passed over by the compiler, and will not be present in output .class file
        if (Debug.ON) {
          System.out.println(
    "**** INIT OF My Controller *****");
        }
      }

      
    public void doGet(HttpServletRequest aRequest, HttpServletResponse aResponse)
                                                  
    throws ServletException, IOException {
        
    if (Debug.ON) {
          System.out.println(
    "Processing doGet");
        }
        doHttpRequest(aRequest, aResponse);
      }

      
    public void doPost(HttpServletRequest aRequest, HttpServletResponse aResponse)
                                                  
    throws ServletException, IOException {
        
    if (Debug.ON) {
          System.out.println(
    "Processing doPost");
        }
        doHttpRequest(aRequest, aResponse);
      }

      
    /// PRIVATE /////

      
    private void doHttpRequest(HttpServletRequest aRequest, HttpServletResponse aResponse)
                                                  
    throws ServletException, IOException {
         
    //empty
      }

    }
  • 相关阅读:
    CSS3 3D的总结(初学者易懂)
    BAT面试算法精品课直通BAT面试算法精品课购买优惠码-牛客网
    深度学习UFLDL老教程笔记1 稀疏自编码器Ⅱ
    深度学习UFLDL老教程笔记1 稀疏自编码器Ⅰ
    算法分析之函数渐近分析
    调用WebServices报错,请求“System.Web.AspNetHostingPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”类型的权限已失败。
    很久之前写的 ,突然想放上去
    ASP.NET Core – Web API 冷知识
    ASP.NET Core C# 反射 & 表达式树 (第三篇)
    ASP.NET Core C# 反射 & 表达式树 (第二篇)
  • 原文地址:https://www.cnblogs.com/super119/p/1924544.html
Copyright © 2011-2022 走看看