Skip to content

Instantly share code, notes, and snippets.

@quephird
Created November 15, 2012 19:05
Show Gist options
  • Save quephird/4080525 to your computer and use it in GitHub Desktop.
Save quephird/4080525 to your computer and use it in GitHub Desktop.
Just demonstrating you can do Ruby-ish stuff in Java too.
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