Created
November 15, 2012 19:05
-
-
Save quephird/4080525 to your computer and use it in GitHub Desktop.
Just demonstrating you can do Ruby-ish stuff in Java too.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.Field; | |
public class FunWithFieldsAndReflection { | |
public static void main (String[] args) { | |
SalesForce sf = new SalesForce(); | |
sf.setFun(false); | |
sf.setGreat(false); | |
sf.setWellArchitected(false); | |
sf.setWellDocumented(false); | |
sf.setRecommended(false); | |
try { | |
Field[] fs = sf.getClass().getDeclaredFields(); | |
for (Field f : fs) { | |
f.setAccessible(true); | |
f.set(sf, true); | |
System.out.println("<" + | |
f.getName() + | |
">" + | |
f.get(sf) + | |
"</" + | |
f.getName() + | |
">"); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
class SalesForce { | |
private boolean great; | |
private boolean fun; | |
private boolean wellArchitected; | |
private boolean wellDocumented; | |
private boolean recommended; | |
public boolean isGreat() { | |
return great; | |
} | |
public void setGreat(boolean great) { | |
this.great = great; | |
} | |
public boolean isFun() { | |
return fun; | |
} | |
public void setFun(boolean fun) { | |
this.fun = fun; | |
} | |
public boolean isWellArchitected() { | |
return wellArchitected; | |
} | |
public void setWellArchitected(boolean wellArchitected) { | |
this.wellArchitected = wellArchitected; | |
} | |
public boolean isWellDocumented() { | |
return wellDocumented; | |
} | |
public void setWellDocumented(boolean wellDocumented) { | |
this.wellDocumented = wellDocumented; | |
} | |
public boolean isRecommended() { | |
return recommended; | |
} | |
public void setRecommended(boolean recommended) { | |
this.recommended = recommended; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment