Skip to content

Commit

Permalink
toString(): Use reflection to fetch value of ByReference object
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasblaesing committed May 13, 2020
1 parent 1079272 commit 4b24046
Show file tree
Hide file tree
Showing 11 changed files with 16 additions and 152 deletions.
10 changes: 0 additions & 10 deletions contrib/platform/src/com/sun/jna/platform/unix/X11.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,6 @@ public Atom getValue() {
NativeLong value = getPointer().getNativeLong(0);
return (Atom)new Atom().fromNative(value, null);
}

@Override
public String toString() {
return super.toString(getValue());
}
}
class Colormap extends XID {
private static final long serialVersionUID = 1L;
Expand Down Expand Up @@ -256,11 +251,6 @@ public Window getValue() {
NativeLong value = getPointer().getNativeLong(0);
return value.longValue() == X11.None ? Window.None : new Window(value.longValue());
}

@Override
public String toString() {
return super.toString(getValue());
}
}
class Pixmap extends Drawable {
private static final long serialVersionUID = 1L;
Expand Down
5 changes: 0 additions & 5 deletions contrib/platform/src/com/sun/jna/platform/win32/BaseTSD.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,6 @@ public ULONG_PTR getValue() {
? getPointer().getInt(0)
: getPointer().getLong(0));
}

@Override
public String toString() {
return super.toString(getValue());
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,6 @@ public void setValue(MC_DISPLAY_TECHNOLOGY_TYPE value) {
public MC_DISPLAY_TECHNOLOGY_TYPE getValue() {
return EnumUtils.fromInteger(getPointer().getInt(0), MC_DISPLAY_TECHNOLOGY_TYPE.class);
}

@Override
public String toString() {
return super.toString(getValue());
}
}
}

Expand Down Expand Up @@ -445,11 +440,6 @@ public void setValue(MC_COLOR_TEMPERATURE value) {
public MC_COLOR_TEMPERATURE getValue() {
return EnumUtils.fromInteger(getPointer().getInt(0), MC_COLOR_TEMPERATURE.class);
}

@Override
public String toString() {
return super.toString(getValue());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,6 @@ public void setValue(MC_VCP_CODE_TYPE value) {
public MC_VCP_CODE_TYPE getValue() {
return EnumUtils.fromInteger(getPointer().getInt(0), MC_VCP_CODE_TYPE.class);
}

@Override
public String toString() {
return super.toString(getValue());
}
}
}
}
Expand Down
20 changes: 0 additions & 20 deletions contrib/platform/src/com/sun/jna/platform/win32/OaIdl.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,6 @@ public void setValue(VARIANT_BOOL value) {
public VARIANT_BOOL getValue() {
return new VARIANT_BOOL(getPointer().getShort(0));
}

@Override
public String toString() {
return super.toString(getValue());
}
}

