代码之家  ›  专栏  ›  技术社区  ›  Jorge Ferreira

用Java产生条件编译时错误

  •  0
  • Jorge Ferreira  · 技术社区  · 16 年前

    我不是指编译错误,因为我犯了语法错误或其他什么错误。在C++中,我们可以根据以下示例创建编译时错误:

    template<int> struct CompileTimeError;
    template<> struct CompileTimeError<true> {};
    
    #define STATIC_CHECK(expr, msg) { CompileTimeError<((expr) != 0)> ERROR_##msg; (void)ERROR_##msg; }
    
    int main(int argc, char* argv[])
    {
        STATIC_CHECK(false, Compile_Time_Failure);
        return 0;
    }
    

    与2005年相比,这将产生:

    ------ Build started: Project: Test, Configuration: Debug Win32 ------
    Compiling...
    Test.cpp
    f:\temp\test\test\test.cpp(17) : error C2079: 'ERROR_Compile_Time_Failure' uses undefined struct 'CompileTimeError<__formal>'
            with
            [
                __formal=0
            ]
    Build log was saved at "file://f:\temp\Test\Test\Debug\BuildLog.htm"
    Test - 1 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    

    在Java中有什么方法可以实现这一点吗?

    4 回复  |  直到 16 年前
        1
  •  2
  •   Matt Quail    16 年前

    apt

    @MyStaticCheck(false, "Compile Time Error, kind-of")
    public static void main(String[] args) {
        return;
    }
    

        2
  •  2
  •   Daniel Spiewak    16 年前
        3
  •  0
  •   Confusion    16 年前
        4
  •  0
  •   Stan    13 年前

    public static final boolean

    public static final int functionSet1 = 0;

    ConditionalBuild.functionSet1 = 1;