Categories
discuss

Best way to alter constants in Java build process

I have inherited a Java application (servlets) that runs under Tomcat. For historical reasons, the code has different “look and feel” options based on where the application will be deployed (essentially a matter of branding).

There are several constants that control this branding process, which have different functions and should not be compacted into a single constant (i.e. BRAND, MULTI-LANGUAGE, plus the locations of icons and css style sheets, etc.).

Currently the development team must manually change the constants (they are at least localized in one data class and well documented), then recompile the application using ANT.

What is the best way to automate this process assuming at least Ant 1.8 and Java 6.x?

I know that there have not been any good solutions using compiler arguments (like one could do in C or C++), and am leaning to some “best way” to edit either the source file containing the constants, or putting them into another file and swapping them out using the ant build process. I’d like to have a result that would work using something like “ant build brand-x” where changing the brand would change the resulting build.

Thanks,

-Richard

Answer

Use the replace task in Ant to change the values.

Categories
discuss

What is the use of package level protection in java?

I know how package level protection in java works. I read a lot of code (including lots of open source stuff) and no-one seem to be using it. The whole protection level seems slightly faulty to me (I’d have c#’s internal any day of the week).

Are there any legit real-world use-cases that are in common use ?

Edit: A little too late after asking this question I realized I’d forgotten to exclude the “standard” pattern of package protected implementation classes, possibly providing implementations of public interfaces. Everyone uses those, as has been noted several times in the replies. I still think there are a large number of nice replies to this question.

Answer

There are two good uses for package level visibility (in my experience):

1) Defining “internal” classes in a public API. Commonly you would define your interfaces and core factories as public and the “internal” implementations as package level. Then the public factories can construct the package level implementation classes and return them as instances of the public interfaces. This nicely allows users to only access the stuff they should.

The downside is that you have to have all this stuff in the same package, which almost never is a good idea for any reasonably-sized API. JSR 294/modules/Project Jigsaw in Java 7 will hopefully provide an alternative by specifying a new visibility modifier (module) that can be used to access classes within a module across packages without making them visible outside the module. You can find an example of how this would work in this article.

2) Unit testing is the other common use case. Frequently you’ll see a src tree and a test tree and stuff that would otherwise be private is instead package level so that unit tests in the same (parallel) package are able to access otherwise hidden methods to check or manipulate state.

Categories
discuss

What makes the Java compiler so fast?

I was wondering about what makes the primary Java compiler (javac by sun) so fast at compilation?

..as well as the C# .NET compiler from Microsoft.

I am comparing them with C++ compilers (such as G++), so maybe my question should have been, what makes C++ compilers so slow 🙂

Answer

That question was nicely answered in this one: Why does C++ compilation take so long? (as jalf pointed out in the comments section)

Basically it’s the missing modules concept of C++, and the aggressive optimization done by the compiler.

Categories
discuss

Why might one also use a blank constructor?

I was reading some Java recently and came across something (an idiom?) new to me: in the program, classes with multiple constructors would also always include a blank constructor. For example:

public class Genotype {
  private boolean bits[];
  private int rating;
  private int length;
  private Random random;

  public Genotype() {              //  <= THIS is the bandit, this one right here
    random = new Random();
  }

  /* creates a Random genetoype */
  public Genotype(int length, Random r) {
    random = r;
    this.length = length;
    bits = new boolean[length];

    for(int i=0;i<length;i++) {
        bits[i] =random.nextBoolean();
    }
  }

  /* copy constructor */
  public Genotype(Genotype g,Random r) {
    random = r;
    bits = new boolean[g.length];
    rating = g.rating;
    length = g.length;

    for(int i=0;i<length;i++) {
        bits[i] = g.bits[i];
    }

  }
}

The first constructor doesn’t seem to be a “real” constructor, it seems as though in every case one of the other constructors will be used. So why is that constructor defined at all?

Answer

I am not sure that the code you were reading was high quality (I’ve reviewed some bioinformatics code in the past and it is unfortunately often not written by professional developers). For example, that third constructor is not a copy constructor and generally there are problems in this code, so I wouldn’t “read too much into it”.

The first constructor is a default constructor. It only initializes the bare minimum and lets users set the rest with getters and setters. Other constructors are often “convenience constructors” that help create objects with less calls. However, this can often lead to inconsistencies between constructors. In fact, there is recent research that shows that a default constructor with subsequent calls to setters is preferable.

There are also certain cases where a default constructor is critical. For example, certain frameworks like digester (used to create objects directly from XML) use default constructors. JavaBeans in general use default constructors, etc.

Also, some classes inherit from other classes. you may see a default constructor when the initialization of the parent object is “good enough”.

In this specific case, if that constructor was not defined, one would have to know all the details in advance. That is not always preferable.

And finally, some IDEs automatically generate a default constructor, it is possible that whoever wrote the class was afraid to eliminate it.

Categories
discuss

Beginner Assistance – Where does this code belong?

I’m trying to develop a firefox extension that inserts additional HTTP header fields into outgoing HTTP requests (to interface with an apache extension i’m concurrently developing).

While I understand the individual components of an extension and understand the basic tutorials that are presented on the web, I’m finding it difficult going from the “Hello World” tutorial extensions, into developing a full blown extension.

The sample code I am wanting to adapt for my purposes is presented at the bottom of Setting HTTP request headers.

I am wondering, where in the extension hierarchy should this code be placed and how is such code called/constructed/activated, will it run automatically when the extension is initialised?

Thanks in advance.

Answer

For a basic extension, you would place your code in the chrome/content directory of the extension. You would hook this content into Firefox using an overlay. While overlays are usually xul content (buttons, etc) they can be anything. Including a script tag which would load fire off your Javascript code.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..