Skip to content

Commit 72e971e

Browse files
committed
Remove unnecessary Class.isInstance usage
Instead replace it with `instanceof` checks. This also removes impossible instance checks for primitives because an Object can never be an instance of a primitive.
1 parent ea9bf42 commit 72e971e

File tree

6 files changed

+111
-118
lines changed

6 files changed

+111
-118
lines changed

src/main/java/ca/weblite/objc/Client.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,12 @@ public Object send(Peerable proxy, String selector, Object... args){
279279
public Pointer sendPointer(Pointer receiver, Pointer selector, Object... args){
280280
//System.out.println("In sendPointer for "+selName(selector));
281281
Object res = send(receiver, selector, args);
282-
if ( Pointer.class.isInstance(res)){
282+
if (res instanceof Pointer) {
283283
return (Pointer)res;
284-
} else if ( Proxy.class.isInstance(res)){
284+
} else if (res instanceof Proxy) {
285285
return ((Proxy)res).getPeer();
286-
} else if ( long.class.isInstance(res) || Long.class.isInstance(res)){
287-
return new Pointer((Long)res);
286+
} else if (res instanceof Long) {
287+
return new Pointer((Long) res);
288288
} else {
289289
return (Pointer)res;
290290
}
@@ -459,39 +459,39 @@ public Message[] buildMessageChain(Object... parameters){
459459

460460

461461
for (int i=0; i<parameters.length; i++){
462-
462+
Object parameter = parameters[i];
463463
Message buffer = new Message();
464464
buffer.coerceInput = this.coerceInputs;
465465
buffer.coerceOutput = this.coerceOutputs;
466-
if ( String.class.isInstance(parameters[i])){
467-
if ( "_".equals(parameters[i])){
466+
if (parameter instanceof String) {
467+
if ("_".equals(parameter)) {
468468
buffer.receiver = Pointer.NULL;
469469
} else {
470-
buffer.receiver = cls((String)parameters[i]);
470+
buffer.receiver = cls((String)parameter);
471471
}
472-
} else if ( Peerable.class.isInstance(parameters[i])){
473-
buffer.receiver = ((Peerable)parameters[i]).getPeer();
472+
} else if (parameter instanceof Peerable) {
473+
buffer.receiver = ((Peerable)parameter).getPeer();
474474
} else {
475-
buffer.receiver = (Pointer)parameters[i];
475+
buffer.receiver = (Pointer)parameter;
476476
}
477477
i++;
478-
if ( String.class.isInstance(parameters[i])){
479-
buffer.selector = sel((String)parameters[i]);
480-
} else if ( Peerable.class.isInstance(parameters[i])){
481-
buffer.selector = ((Peerable)parameters[i]).getPeer();
478+
if (parameter instanceof String) {
479+
buffer.selector = sel((String)parameter);
480+
} else if (parameter instanceof Peerable) {
481+
buffer.selector = ((Peerable)parameter).getPeer();
482482

483483
} else {
484-
buffer.selector = (Pointer)parameters[i];
484+
buffer.selector = (Pointer)parameter;
485485
}
486486
i++;
487-
while ( i<parameters.length && parameters[i] != null ){
487+
while ( i<parameters.length && parameter != null ){
488488
buffer.args.add(parameters[i++]);
489489

490490
}
491491
messages.add(buffer);
492492
}
493493

494-
return messages.toArray(new Message[messages.size()]);
494+
return messages.toArray(Message[]::new);
495495
}
496496

497497

src/main/java/ca/weblite/objc/Proxy.java

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class Proxy implements Peerable {
3636
* that we don't create different Proxy objects for the same Objective-C
3737
* native pointer.
3838
*/
39-
private static Map<Pointer,Proxy> proxyCache = new HashMap<Pointer,Proxy>();
39+
private static final Map<Pointer,Proxy> proxyCache = new HashMap<>();
4040

4141
/**
4242
* The client that is used to make requests to the Objective-C runtime.
@@ -62,7 +62,7 @@ public class Proxy implements Peerable {
6262
*/
6363
public static Object retain(Object obj){
6464
synchronized(proxyCache){
65-
if ( Proxy.class.isInstance(obj) ){
65+
if (obj instanceof Proxy) {
6666
Proxy pobj = (Proxy)obj;
6767
pobj.retainCount++;
6868
}
@@ -82,7 +82,7 @@ public static Object retain(Object obj){
8282
*/
8383
public static Object release(Object obj){
8484
synchronized (proxyCache){
85-
if ( Proxy.class.isInstance(obj) ){
85+
if (obj instanceof Proxy) {
8686
Proxy pobj = (Proxy)obj;
8787
pobj.retainCount--;
8888
if ( pobj.retainCount <= 0 ){
@@ -268,16 +268,17 @@ public String sendString(String selector, Object... args){
268268
*/
269269
public int sendInt(Pointer selector, Object... args){
270270
Object res = send(selector, args);
271-
if ( boolean.class.isInstance(res) || Boolean.class.isInstance(res)){
272-
return ((Boolean)res)?1:0;
273-
} else if ( byte.class.isInstance(res) || Byte.class.isInstance(res)) {
274-
return new Byte((Byte)res).intValue();
275-
} else if ( int.class.isInstance(res) || Integer.class.isInstance(res)){
276-
return (Integer)res;
277-
} else if ( long.class.isInstance(res) || Long.class.isInstance(res)){
278-
return new Long((Long)res).intValue();
271+
if (res instanceof Boolean) {
272+
return ((Boolean)res) ? 1 : 0;
273+
} else if (res instanceof Byte) {
274+
return (Byte) res;
275+
} else if (res instanceof Integer){
276+
return (Integer) res;
277+
} else if (res instanceof Long){
278+
return ((Long) res).intValue();
279279
} else {
280-
return (Integer)res;
280+
// TODO Use custom exception class
281+
throw new RuntimeException("Result not convertible to int: " + res);
281282
}
282283
}
283284

@@ -323,19 +324,20 @@ public double sendDouble(String selector, Object... args){
323324
*/
324325
public boolean sendBoolean(Pointer selector, Object... args){
325326
Object res = send(selector, args);
326-
if ( boolean.class.isInstance(res) || Boolean.class.isInstance(res)){
327-
return (Boolean)res;
328-
} else if ( byte.class.isInstance(res) || Byte.class.isInstance(res)) {
329-
byte bres = (Byte)res;
330-
return bres > 0 ? true:false;
331-
} else if ( int.class.isInstance(res) || Integer.class.isInstance(res)){
332-
int ires = (Integer)res;
333-
return ires > 0 ? true:false;
334-
} else if ( long.class.isInstance(res) || Long.class.isInstance(res)){
335-
long lres = (Long)res;
336-
return lres > 0L ? true:false;
327+
if (res instanceof Boolean) {
328+
return (Boolean) res;
329+
} else if (res instanceof Byte) {
330+
byte bres = (Byte) res;
331+
return bres > 0;
332+
} else if (res instanceof Integer){
333+
int ires = (Integer) res;
334+
return ires > 0;
335+
} else if (res instanceof Long) {
336+
long lres = (Long) res;
337+
return lres > 0L;
337338
} else {
338-
return (Boolean)res;
339+
// TODO Use custom exception class
340+
throw new RuntimeException("Result not convertible to boolean: " + res);
339341
}
340342
}
341343
/**
@@ -522,15 +524,16 @@ public String toString(){
522524
*/
523525
@Override
524526
public boolean equals(Object o){
525-
if ( !Peerable.class.isInstance(o) ){
527+
if (o == this) {
528+
return true;
529+
} else if (!(o instanceof Peerable)) {
526530
return false;
527531
}
528532
Peerable p = (Peerable)o;
529533
return (getPeer() == p.getPeer());
530534

531535
}
532536

533-
/** {@inheritDoc} */
534537
@Override
535538
public int hashCode(){
536539
return getPeer().hashCode();
@@ -608,6 +611,4 @@ public Pointer getPointer(String key){
608611
return sendPointer("valueForKey:", key);
609612
}
610613

611-
612-
613614
}

src/main/java/ca/weblite/objc/RuntimeUtils.java

Lines changed: 55 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -796,95 +796,88 @@ public static ByReference getAsReferenceWrapper(Object val, String signature){
796796
switch ( signature.charAt(0)){
797797
case 'i':
798798
case 'I':
799-
if ( !int.class.isInstance(val) ){
800-
if ( Number.class.isInstance(val) ){
801-
val = ((Number)val).intValue();
802-
} else if ( String.class.isInstance(val)){
803-
val = Integer.valueOf((String) val);
804-
} else {
805-
throw new RuntimeException("Attempt to pass ineligible value to int: "+val);
806-
}
799+
int intVal;
800+
if (val instanceof Number) {
801+
intVal = ((Number) val).intValue();
802+
} else if (val instanceof String) {
803+
intVal = Integer.parseInt((String) val);
804+
} else {
805+
throw new RuntimeException("Attempt to pass ineligible value to int: "+val);
807806
}
808-
return new IntByReference((Integer)val);
807+
return new IntByReference(intVal);
809808
case 's':
810809
case 'S':
811-
if ( !short.class.isInstance(val) ){
812-
if ( Number.class.isInstance(val) ){
813-
val = ((Number)val).shortValue();
814-
} else if ( String.class.isInstance(val)){
815-
val = Short.valueOf((String) val);
816-
} else {
817-
throw new RuntimeException("Attempt to pass ineligible value to short: "+val);
818-
}
810+
short shortVal;
811+
if (val instanceof Number) {
812+
shortVal = ((Number) val).shortValue();
813+
} else if (val instanceof String) {
814+
shortVal = Short.parseShort((String) val);
815+
} else {
816+
throw new RuntimeException("Attempt to pass ineligible value to short: "+val);
819817
}
820-
return new ShortByReference((Short)val);
818+
return new ShortByReference(shortVal);
821819

822820
case 'l':
823821
case 'L':
824822
case 'q':
825823
case 'Q':
826-
if ( !long.class.isInstance(val) ){
827-
if ( Number.class.isInstance(val) ){
828-
val = ((Number)val).longValue();
829-
} else if ( String.class.isInstance(val)){
830-
val = Long.valueOf((String) val);
831-
} else {
832-
throw new RuntimeException("Attempt to pass ineligible value to long: "+val);
833-
}
824+
long longVal;
825+
if (val instanceof Number) {
826+
longVal = ((Number) val).longValue();
827+
} else if (val instanceof String) {
828+
longVal = Long.parseLong((String) val);
829+
} else {
830+
throw new RuntimeException("Attempt to pass ineligible value to long: "+val);
834831
}
835-
return new LongByReference((Long)val);
832+
return new LongByReference(longVal);
836833

837834
case 'f':
838-
if ( !float.class.isInstance(val) ){
839-
if ( Number.class.isInstance(val) ){
840-
val = ((Number)val).floatValue();
841-
} else if ( String.class.isInstance(val)){
842-
val = Float.valueOf((String) val);
843-
} else {
844-
throw new RuntimeException("Attempt to pass ineligible value to long: "+val);
845-
}
835+
float floatVal;
836+
if (val instanceof Number) {
837+
floatVal = ((Number) val).floatValue();
838+
} else if (val instanceof String) {
839+
floatVal = Float.parseFloat((String) val);
840+
} else {
841+
throw new RuntimeException("Attempt to pass ineligible value to float: "+val);
846842
}
847-
return new FloatByReference((Float)val);
843+
return new FloatByReference(floatVal);
848844

849845
case 'd':
850-
if ( !double.class.isInstance(val) ){
851-
if ( Number.class.isInstance(val) ){
852-
val = ((Number)val).doubleValue();
853-
} else if ( String.class.isInstance(val)){
854-
val = Double.valueOf((String) val);
855-
} else {
856-
throw new RuntimeException("Attempt to pass ineligible value to long: "+val);
857-
}
846+
double doubleVal;
847+
if (val instanceof Number) {
848+
doubleVal = ((Number) val).doubleValue();
849+
} else if (val instanceof String) {
850+
doubleVal = Double.parseDouble((String) val);
851+
} else {
852+
throw new RuntimeException("Attempt to pass ineligible value to double: "+val);
858853
}
859-
return new DoubleByReference((Double)val);
854+
return new DoubleByReference(doubleVal);
860855
case 'B':
861856
case 'b':
862857
case 'c':
863858
case 'C':
864-
if (Boolean.class.isInstance(val)) {
865-
val = (byte) (Boolean.TRUE.equals(val) ? 1 : 0);
866-
} else if (Number.class.isInstance(val)) {
867-
val = ((Number) val).byteValue();
868-
} else if (String.class.isInstance(val)) {
869-
val = Byte.valueOf((String) val);
870-
} else {
871-
throw new RuntimeException("Attempt to pass ineligible value to byte: " + val);
872-
}
873-
return new ByteByReference((Byte) val);
859+
byte byteVal;
860+
if (val instanceof Boolean) {
861+
byteVal = (byte) (Boolean.TRUE.equals(val) ? 1 : 0);
862+
} else if (val instanceof Number) {
863+
byteVal = ((Number) val).byteValue();
864+
} else if (val instanceof String) {
865+
byteVal = Byte.parseByte((String) val);
866+
} else {
867+
throw new RuntimeException("Attempt to pass ineligible value to byte: " + val);
868+
}
869+
return new ByteByReference(byteVal);
874870
case 'v':
875871
return null;
876872
case '^':
877873
default:
878-
if ( Pointer.class.isInstance(val) ){
874+
if (val instanceof Pointer) {
879875
return new PointerByReference((Pointer)val);
880-
} else if ( Long.class.isInstance(val) || long.class.isInstance(val)){
881-
return new PointerByReference(new Pointer((Long)val));
876+
} else if (val instanceof Long) {
877+
return new PointerByReference(new Pointer((Long) val));
882878
} else {
883879
throw new RuntimeException("Don't know what to do for conversion of value "+val+" and signature "+signature);
884-
}
885-
886-
887-
880+
}
888881
}
889882

890883
}

src/main/java/ca/weblite/objc/mappers/NSObjectMapping.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ private NSObjectMapping() { }
2828
public Object cToJ(Object cVar, String signature, TypeMapping root) {
2929
//System.out.println("Mapping NSObject to Java "+cVar+" sig: "+signature);
3030
Pointer cObj = Pointer.NULL;
31-
if ( Pointer.class.isInstance(cVar) ){
31+
if (cVar instanceof Pointer) {
3232
cObj = (Pointer)cVar;
33-
} else if (long.class.isInstance(cVar) || Long.class.isInstance(cVar) ){
34-
cObj = new Pointer((long) cVar);
33+
} else if (cVar instanceof Long) {
34+
cObj = new Pointer((Long) cVar);
3535
} else {
3636
return cVar;
3737
}
@@ -60,11 +60,10 @@ public Object jToC(Object jVar, String signature, TypeMapping root) {
6060
if ( jVar == null ){
6161
return Pointer.NULL;
6262
}
63-
if ( String.class.isInstance(jVar)){
64-
//////System.out.println("Converting string ["+jVar+"] to string");
63+
if (jVar instanceof String) {
6564
return RuntimeUtils.str((String)jVar);
6665
}
67-
if ( Peerable.class.isInstance(jVar)){
66+
if (jVar instanceof Peerable) {
6867
return ((Peerable)jVar).getPeer();
6968
} else if (jVar instanceof Pointer) {
7069
return jVar;

src/main/java/ca/weblite/objc/mappers/PointerMapping.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ private PointerMapping() { }
2121

2222
@Override
2323
public Object cToJ(Object cVar, String signature, TypeMapping root) {
24-
if ( Pointer.class.isInstance(cVar)) return cVar;
25-
return new Pointer((long) cVar);
24+
if (cVar instanceof Pointer) return cVar;
25+
return new Pointer((Long) cVar);
2626
}
2727

2828
@Override

src/main/java/ca/weblite/objc/mappers/ScalarMapping.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ private ScalarMapping() { }
2121
public Object cToJ(Object cVar, String signature, TypeMapping root) {
2222
//System.out.println("C to J for signature "+signature);
2323
char firstChar = signature.charAt(0);
24-
if ( Long.class.isInstance(cVar) || long.class.isInstance(cVar)){
25-
long cObj = (Long)cVar;
24+
if (cVar instanceof Long) {
25+
long cObj = (Long) cVar;
2626
switch (firstChar){
2727
case 'i':
2828
case 'I':

0 commit comments

Comments
 (0)