Wednesday, January 22, 2014

Git: Changing a remote URL


https://help.github.com/articles/changing-a-remote-s-url

Example:

$ git remote -v
origin  ssh://core@1.2.3.197/data/kgit/explorer (fetch)

Currently origin is pointing to  ssh://core@1.2.3.197/data/kgit/explorer


Want to change my remote url to ssh://kondal.kolipaka@gerrit.mycompany.net:29418/explorer.git

$ git remote set-url origin ssh://kondal.kolipaka@gerrit.mycompany.net:29418/explorer.git


Check after change:

$ git remote -v

origin  ssh://kondal.kolipaka@gerrit.mycompany.net:29418/explorer.git (push)

Thursday, January 16, 2014

Java Type Erasure: Cannot perform instanceof check against parameterized type

Can we use something like this ?

public Object[] getElements(Object inputElement) {
if (inputElement instanceof List<Parameter>) {
return ((List<Parameter>) inputElement).toArray();
}
return new Object[] {};
}

No!!!!

Compiler Error message:
Cannot perform instanceof check against parameterized type List<Parameter>. Use the form List<?> instead since further generic type information will be erased at runtime


It's telling us to use List<?> more generic type rather than specific List<Parameter>.

public Object[] getElements(Object inputElement) {
if (inputElement instanceof List<?>) {
return ((List<?>) inputElement).toArray();
}
return new Object[] {};
}


Why?

In simple terms, generic types will be erased at compile time. During the type erasure process, the Java compiler erases all type parameters and replaces each with its first bound if the type parameter is bounded, or Object if the type parameter is unbounded.

It means, the .class file or generated byte code will not have any generic type information.


Below is the code after applying type erasure by compiler.

public Object[] getElements(Object inputElement) {
if (inputElement instanceof List) {
return ((List) inputElement).toArray();
}
return new Object[] {};
}


Other example from SUN docs:

public class Node<T> {

    private T data;
    private Node<T> next;

    public Node(T data, Node<T> next) }
        this.data = data;
        this.next = next;
    }

    public T getData() { return data; }
    // ...
}

Because the type parameter T is unbounded, the Java compiler replaces it with Object:

public class Node {

    private Object data;
    private Node next;

    public Node(Object data, Node next) {
        this.data = data;
        this.next = next;
    }

    public Object getData() { return data; }
    // ...
}


In the following example, the generic Node class uses a bounded type parameter:

public class Node<T extends Comparable<T>> {

    private T data;
    private Node<T> next;

    public Node(T data, Node<T> next) {
        this.data = data;
        this.next = next;
    }

    public T getData() { return data; }
    // ...
}
The Java compiler replaces the bounded type parameter T with the first bound class, Comparable:

public class Node {

    private Comparable data;
    private Node next;

    public Node(Comparable data, Node next) {
        this.data = data;
        this.next = next;
    }

    public Comparable getData() { return data; }
    // ...
}


Resources:
http://docs.oracle.com/javase/tutorial/java/generics/erasure.html

Change Eclipse theme

I am working with eclipse 4.4(Luna), someway I don't like the eclipse new theme!! might be I was so used to the old classic theme.

Eclipse provides the option to change through Preferences.

Window->Preferences->General->Appearance

In Theme drown-down list you can find 'classic' theme, Select and click on 'Apply'. You are done!




Eclipse Error: An API baseline has not been set for the current workspace.

I was getting this error, when I imported eclipse JSDT source code which i have cloned into eclipse workspace.

To resolve this issue, modify the Eclipse workspace preferences.

Windows > Preferences > Plug-in Development > API Baselines > Options > Missing API baseline
Then, change "Error" to "Ignore" in the drop down list.






Monday, January 13, 2014

Annotations for Ignoring properties In JSON - Jackson

For my reference....

Sometimes POJOs contain properties that you do not want to write out, so you can do:

public class Value {
  public int value;
  @JsonIgnore public int internalValue;
}
and get JSON like:
{ "value" : 42 }

or, you may get properties in JSON that you just want to skip: if so, you can use:

@JsonIgnoreProperties({ "extra", "uselessValue" })
public class Value {
  public int value;
}
which would be able to handle JSON like:
{ "value" : 42, "extra" : "fluffy", "uselessValue" : -13 }

Finally, you may even want to just ignore any "extra" properties from JSON (ones for which there is no counterpart in POJO). This can be done by adding:

@JsonIgnoreProperties(ignoreUnknown=true)
public class PojoWithAny {
  public int value;
}

Reference:
http://jackson.codehaus.org/

Thursday, January 9, 2014

java.lang.OutOfMemoryError: PermGen space with eclipse

By default, Eclipse comes with below PermGen configuration in eclipse.ini file.
--launcher.XXMaxPermSize
256m

But this is not working with some versions. As per eclipse bug, this is not working with Java 1.6_21, but I was facing this issue even in Java 1.6_32 as well.

Anyway,add below parameter in eclipse.ini as a vm argument to resolve the issue.
-XX:MaxPermSize=256M

Even after this, if your eclipse is crashing with PermGen space issue, then try to increase the size.
I would say 512M is a very big size for PermGen space, If some application is taking more than that means, it's a real application issue, developers need to analyze where it's taking so much of space.

Resources:
http://wiki.eclipse.org/FAQ_How_do_I_increase_the_permgen_size_available_to_Eclipse%3F
https://bugs.eclipse.org/bugs/show_bug.cgi?id=319514 

Wednesday, January 8, 2014

Getting Eclipse Workspace file system path

ResourcePlugin is a starting point for workspace initialization. This will hold the all required information about the workspace.

IWorkspace workspace = ResourcesPlugin.getWorkspace();

//Local file system
IPath file = workspace.getRoot().getLocation();

Output path:
D:\Work\KKWorkspaces\runtime-New_configuration


//Remote location 
URI locationURI = workspace.getRoot().getLocationURI();

OutputPath:
file:/D:/Work/KKWorkspaces/runtime-New_configuration