33

CHow can I correctly provide the following functionally from C# in Java?

[C#]

#define PRODUCTION //Change from sandbox to production to switch between both systems.

#if SANDBOX
    using NetSuite.com.netsuite.sandbox.webservices;
#endif

#if PRODUCTION
    using NetSuite.com.netsuite.webservices;
#endif
Paul Renton
  • 2,652
  • 6
  • 25
  • 38
Robert H
  • 11,520
  • 18
  • 68
  • 110
  • 2
    Looks like I can't add an answer since the question's been closed, *but* I've read about a tool called [Prebop](http://boldinventions.com/index.php?option=com_content&view=article&id=81:javapreprocessorusingeclipse) (Disclaimer: I've not yet used it), that might do this and it apparently has support for Eclipse. (I agree with others though: in your situation, use DI. -- Just putting this here for other Googlers.) – BrainSlugs83 Jul 01 '15 at 17:37
  • see this answer, https://stackoverflow.com/a/48330157/3066295 – TT-- Sep 28 '18 at 02:40

5 Answers5

26

Java doesn't have a preprocessor - so the simple answer is that you can't.

This sort of thing is normally handled in Java using Dependency Injection - which is both more powerful and more flexible.

http://www.vogella.com/articles/DependencyInjection/article.html

Tim B
  • 40,716
  • 16
  • 83
  • 128
  • 7
    It should be noted that, in the OP's given example, he should also be using DI/IoC in C# anyway -- it's basically a bad example -- I think the question is still valid though, and this answer, while valid for the OP's scenario is kind of disappointing -- (not your fault @TimB, though the answer below about using cpp is kind of interesting...) – BrainSlugs83 Jul 01 '15 at 17:27
  • 2
    I strongly disagree that DI is "more flexible and more powerful." A jpp analogue to cpp similarly would allow for textual substitutions which can aid dramatically in the readability of repeated expressions where you simply don't want a method call, among many things. Not including it in the language started as a silly argument of potential abuse and remains so. Java has many _valid_ arguments regarding abuse (e.g., disallowing MI, not including operator overloading, etc.), but this was one place were I believe they botched it. Thankfully, C# didn't follow in those particular footsteps. – alife May 11 '22 at 18:10
  • I came from C, I used to use the pre-processor all the time. I've not found anything in decades of using Java where I've needed the pre-processor. There's always a cleaner way. Particularly with things like Cucumber available for when you need a less code-focussed approach – Tim B Mar 08 '23 at 14:00
10

Java doesn't have a preprocessor, yet that doesn't mean that you can't run Java code through cpp - though it would not be supported by any tools, AFAIK.

Ingo
  • 36,037
  • 5
  • 53
  • 100
  • 2
    Looks like I can't add an answer since the question's been closed, *but* I've read about a tool called [Prebop](http://boldinventions.com/index.php?option=com_content&view=article&id=81:javapreprocessorusingeclipse) (Disclaimer: I've not yet used it), that might do this and it apparently has support for Eclipse. (I agree with others though: in the OP's situation, he should use DI. -- Just putting this here for other Googlers.) – BrainSlugs83 Jul 01 '15 at 17:40
1

Use Dependency Injection/Inversion of Control. Depending on your actual needs, you might be able to get away with something as simple as property file/environment variables to control things.

You might be able to use static defines around some types of initialization/code.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • 1
    Using the example in the post, how would DI/IoC help? From what I've read (albeit briefly) it appears that DI is mostly used for decoupling and not conditional compilation. My specific use case surrounds changing import statements en mass in order to connect to a sandbox vs production system. Is there a specific tutorial/set of docs I should read? – Robert H Dec 12 '13 at 15:21
  • DI/IoC is "conditional compilation" at run time. Without knowing what you're actually trying to swap in and out, I'm guessing, but you'd use one of two webservice implementations, e.g., in development, they'd all be stubbed out to return known values. This is often done for testing purposes, for example. It's more likely we'd use different connection parameters/endpoints in development, which is generally just config values, which wouldn't require DI. – Dave Newton Dec 12 '13 at 15:28
  • A solution would be to use the same namespace for both versions, putting each version in its own JAR archive and just loading the appropriate one. – MauganRa Oct 14 '16 at 12:41
1

You can use something based on <#FreeMarker>.

enter image description here

source: https://github.com/mkowsiak/jpp

However, this solution will require pre-compilation step, if you want to change the code. On the other hand, you can still create code that works without pre-processing steps - sort of "default" compilation.

Oo.oO
  • 12,464
  • 3
  • 23
  • 45
0

I'm using java-comment-preprocessor. It is very easy and convenient and provides also integration for Maven, Ant and Gradle. It is using Java comments and a preprocessor is used generating the actual code based on the preprocessor flags, e.g.:

//#if simulator
private final static int FOO = 2;
//#else
private final static int FOO = 1;
//#endif

Maven integration:

<plugin>
                <groupId>com.igormaznitsa</groupId>
                <artifactId>jcp</artifactId>
                <version>7.0.5</version>
                <executions>
                    <execution>
                        <id>preprocessSources</id>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>preprocess</goal>
                        </goals>
                        <configuration>
                            <vars>
                                <simulator>${simulator}</simulator>
                            </vars>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
k_o_
  • 5,143
  • 1
  • 34
  • 43