Skip to content

Commit

Permalink
Apply generic definitions wherever applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyor Goldstein committed Jan 11, 2016
1 parent 3e67635 commit 8e87337
Show file tree
Hide file tree
Showing 59 changed files with 1,924 additions and 1,764 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Features
* [#567](https://github.com/java-native-access/jna/pull/567): Added `PrintWindow`, `IsWindowEnabled`, `IsWindow`, `FindWindowEx`, `GetAncestor`, `GetCursorPos`, `SetCursorPos`, `SetWinEventHook`, `UnhookWinEvent`, `CopyIcon`, and `GetClassLong` to `com.sun.jna.platform.win32.User32` and supporting constants to `com.sun.jna.platform.win32.WinUser` - [@mlfreeman2](https://github.com/mlfreeman2).
* [#573](https://github.com/java-native-access/jna/pull/573): Added `EnumProcessModules`, `GetModuleInformation`, and `GetProcessImageFileName` to `com.sun.jna.platform.win32.Psapi` and added `ExtractIconEx` to `com.sun.jna.platform.win32.Shell32` - [@mlfreeman2](https://github.com/mlfreeman2).
* [#574](https://github.com/java-native-access/jna/pull/574): Using static final un-modifiable List of field names for structure(s) [@lgoldstein](https://github.com/lgoldstein)
* [#577](https://github.com/java-native-access/jna/pull/577): : Apply generic definitions wherever applicable [@lgoldstein](https://github.com/lgoldstein)

Bug Fixes
---------
Expand Down
8 changes: 4 additions & 4 deletions src/com/sun/jna/CallbackParameterContext.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/* Copyright (c) 2007 Timothy Wall, All Rights Reserved
*
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* Lesser General Public License for more details.
*/
package com.sun.jna;

Expand All @@ -19,7 +19,7 @@ public class CallbackParameterContext extends FromNativeContext {
private Method method;
private Object[] args;
private int index;
CallbackParameterContext(Class javaType, Method m, Object[] args, int index) {
CallbackParameterContext(Class<?> javaType, Method m, Object[] args, int index) {
super(javaType);
this.method = m;
this.args = args;
Expand Down
8 changes: 4 additions & 4 deletions src/com/sun/jna/CallbackProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* Lesser General Public License for more details.
*/
package com.sun.jna;

Expand All @@ -19,12 +19,12 @@
*/
public interface CallbackProxy extends Callback {

/** This is the callback method invoked from native code.
/** This is the callback method invoked from native code.
* It must <em>not</em> throw any exceptions whatsoever.
*/
Object callback(Object[] args);
/** Returns the types of the parameters to the callback method. */
Class[] getParameterTypes();
Class<?>[] getParameterTypes();
/** Returns the type of the callback method's return value. */
Class getReturnType();
Class<?> getReturnType();
}
178 changes: 92 additions & 86 deletions src/com/sun/jna/CallbackReference.java

Large diffs are not rendered by default.

111 changes: 50 additions & 61 deletions src/com/sun/jna/DefaultTypeMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,87 +8,74 @@
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* Lesser General Public License for more details.
*/

package com.sun.jna;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Collection;
import java.util.List;

/** Provide custom mappings to and from native types. The default lookup
* checks classes corresponding to converters in the order added; if the
* class to be converted is an instance of the converter's registered class,
* the converter will be used.<p>
* Derived classes should install additional converters using
* the converter will be used.<p>
* Derived classes should install additional converters using
* {@link #addToNativeConverter}
* and/or {@link #addFromNativeConverter} in the default constructor. Classes
* for primitive types will automatically register for the corresponding
* Object type and vice versa (i.e. you don't have to register both
* Object type and vice versa (i.e. you don't have to register both
* <code>int.class</code> and <code>Integer.class</code>).
* If you want different mapping behavior than the default, simply override
* {@link #getToNativeConverter} and {@link #getFromNativeConverter}.
* @see Library#OPTION_TYPE_MAPPER
* @see Library#OPTION_TYPE_MAPPER
*/
public class DefaultTypeMapper implements TypeMapper {
private static class Entry {
public Class type;
public Class<?> type;
public Object converter;
public Entry(Class type, Object converter) {
public Entry(Class<?> type, Object converter) {
this.type = type;
this.converter = converter;
}
}
private List toNativeConverters = new ArrayList();
private List fromNativeConverters = new ArrayList();
private Class getAltClass(Class cls) {

private List<Entry> toNativeConverters = new ArrayList<Entry>();
private List<Entry> fromNativeConverters = new ArrayList<Entry>();

private Class<?> getAltClass(Class<?> cls) {
if (cls == Boolean.class) {
return boolean.class;
}
else if (cls == boolean.class) {
} else if (cls == boolean.class) {
return Boolean.class;
}
else if (cls == Byte.class) {
} else if (cls == Byte.class) {
return byte.class;
}
else if (cls == byte.class) {
} else if (cls == byte.class) {
return Byte.class;
}
else if (cls == Character.class) {
} else if (cls == Character.class) {
return char.class;
}
else if (cls == char.class) {
} else if (cls == char.class) {
return Character.class;
}
else if (cls == Short.class) {
} else if (cls == Short.class) {
return short.class;
}
else if (cls == short.class) {
} else if (cls == short.class) {
return Short.class;
}
else if (cls == Integer.class) {
} else if (cls == Integer.class) {
return int.class;
}
else if (cls == int.class) {
} else if (cls == int.class) {
return Integer.class;
}
else if (cls == Long.class) {
} else if (cls == Long.class) {
return long.class;
}
else if (cls == long.class) {
} else if (cls == long.class) {
return Long.class;
}
else if (cls == Float.class) {
} else if (cls == Float.class) {
return float.class;
}
else if (cls == float.class) {
} else if (cls == float.class) {
return Float.class;
}
else if (cls == Double.class) {
} else if (cls == Double.class) {
return double.class;
}
else if (cls == double.class) {
} else if (cls == double.class) {
return Double.class;
}
return null;
Expand All @@ -100,56 +87,58 @@ else if (cls == double.class) {
* @param converter {@link ToNativeConverter} to transform an object of
* the given Java class into its native-compatible form.
*/
public void addToNativeConverter(Class cls, ToNativeConverter converter) {
public void addToNativeConverter(Class<?> cls, ToNativeConverter converter) {
toNativeConverters.add(new Entry(cls, converter));
Class alt = getAltClass(cls);
Class<?> alt = getAltClass(cls);
if (alt != null) {
toNativeConverters.add(new Entry(alt, converter));
}
}
/** Add a {@link FromNativeConverter} to convert a native result type into the
/**
* Add a {@link FromNativeConverter} to convert a native result type into the
* given Java type. Converters are checked for in the order added.
*
* @param cls Java class for the Java representation of a native type.
* @param converter {@link FromNativeConverter} to transform a
* native-compatible type into its Java equivalent.
*/
public void addFromNativeConverter(Class cls, FromNativeConverter converter) {
public void addFromNativeConverter(Class<?> cls, FromNativeConverter converter) {
fromNativeConverters.add(new Entry(cls, converter));
Class alt = getAltClass(cls);
Class<?> alt = getAltClass(cls);
if (alt != null) {
fromNativeConverters.add(new Entry(alt, converter));
}
}
/** Add a {@link TypeConverter} to provide bidirectional mapping between
* a native and Java type.

/**
* Add a {@link TypeConverter} to provide bidirectional mapping between
* a native and Java type.
*
* @param cls Java class representation for a native type
* @param converter {@link TypeConverter} to translate between native and
* Java types.
*/
public void addTypeConverter(Class cls, TypeConverter converter) {
public void addTypeConverter(Class<?> cls, TypeConverter converter) {
addFromNativeConverter(cls, converter);
addToNativeConverter(cls, converter);
}

private Object lookupConverter(Class javaClass, List converters) {
for (Iterator i=converters.iterator();i.hasNext();) {
Entry entry = (Entry)i.next();

private Object lookupConverter(Class<?> javaClass, Collection<? extends Entry> converters) {
for (Entry entry : converters) {
if (entry.type.isAssignableFrom(javaClass)) {
return entry.converter;
}
}
return null;
}
/* (non-Javadoc)
* @see com.sun.jna.TypeMapper#getFromNativeConverter(java.lang.Class)
*/
public FromNativeConverter getFromNativeConverter(Class javaType) {

@Override
public FromNativeConverter getFromNativeConverter(Class<?> javaType) {
return (FromNativeConverter)lookupConverter(javaType, fromNativeConverters);
}
/* (non-Javadoc)
* @see com.sun.jna.TypeMapper#getToNativeConverter(java.lang.Class)
*/
public ToNativeConverter getToNativeConverter(Class javaType) {

@Override
public ToNativeConverter getToNativeConverter(Class<?> javaType) {
return (ToNativeConverter)lookupConverter(javaType, toNativeConverters);
}
}
12 changes: 6 additions & 6 deletions src/com/sun/jna/FromNativeContext.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
/* Copyright (c) 2007 Timothy Wall, All Rights Reserved
*
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* Lesser General Public License for more details.
*/
package com.sun.jna;

/** Provides context for converting a native value into a Java type. */
public class FromNativeContext {
private Class type;
FromNativeContext(Class javaType) {
private Class<?> type;
FromNativeContext(Class<?> javaType) {
this.type = javaType;
}
/** The desired Java type of the result. */
public Class getTargetType() {
public Class<?> getTargetType() {
return type;
}
}
6 changes: 3 additions & 3 deletions src/com/sun/jna/FromNativeConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* Lesser General Public License for more details.
*/

package com.sun.jna;

/** Define conversion from a native type to the appropriate Java type. */
public interface FromNativeConverter {
/** Convert the given native object into its Java representation using
* the given context.
* the given context.
*/
Object fromNative(Object nativeValue, FromNativeContext context);
/** Indicate the native type used by this converter. */
Class nativeType();
Class<?> nativeType();
}
Loading

0 comments on commit 8e87337

Please sign in to comment.