Iterate over Static Fields and Values from a Java Class

Writing by on Monday, 5 of October , 2009 at 4:14 pm

Here’s a method that shows how to get all the static fields in a Java class and return them.

	public static List printFields(Class c) throws ClassNotFoundException, IllegalAccessException {
    		List fields = new ArrayList();
        java.lang.reflect.Field[] f = c.getDeclaredFields();
        for (java.lang.reflect.Field field : f) {
            int modifier = field.getModifiers();
            if (java.lang.reflect.Modifier.isStatic(modifier)){
          		String val = (String) field.get( null );
          		System.out.print(field.getName() +  ": " + val.toString() + ", ");
          		fields.add(val);
            }
        }
        return fields;
    }

Leave a comment

Category: Java

Generate Timestamps in MySQL

Writing by on Thursday, 1 of October , 2009 at 5:20 pm

So you want to populate a new column in MySQL with fake dates?

Here’s how to add a new datetime column in SQL

alter table city_new add column mytime datetime;

Here’s how to populate it with fake timestamps data

update city_new set mytime = select from_unixtime(
unix_timestamp(‘2009-01-01 01:00:00’)+floor(rand()*31536000)
);

Here’s a more generic formula

select from_unixtime(
unix_timestamp( ‘start timestamp’)
+floor(rand()* (max interval in seconds) )
);

Leave a comment

Category: Tips and Tricks

Eclipse Error: Could not open the editor: Resource is out of sync with the file system

Writing by on Friday, 10 of July , 2009 at 11:37 am

One of the several eclipse errors.

Could not open the editor: Resource is out of sync with the file system

Easy fix (that worked for me) is to select the Project and hit F5 or Right Click and click Refresh.
Simply refreshing the particular resource did not work.

Usually happens when some files are edited outside of eclipse.

Comments (1)

Category: Tips and Tricks

Ear Worm

Writing by on Thursday, 21 of May , 2009 at 6:05 pm

Titan Watches have been doing a version of the Wolfgang Amadeus Mozart – Symphony No. 25 in G minor and I really like this one (some claim this is by A R Rahman). This one has been stuck in my head.

Leave a comment

Category: YouTube

Seek bar suddenly disappears in VLC Media Player

Writing by on Wednesday, 4 of March , 2009 at 11:14 am

Can’t find out how to turn on the seek bar in VLC Media Player in the Full Screen Mode?

Close VLC Media Player and then Delete the vlc folder in your %appdata% (Delete C:\Documents and Settings\username\Application Data\vlc)

Optionally you might also try to Reset Preferences. Tools menu > Preferences > Reset Preferences > Save (Did not work for me.)

Comments (4)

Category: Tips and Tricks

Find out what version a Java Class is compiled with

Writing by on Tuesday, 17 of February , 2009 at 6:12 pm

I got an UnsatisfiedLinkError and wanted to find out whether our JDK upgrade had caused it and did some searching and found that javap -verbose lets you find out the version of a compiled class. So get the class (could be in a JAR file) and then follow this example where I find the version of MyCallback.class

C:>c:\jdk1.6.0_07\bin\javap -verbose MyCallback
Compiled from “MyCallback.java”
public class com.shivdev.MyCallback extends java.lang.Object implements com.shivdev.IFilter
SourceFile: “MyCallback.java”
minor version: 0
major version: 49

Then compare with the following:

major  minor  JDK
45       3    1.0
45       3    1.1
46       0    1.2
47       0    1.3
48       0    1.4
49       0    1.5
50       0    1.6

Alternatively, you can use this java class to find out the version:

public class FindVersion {

    private static int convert(byte upper, byte lower) {
        int ret =  upper << 8;
        ret |= lower;
        return ret;
    }

    public static void main(String[] args) throws Exception {
    		final String CLASS = "MyCallback.class";
        ClassLoader loader = FindVersion.class.getClassLoader();
        java.io.InputStream in = loader.getResourceAsStream(CLASS);
        try {
            byte[] buffer = new byte[8];
            in.read(buffer);
            int minor = convert(buffer[4], buffer[5]);
            int major = convert(buffer[6], buffer[7]);
            System.out.println(major + "." + minor);
        } finally {
            in.close();
        }
    }
}

Leave a comment

Category: Java

Shivdev Kalambi's Blog

Shivdev Kalambi is a Software Development Manager, previously a Principal Software Engineer at ArcSight/HP. With over 16 years' experience in software development, he's worked on several technologies and played different roles and contributed to all phases of projects. Non-tech activies include Ping-pong, Rock Climbing and Yoga at PG, Golf, Skiing, Swimming & a beer enthusiast.