pt123
pt123

Reputation: 2166

Ant The best way of excluding sensitive class files

Ant Best way of excluding sensitive class files

After a few trials, I found that if the class is mentioned in the code javac will ignore the exclude list and still compile the class if it can find it rather than throwing an error.

I would prefer an error was thrown than it compiling with my sensitive class.

I also noticed that conditional compilation is ignored so if the code is like

static final boolean DEBUG = false;
//interface which TestSensitive & NormalClass implement
    ITestWrapper testWrapper = null; 

        if(DEBUG){
            testWrapper = new TestSensitive();
        }else{
            testWrapper = new NormalClass();
        }

testWrapper.print_Msg();

In build.xml in javac

 <src path="${source.absolute.dir}" />
        <exclude name="**/Test*.java" />
        <src path="${gen.absolute.dir}" />

TestSensitive is still being compiled even though the call to testWrapper = new TestSensitive() should be ignored by the conditional compile.

http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.21

http://www.javapractices.com/topic/TopicAction.do?Id=64

At the moment the only way I think can achieve what I want is to copy out the TestSensitive class and copy in a dummy hollow TestSensitive class. Is there a better way of doing this.

Upvotes: 0

Views: 147

Answers (1)

Peter Butkovic
Peter Butkovic

Reputation: 12139

Let me answer to the non-ant part of the question (anyway, till you solve it you can't progress any further):

Seems you're contradicting youself, your DEBUG is true, so why do you wonder about TestSensitive? It's the if branch which is chosen.

However I believe for references to other classes, you need to have these during compilation on your classpath. I believe you can't prevent that this way.

However there are ways to achieve the behaviour expected. For example:

  • via reflection - you would load class using it's name and instantiate/use it later, or
  • as you already have common interface ITestWrapper:
    • and use some IOC (inversion of control) concept using framework like Spring or
    • via some self registration of classes (like listeners)

Upvotes: 2

Related Questions