All pastes #403000 Raw Edit

Something

public java v1 · immutable
·published 2007-03-20 03:21 UTC
rendered paste body
import impsoft.nexus.bots.color.methods.ColorScript;import impsoft.nexus.gui.threads.GuiThread;import java.lang.reflect.Field;import org.eclipse.swt.SWT;import org.eclipse.swt.events.SelectionEvent;import org.eclipse.swt.events.SelectionListener;import org.eclipse.swt.layout.FillLayout;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Shell;import org.eclipse.swt.widgets.Table;import org.eclipse.swt.widgets.TableColumn;import org.eclipse.swt.widgets.TableItem;public class ReflectionExplorer extends ColorScript {	public boolean doStart(String cmd, String[] args)			throws InterruptedException {		return true;	}	public void script() throws InterruptedException {		GuiThread.theDisplay.asyncExec(new Runnable() {			public void run() {				makeGui(theBot.theApplet);			}		});	}	public void makeGui(Object o) {		if (o == null) {			return;		}		Shell shell = new Shell(GuiThread.theDisplay);		shell.setText("Explorer for " + o);		shell.setLayout(new FillLayout());		final Composite composite = new Composite(shell, SWT.NONE);		composite.setLayout(new FillLayout());		final Table table = new Table(composite, SWT.FULL_SELECTION				| SWT.BORDER);		table.setLinesVisible(true);		table.setHeaderVisible(true);		TableColumn newColumnTableColumn1 = new TableColumn(table, SWT.NONE);		newColumnTableColumn1.setWidth(100);		newColumnTableColumn1.setText("name");		TableColumn newColumnTableColumn2 = new TableColumn(table, SWT.NONE);		newColumnTableColumn2.setWidth(200);		newColumnTableColumn2.setText("type");		TableColumn newColumnTableColumn3 = new TableColumn(table, SWT.NONE);		newColumnTableColumn3.setWidth(300);		newColumnTableColumn3.setText("value");		final Field fields[] = o.getClass().getDeclaredFields();		final Object FieldObjects[] = new Object[fields.length];		for (int i = 0; i < fields.length; i++) {			fields[i].setAccessible(true);			FieldObjects[i] = fieldObject(fields[i], o);			fields[i].setAccessible(false);		}		for (Field field : fields) {			field.setAccessible(true);			String name = field.getName();			String type = field.getType().toString();			String value = fieldValue(field, o);			TableItem newItemTableItem = new TableItem(table, SWT.BORDER);			newItemTableItem.setText(0, name);			newItemTableItem.setText(1, type);			newItemTableItem.setText(2, value);			field.setAccessible(false);		}		table.addSelectionListener(new SelectionListener() {			public void widgetDefaultSelected(SelectionEvent arg0) {			}			public void widgetSelected(SelectionEvent e) {				try {					makeGui(FieldObjects[table.getSelectionIndex()]);				} catch (Exception k) {					exception(k);				}			}		});		shell.pack();		GuiThread.center(GuiThread.theShell.getBounds(), shell);		shell.open();	}	public Object fieldObject(Field theField, Object theObject) {		Class typeClass = theField.getType();		String fieldType = typeClass.getName();		if (fieldType.equalsIgnoreCase("boolean")) {		} else if (fieldType.equalsIgnoreCase("int")) {		} else if (fieldType.equalsIgnoreCase("double")) {		} else if (fieldType.equalsIgnoreCase("long")) {		} else if (fieldType.equalsIgnoreCase("byte")) {		} else if (fieldType.equalsIgnoreCase("char")) {		} else if (fieldType.equalsIgnoreCase("float")) {		} else if (fieldType.equalsIgnoreCase("short")) {		} else if (typeClass.isArray()) {			try {				// TODO			} catch (Exception e) {				exception(e);			}		} else {			try {				if (theField != null) {					Object x = theField.get(theObject);					return x;				}			} catch (Exception e) {				exception(e);			}		}		return null;	}	public String fieldValue(Field theField, Object theObject) {		try {			Class typeClass = theField.getType();			String fieldType = typeClass.getName();			if (fieldType.equalsIgnoreCase("boolean")) {				if (theField != null) {					boolean x = theField.getBoolean(theObject);					return "" + x;				}			} else if (fieldType.equalsIgnoreCase("int")) {				if (theField != null) {					int x = theField.getInt(theObject);					return " " + x;				}			} else if (fieldType.equalsIgnoreCase("double")) {				if (theField != null) {					double x = theField.getDouble(theObject);					return "" + x;				}			} else if (fieldType.equalsIgnoreCase("long")) {				if (theField != null) {					long x = theField.getLong(theObject);					return "" + x;				}			} else if(fieldType.equalsIgnoreCase("byte")) {				if (theField != null) {					byte x = theField.getByte(theObject);					return "" + x;				}			} else if (fieldType.equalsIgnoreCase("char")) {				if (theField != null) {					char x = theField.getChar(theObject);					return "" + x;				}			} else if (fieldType.equalsIgnoreCase("float")) {				if (theField != null) {					float x = theField.getFloat(theObject);					return "" + x;				}			} else if (fieldType.equalsIgnoreCase("short")) {				if (theField != null) {					short x = theField.getShort(theObject);					return "" + x;				}			} else if (typeClass.isArray()) {				Class componentClass = typeClass.getComponentType();				String componentName = componentClass.getName();				String printOut = "";				while (componentClass.isArray()) {					componentClass = componentClass.getComponentType();					printOut += "[]";				}				return componentName + printOut;			} else {				if (theField != null) {					Object x = theField.get(theObject);					if (x != null) {						return x.toString();					} else {						return "null";					}				}			}		} catch (Exception e) {			exception(e);		}		return "";	}}