Rozwój developera – TouK dla KarieraPlus

Po swoim wystąpieniu na Confiturze, które dotyczyło testowania i, jak to zwykle prezentacje Jakuba, przyciągnęło masę słuchaczy, Jakub Nabrdalik z TouK opowiedział portalowi

KarieraPlus o tym, co rozwija go jako developera. http://karieraplus.pl/artykuly/najpopularniejszy-na-confiturze Wcześniej na łamach tego samego magazynu Bartek Zdanowski podpowiadał młodym programistom, gdzie szukać inspiracji, by powiększać swoje zawodowe kompetencje. http://karieraplus.pl/artykuly/gra-o-przyszlosc Obie rozmowy przeprowadzone były w kontekście Confitury, ale można znaleźć w nich uniwersalne wskazówki: co robić i gdzie bywać, by nie ustawać w rozwoju. Bartek wspomina m.in. o warsztatach, które pozwalają nam w TouK dzielić się wiedzą i pomagają zdobywać nowe umiejętności.

You May Also Like

Mock Retrofit using Dagger and Mockito

Retrofit is one of the most popular REST client for Android, if you never use it, it is high time to start. There are a lot of articles and tutorial talking about Retrofit. I just would like to show how to mock a REST server during develop of app and i...Retrofit is one of the most popular REST client for Android, if you never use it, it is high time to start. There are a lot of articles and tutorial talking about Retrofit. I just would like to show how to mock a REST server during develop of app and i...

Private fields and methods are not private in groovy

I used to code in Java before I met groovy. Like most of you, groovy attracted me with many enhancements. This was to my surprise to discover that method visibility in groovy is handled different than Java!

Consider this example:

class Person {
private String name
public String surname

private Person() {}

private String signature() { "${name?.substring(0, 1)}. $surname" }

public String toString() { "I am $name $surname" }
}

How is this class interpreted with Java?

  1. Person has private constructor that cannot be accessed
  2. Field "name" is private and cannot be accessed
  3. Method signature() is private and cannot be accessed

Let's see how groovy interpretes Person:

public static void main(String[] args) {
def person = new Person() // constructor is private - compilation error in Java
println(person.toString())

person.@name = 'Mike' // access name field directly - compilation error in Java
println(person.toString())

person.name = 'John' // there is a setter generated by groovy
println(person.toString())

person.@surname = 'Foo' // access surname field directly
println(person.toString())

person.surname = 'Bar' // access auto-generated setter
println(person.toString())

println(person.signature()) // call private method - compilation error in Java
}

I was really astonished by its output:

I am null null
I am Mike null
I am John null
I am John Foo
I am John Bar
J. Bar

As you can see, groovy does not follow visibility directives at all! It treats them as non-existing. Code compiles and executes fine. It's contrary to Java. In Java this code has several errors, pointed out in comments.

I've searched a bit on this topic and it seems that this behaviour is known since version 1.1 and there is a bug report on that: http://jira.codehaus.org/browse/GROOVY-1875. It is not resolved even with groovy 2 release. As Tim Yates mentioned in this Stackoverflow question: "It's not clear if it is a bug or by design". Groovy treats visibility keywords as a hint for a programmer.

I need to keep that lesson in mind next time I want to make some field or method private!