GC.GCB
Reuse objects; 'getClipBounds()' should not be called too often
Description
This rule flags code that calls `getClipBounds()' too often.
The `getClipBounds()' method always returns a new rectangle. Allocating more memory every time this method is called overworks the garbage collector.
Note: Violations are only reported for methods with more than one `getClipBounds()'.
Example
package GC;
import java.awt.Graphics;
public class GCB {
public void paint(Graphics g) {
int firstColLine = g.getClipBounds().x;
int lastColLine = g.getClipBounds().x + g.getClip-
Bounds().width;
}
}
Repair
package GC;
import java.awt.Graphics;
import java.awt.Rectangle;
public class GCB {
public void paint(Graphics g) {
Rectangle rec = g.getClipBounds();// instantiate a object,
rec
int firstColLine = rec.x; // reuse "rec"
int lastColLine = rec.x + rec.width; // reuse "rec"
}
}
|