public static class _VARIANT_BOOLByReference extends ByReference {
Expand All @@ -224,11 +219,6 @@ public void setValue(VARIANT_BOOL value) {
public VARIANT_BOOL getValue() {
return new VARIANT_BOOL(getPointer().getShort(0));
}

@Override
public String toString() {
return super.toString(getValue());
}
}

@FieldOrder({"date"})
Expand Down Expand Up @@ -321,11 +311,6 @@ public void setValue(DISPID value) {
public DISPID getValue() {
return new DISPID(getPointer().getInt(0));
}

@Override
public String toString() {
return super.toString(getValue());
}
}

public static class MEMBERID extends DISPID {
Expand Down Expand Up @@ -357,11 +342,6 @@ public void setValue(MEMBERID value) {
public MEMBERID getValue() {
return new MEMBERID(getPointer().getInt(0));
}

@Override
public String toString() {
return super.toString(getValue());
}
}

// The Collect property. You use this property if the method you are calling
Expand Down
10 changes: 0 additions & 10 deletions contrib/platform/src/com/sun/jna/platform/win32/WTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,6 @@ public BSTR getValue() {
public String getString() {
return this.getValue().getValue();
}

@Override
public String toString() {
return super.toString(getValue());
}
}

public static class LPSTR extends PointerType {
Expand Down Expand Up @@ -323,10 +318,5 @@ public void setValue(VARTYPE value) {
public VARTYPE getValue() {
return new VARTYPE(getPointer().getShort(0));
}

@Override
public String toString() {
return super.toString(getValue());
}
}
}
55 changes: 0 additions & 55 deletions contrib/platform/src/com/sun/jna/platform/win32/WinDef.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,6 @@ public void setValue(WORD value) {
public WORD getValue() {
return new WORD(getPointer().getShort(0));
}

@Override
public String toString() {
return super.toString(getValue());
}
}

/**
Expand Down Expand Up @@ -212,11 +207,6 @@ public void setValue(DWORD value) {
public DWORD getValue() {
return new DWORD(getPointer().getInt(0));
}

@Override
public String toString() {
return super.toString(getValue());
}
}

/**
Expand Down Expand Up @@ -288,11 +278,6 @@ public void setValue(LONG value) {
public LONG getValue() {
return new LONG(getPointer().getInt(0));
}

@Override
public String toString() {
return super.toString(getValue());
}
}

/**
Expand Down Expand Up @@ -364,11 +349,6 @@ public void setValue(LONGLONG value) {
public LONGLONG getValue() {
return new LONGLONG(getPointer().getLong(0));
}

@Override
public String toString() {
return super.toString(getValue());
}
}

/**
Expand Down Expand Up @@ -926,11 +906,6 @@ public void setValue(ULONG value) {
public ULONG getValue() {
return new ULONG(getPointer().getInt(0));
}

@Override
public String toString() {
return super.toString(getValue());
}
}

/**
Expand Down Expand Up @@ -1002,11 +977,6 @@ public void setValue(ULONGLONG value) {
public ULONGLONG getValue() {
return new ULONGLONG(getPointer().getLong(0));
}

@Override
public String toString() {
return super.toString(getValue());
}
}

/**
Expand Down Expand Up @@ -1284,11 +1254,6 @@ public void setValue(USHORT value) {
public USHORT getValue() {
return new USHORT(getPointer().getShort(0));
}

@Override
public String toString() {
return super.toString(getValue());
}
}

/**
Expand Down Expand Up @@ -1392,11 +1357,6 @@ public void setValue(UINT value) {
public UINT getValue() {
return new UINT(getPointer().getInt(0));
}

@Override
public String toString() {
return super.toString(getValue());
}
}

/**
Expand Down Expand Up @@ -1461,11 +1421,6 @@ public void setValue(SCODE value) {
public SCODE getValue() {
return new SCODE(getPointer().getInt(0));
}

@Override
public String toString() {
return super.toString(getValue());
}
}

/**
Expand Down Expand Up @@ -1637,11 +1592,6 @@ public void setValue(BOOL value) {
public BOOL getValue() {
return new BOOL(getPointer().getInt(0));
}

@Override
public String toString() {
return super.toString(getValue());
}
}

/**
Expand Down Expand Up @@ -1778,11 +1728,6 @@ public void setValue(CHAR value) {
public CHAR getValue() {
return new CHAR(getPointer().getByte(0));
}

@Override
public String toString() {
return super.toString(getValue());
}
}

/**
Expand Down
15 changes: 0 additions & 15 deletions contrib/platform/src/com/sun/jna/platform/win32/WinNT.java
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,6 @@ public PSID getValue() {
return new PSID(p);
}
}

@Override
public String toString() {
return super.toString(getValue());
}
}

/**
Expand Down Expand Up @@ -1367,11 +1362,6 @@ public HANDLE getValue() {
h.setPointer(p);
return h;
}

@Override
public String toString() {
return super.toString(getValue());
}
}

/**
Expand Down Expand Up @@ -2572,11 +2562,6 @@ public ACL getValue() {
return new ACL(p);
}
}

@Override
public String toString() {
return super.toString(getValue());
}
}

@FieldOrder({"Revision", "Sbz1", "Control", "Owner", "Group", "Sacl", "Dacl"})
Expand Down
5 changes: 0 additions & 5 deletions contrib/platform/src/com/sun/jna/platform/win32/WinReg.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ public HKEY getValue() {
h.setPointer(p);
return h;
}

@Override
public String toString() {
return super.toString(getValue());
}
}

HKEY HKEY_CLASSES_ROOT = new HKEY(0x80000000);
Expand Down
29 changes: 16 additions & 13 deletions src/com/sun/jna/ptr/ByReference.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.sun.jna.Memory;
import com.sun.jna.Pointer;
import com.sun.jna.PointerType;
import java.lang.reflect.Method;

/** Provides generic "pointer to type" functionality, often used in C
* code to return values to the caller in addition to a function result.
Expand All @@ -43,19 +44,21 @@ protected ByReference(int dataSize) {
setPointer(new Memory(dataSize));
}

/**
* Express this reference and the value it points to.
*
* @param value
* The value to display. Callers should pass the result of
* {@code getValue()}.
* @return A formatted string containing the object type, reference address, and
* value
*/
protected String toString(Object value) {
if (value == null) {
return String.format("null@0x%x", Pointer.nativeValue(getPointer()));
@Override
public String toString() {
try {
Method getValue = getClass().getMethod("getValue");
Object value = getValue.invoke(this);
if (value == null) {
return String.format("null@0x%x", Pointer.nativeValue(getPointer()));
}
return String.format("%s@0x%x=%s", value.getClass().getSimpleName(), Pointer.nativeValue(getPointer()), value);
} catch (Exception ex) {
return String.format(
"ByReference Contract violated - %s#getValue raised exception: %s",
getClass().getName(),
ex.getMessage()
);
}
return String.format("%s@0x%x=%s", value.getClass().getSimpleName(), Pointer.nativeValue(getPointer()), value);
}
}
4 changes: 0 additions & 4 deletions src/com/sun/jna/ptr/PointerByReference.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,4 @@ public Pointer getValue() {
return getPointer().getPointer(0);
}

@Override
public String toString() {
return super.toString(getValue());
}
}

0 comments on commit 4b24046

Please sign in to comment.