Java

StackTrace


Throwable t = ...;
StringWriter sw = new StringWriter();
t.printStackTrace(new PrintWriter(sw));
String stacktrace = sw.toString();

Time


System.currentTimeMillis();

Shutdown


Runtime.getRuntime().addShutdownHook(
  new Thread() {
    public void run() {
      endApp();
      }
    }
  );

Exception


try {
  // ...
  }
catch (Exception e) {
  System.err.println("ERROR(()):\n" +
  "          " + e.getMessage() + "\n");
  e.printStackTrace(System.err);
  // ...
  }

Design by Contract

Precondition-public


// PreconditionException is_a RuntimeException
public void abc(int par) {
  if (par < 0) throw new PreconditionException("Illegal par: " + par);
    // ...
    }
  }

Precondition-private


private void abc(int par) {
  assert (par >=0);

Class Invariant


private boolean check() {
  // ...
  }

public void abc() {
  assert (check());
  // ...
  }

Internal Invariant


if ( a> 0) {
  // ...
  }
else {
  assert (a<=0)
  // ...
  }

Control-Flow Invariant


switch (xxx) {
  // ...
  default:
    assert (false);
  }

Conditional Compilation


static final boolean keepThis = false;
if (keepThis) {
  // compiler will eliminate this
  }

Checking for Enabled Assertions


static {
  boolean assertEnabled = false;
  assert   assertEnabled = true;
  if (! assertEnabled) {
    throw new RuntimeException("assertion must be enabled !");
    }
  }

Splash


JWindow window = new JWindow();
window.getContentPane().setLayout(new BorderLayout());
ImageIcon icon = new ImageIcon("logo.gif");
window.getContentPane().add(BorderLayout.CENTER, new JLabel(icon));
JLabel text = new JLabel("Welcome", JLabel.CENTER);
JPanel p = new JPanel();
p.setLayout(new GridLayout(2, 1));
p.add(text);
window.getContentPane().add(BorderLayout.SOUTH, p);
window.pack();
window.setVisible(true);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
window.setLocation(((int)(screenSize.width)  - window.getWidth()) / 2,
((int)(screenSize.height) - window.getHeight()) / 2);

Properties


public void writeProperties() {
  FileOutputStream fos = new FileOutputStream("test.prop");
  BufferedOutputStream bos =  new BufferedOutputStream(fos);
  Properties p = new Properties();
  p.setProperty("key1", "value1");
  p.setProperty("key2", "value2");
  p.store(bos, "Testing");
  bos.close();
  }

public void readProperties() {
  FileInputStream fis = new FileInputStream("test.prop");
  BufferedInputStream bis = new BufferedInputStream(fis);
  Properties p = new Properties();
  p.load(bis);
  bis.close();
  System.out.println("key1=" + p.getProperty("key1"));
  System.out.println("key2=" + p.getProperty("key2"));
  }

Exec


Process p = Runtime.getRuntime().exec("command");
InputStream is = p.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String s;
while ((s = br.readLine()) != null) {
  System.out.println(s);
  }
br.close();

MS Look&Feel


import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
class MyOwnWindowsLookAndFeel extends WindowsLookAndFeel {
  public boolean isSupportedLookAndFeel() {return true;}
  }

Assertion


assert   : catch (AssertionError e) {
  // ...
  }

javac -source 1.4
java -ea
java -ea:

JMP Profile

java -XrunjmpHotSpot Monitor
java -XX:+PrintCompilation

Immutable Class

  • no mutators
  • no method can be overwridden
  • all fields final
  • all fields private
  • exclusive access to any mutable components (deliver only a copy)

Traps

  • Static methods can\u2019t be overridden, they are hidden.
  • hashCode(…) should be overridden together with equals(…).
  • finaly {…} is always processed (even after return in try {…}).

JConsole


java -Dcom.sun.management.jmxremote
java -Dcom.sun.management.jmxremote.port=<port>
        -Dcom.sun.management.jmxremote.ssl=false
        -Dcom.sun.management.jmxremote.authenticate=false
jconsole

HProf


java -Xrunhprof:help
java -agentlib:hprof=cpu=samples,interval=20,depth=3

OpenGL for 2D


java -Dsun.java2d.opengl=true

Method templates


<T extends Bla>
T Y.func(Class<T> t);
X x = Y.class.func(X.class);

Leave a Reply

You must be logged in to post a comment.