Many of a times, I have come across the code where if/else used instead of the switch case. Its not that annoying for readability but many of us actually forget the truth that switch case statements generate jump tables which are faster while executing compared to if/else. I agree that we get very minimal efficiency compared to other improvements but we get some improvement in many of the cases. If the case statements are more dense then the effect more. Java allows integer types (see java for more info on integer types. even char is integer type) and enums to be used in switch case. From now onwards we should keep in mind that use switch/case where ever possible, and avoid using default case if possible for more efficiency. This will beautify the code also.
This comment applies to other languages like C/C++ also.
Sunday, March 28, 2010
Finding the jar/jars from which the class/resource is loaded
There is always a very good chance of having the same class in two different jars available in the class path of the class loader. Its the fact that which ever jar comes first in the search is loaded and used. But if you wonder from which jar or path the class/resource (may be a properties file) is loaded from. Here you go. just write the following snippet of code in the context you want to know the resource location.
URL url = Thread.currentThread().getContextClassLoader().getResource("com/example/ClassToFind.class");
if(url != null){
System.out.println(" location of resource in context "+url.getPath());
} else {
url = super.getClass().getClassLoader().getResource(name);
if(url != null){
System.out.println(" location of resource class loder "+ super.getClass().getClassLoader() + " is "+ url.getPath());
}
}
If the ClassToFind.class is in example.jar then the above will print
location-of-example.jar!com/example/ClassToFind.class
If you want to find all the jars containg the resource then you can use this instead.
java.util.Enumeration urls = Thread.currentThread().getContextClassLoader()
.getResources("");
while(urls.hasMoreElements()){
System.out.println(urls.nextElement().getPath());
}
There are few other ways to get around this. If you have put Java in verbose mode or If you are running in JBOSS you can enable the class loader logs. see JBOSS docs for more information.
URL url = Thread.currentThread().getContextClassLoader().getResource("com/example/ClassToFind.class");
if(url != null){
System.out.println(" location of resource in context "+url.getPath());
} else {
url = super.getClass().getClassLoader().getResource(name);
if(url != null){
System.out.println(" location of resource class loder "+ super.getClass().getClassLoader() + " is "+ url.getPath());
}
}
If the ClassToFind.class is in example.jar then the above will print
location-of-example.jar!com/example/ClassToFind.class
If you want to find all the jars containg the resource then you can use this instead.
java.util.Enumeration
.getResources("");
while(urls.hasMoreElements()){
System.out.println(urls.nextElement().getPath());
}
There are few other ways to get around this. If you have put Java in verbose mode or If you are running in JBOSS you can enable the class loader logs. see JBOSS docs for more information.
Wednesday, March 24, 2010
Allowing only defined classes to access the method in Java
If you wonder how do you allow only a specific class to be allowed to access your method. Here you go. You can check the class name on the stack and do take a decision whether you allow are reject permission to.
Class callerClass = Reflection.getCallerClass(2); //2 stack position
If(!validList_.contains(callerClass)) {
Throw new IllegalAccessException(); or SecurityException();
}
In the above snippet validList is list of classes you wish to allow. This is barrowed from the sun.misc.Unsafe classe.
Class callerClass = Reflection.getCallerClass(2); //2 stack position
If(!validList_.contains(callerClass)) {
Throw new IllegalAccessException(); or SecurityException();
}
In the above snippet validList is list of classes you wish to allow. This is barrowed from the sun.misc.Unsafe classe.
Subscribe to:
Posts (Atom)