Skip to content

Commit 80d3a6f

Browse files
committed
[PATCH] Pattern matching of instanceof in java.desktop shared code
1 parent f95af74 commit 80d3a6f

File tree

271 files changed

+824
-1453
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

271 files changed

+824
-1453
lines changed

src/java.desktop/share/classes/com/sun/beans/TypeResolver.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -164,14 +164,12 @@ public static Type resolve(Type actual, Type formal) {
164164
? Array.newInstance((Class<?>) comp, 0).getClass()
165165
: GenericArrayTypeImpl.make(comp);
166166
}
167-
if (formal instanceof ParameterizedType) {
168-
ParameterizedType fpt = (ParameterizedType) formal;
167+
if (formal instanceof ParameterizedType fpt) {
169168
Type[] actuals = resolve(actual, fpt.getActualTypeArguments());
170169
return ParameterizedTypeImpl.make(
171170
(Class<?>) fpt.getRawType(), actuals, fpt.getOwnerType());
172171
}
173-
if (formal instanceof WildcardType) {
174-
WildcardType fwt = (WildcardType) formal;
172+
if (formal instanceof WildcardType fwt) {
175173
Type[] upper = resolve(actual, fwt.getUpperBounds());
176174
Type[] lower = resolve(actual, fwt.getLowerBounds());
177175
return new WildcardTypeImpl(upper, lower);
@@ -233,26 +231,22 @@ public static Class<?> erase(Type type) {
233231
if (type instanceof Class) {
234232
return (Class<?>) type;
235233
}
236-
if (type instanceof ParameterizedType) {
237-
ParameterizedType pt = (ParameterizedType) type;
234+
if (type instanceof ParameterizedType pt) {
238235
return (Class<?>) pt.getRawType();
239236
}
240-
if (type instanceof TypeVariable) {
241-
TypeVariable<?> tv = (TypeVariable<?>)type;
237+
if (type instanceof TypeVariable<?> tv) {
242238
Type[] bounds = tv.getBounds();
243239
return (0 < bounds.length)
244240
? erase(bounds[0])
245241
: Object.class;
246242
}
247-
if (type instanceof WildcardType) {
248-
WildcardType wt = (WildcardType)type;
243+
if (type instanceof WildcardType wt) {
249244
Type[] bounds = wt.getUpperBounds();
250245
return (0 < bounds.length)
251246
? erase(bounds[0])
252247
: Object.class;
253248
}
254-
if (type instanceof GenericArrayType) {
255-
GenericArrayType gat = (GenericArrayType)type;
249+
if (type instanceof GenericArrayType gat) {
256250
return Array.newInstance(erase(gat.getGenericComponentType()), 0).getClass();
257251
}
258252
throw new IllegalArgumentException("Unknown Type kind: " + type.getClass());

src/java.desktop/share/classes/com/sun/beans/WildcardTypeImpl.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -103,8 +103,7 @@ public Type[] getLowerBounds() {
103103
*/
104104
@Override
105105
public boolean equals(Object object) {
106-
if (object instanceof WildcardType) {
107-
WildcardType type = (WildcardType) object;
106+
if (object instanceof WildcardType type) {
108107
return Arrays.equals(this.upperBounds, type.getUpperBounds())
109108
&& Arrays.equals(this.lowerBounds, type.getLowerBounds());
110109
}

src/java.desktop/share/classes/com/sun/beans/decoder/JavaElementHandler.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -126,8 +126,7 @@ private Object getValue() {
126126
if ((this.type == null) || isValid(owner)) {
127127
return owner;
128128
}
129-
if (owner instanceof XMLDecoder) {
130-
XMLDecoder decoder = (XMLDecoder) owner;
129+
if (owner instanceof XMLDecoder decoder) {
131130
owner = decoder.getOwner();
132131
if (isValid(owner)) {
133132
return owner;

src/java.desktop/share/classes/com/sun/beans/decoder/PropertyElementHandler.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -228,8 +228,7 @@ private static Method findGetter(Class<?> type, String name, Class<?>...args) th
228228
if (method != null) {
229229
return method;
230230
}
231-
} else if (pd instanceof IndexedPropertyDescriptor) {
232-
IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
231+
} else if (pd instanceof IndexedPropertyDescriptor ipd) {
233232
Method method = ipd.getIndexedReadMethod();
234233
if (method != null) {
235234
return method;
@@ -259,8 +258,7 @@ private static Method findSetter(Class<?> type, String name, Class<?>...args) th
259258
if (method != null) {
260259
return method;
261260
}
262-
} else if (pd instanceof IndexedPropertyDescriptor) {
263-
IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
261+
} else if (pd instanceof IndexedPropertyDescriptor ipd) {
264262
Method method = ipd.getIndexedWriteMethod();
265263
if (method != null) {
266264
return method;

src/java.desktop/share/classes/com/sun/beans/finder/MethodFinder.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,10 @@ public static Method findAccessibleMethod(Method method) throws NoSuchMethodExce
165165
private static Method findAccessibleMethod(Method method, Type generic) throws NoSuchMethodException {
166166
String name = method.getName();
167167
Class<?>[] params = method.getParameterTypes();
168-
if (generic instanceof Class) {
169-
Class<?> type = (Class<?>) generic;
168+
if (generic instanceof Class<?> type) {
170169
return findAccessibleMethod(type.getMethod(name, params));
171170
}
172-
if (generic instanceof ParameterizedType) {
173-
ParameterizedType pt = (ParameterizedType) generic;
171+
if (generic instanceof ParameterizedType pt) {
174172
Class<?> type = (Class<?>) pt.getRawType();
175173
for (Method m : type.getMethods()) {
176174
if (m.getName().equals(name)) {

src/java.desktop/share/classes/com/sun/beans/finder/Signature.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -87,8 +87,7 @@ public boolean equals(Object object) {
8787
if (this == object) {
8888
return true;
8989
}
90-
if (object instanceof Signature) {
91-
Signature signature = (Signature) object;
90+
if (object instanceof Signature signature) {
9291
return isEqual(signature.type, this.type)
9392
&& isEqual(signature.name, this.name)
9493
&& isEqual(signature.args, this.args);

src/java.desktop/share/classes/com/sun/imageio/plugins/bmp/BMPImageReaderSpi.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -74,11 +74,10 @@ public String getDescription(Locale locale) {
7474
}
7575

7676
public boolean canDecodeInput(Object source) throws IOException {
77-
if (!(source instanceof ImageInputStream)) {
77+
if (!(source instanceof ImageInputStream stream)) {
7878
return false;
7979
}
8080

81-
ImageInputStream stream = (ImageInputStream)source;
8281
byte[] b = new byte[2];
8382
stream.mark();
8483
boolean full = ReaderUtil.tryReadFully(stream, b);

src/java.desktop/share/classes/com/sun/imageio/plugins/bmp/BMPImageWriter.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -367,8 +367,7 @@ public void write(IIOMetadata streamMetadata,
367367
* model types but it might be non compatible with win98
368368
* and friends.
369369
*/
370-
if (colorModel instanceof DirectColorModel) {
371-
DirectColorModel dcm = (DirectColorModel)colorModel;
370+
if (colorModel instanceof DirectColorModel dcm) {
372371
rmask = dcm.getRedMask();
373372
gmask = dcm.getGreenMask();
374373
bmask = dcm.getBlueMask();
@@ -637,21 +636,16 @@ public void write(IIOMetadata streamMetadata,
637636
int pos = 0;
638637
int startX = srcRect.x - src.getSampleModelTranslateX();
639638
int startY = srcRect.y - src.getSampleModelTranslateY();
640-
if (sm instanceof ComponentSampleModel) {
641-
ComponentSampleModel csm = (ComponentSampleModel)sm;
639+
if (sm instanceof ComponentSampleModel csm) {
642640
pos = csm.getOffset(startX, startY, 0);
643641
for(int nb=1; nb < csm.getNumBands(); nb++) {
644642
if (pos > csm.getOffset(startX, startY, nb)) {
645643
pos = csm.getOffset(startX, startY, nb);
646644
}
647645
}
648-
} else if (sm instanceof MultiPixelPackedSampleModel) {
649-
MultiPixelPackedSampleModel mppsm =
650-
(MultiPixelPackedSampleModel)sm;
646+
} else if (sm instanceof MultiPixelPackedSampleModel mppsm) {
651647
pos = mppsm.getOffset(startX, startY);
652-
} else if (sm instanceof SinglePixelPackedSampleModel) {
653-
SinglePixelPackedSampleModel sppsm =
654-
(SinglePixelPackedSampleModel)sm;
648+
} else if (sm instanceof SinglePixelPackedSampleModel sppsm) {
655649
pos = sppsm.getOffset(startX, startY);
656650
}
657651

src/java.desktop/share/classes/com/sun/imageio/plugins/common/ImageUtil.java

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -142,9 +142,7 @@ public static final ColorModel createColorModel(SampleModel sampleModel) {
142142
transparency,
143143
dataType);
144144
} else if (sampleModel.getNumBands() <= 4 &&
145-
sampleModel instanceof SinglePixelPackedSampleModel) {
146-
SinglePixelPackedSampleModel sppsm =
147-
(SinglePixelPackedSampleModel)sampleModel;
145+
sampleModel instanceof SinglePixelPackedSampleModel sppsm) {
148146

149147
int[] bitMasks = sppsm.getBitMasks();
150148
int rmask = 0;
@@ -844,9 +842,7 @@ public static ColorModel createColorModel(ColorSpace colorSpace,
844842
premultiplied,
845843
transparency,
846844
dataType);
847-
} else if (sampleModel instanceof SinglePixelPackedSampleModel) {
848-
SinglePixelPackedSampleModel sppsm =
849-
(SinglePixelPackedSampleModel)sampleModel;
845+
} else if (sampleModel instanceof SinglePixelPackedSampleModel sppsm) {
850846

851847
int[] bitMasks = sppsm.getBitMasks();
852848
int rmask = 0;
@@ -901,9 +897,7 @@ public static ColorModel createColorModel(ColorSpace colorSpace,
901897
public static int getElementSize(SampleModel sm) {
902898
int elementSize = DataBuffer.getDataTypeSize(sm.getDataType());
903899

904-
if (sm instanceof MultiPixelPackedSampleModel) {
905-
MultiPixelPackedSampleModel mppsm =
906-
(MultiPixelPackedSampleModel)sm;
900+
if (sm instanceof MultiPixelPackedSampleModel mppsm) {
907901
return mppsm.getSampleSize(0) * mppsm.getNumBands();
908902
} else if (sm instanceof ComponentSampleModel) {
909903
return sm.getNumBands() * elementSize;
@@ -918,14 +912,11 @@ public static int getElementSize(SampleModel sm) {
918912
public static long getTileSize(SampleModel sm) {
919913
int elementSize = DataBuffer.getDataTypeSize(sm.getDataType());
920914

921-
if (sm instanceof MultiPixelPackedSampleModel) {
922-
MultiPixelPackedSampleModel mppsm =
923-
(MultiPixelPackedSampleModel)sm;
915+
if (sm instanceof MultiPixelPackedSampleModel mppsm) {
924916
return (mppsm.getScanlineStride() * mppsm.getHeight() +
925917
(mppsm.getDataBitOffset() + elementSize -1) / elementSize) *
926918
((elementSize + 7) / 8);
927-
} else if (sm instanceof ComponentSampleModel) {
928-
ComponentSampleModel csm = (ComponentSampleModel)sm;
919+
} else if (sm instanceof ComponentSampleModel csm) {
929920
int[] bandOffsets = csm.getBandOffsets();
930921
int maxBandOff = bandOffsets[0];
931922
for (int i=1; i<bandOffsets.length; i++)
@@ -946,9 +937,7 @@ public static long getTileSize(SampleModel sm) {
946937
for (int i=1; i<bankIndices.length; i++)
947938
maxBandOff = Math.max(maxBandOff, bankIndices[i]);
948939
return size * (maxBandOff + 1) * ((elementSize + 7) / 8);
949-
} else if (sm instanceof SinglePixelPackedSampleModel) {
950-
SinglePixelPackedSampleModel sppsm =
951-
(SinglePixelPackedSampleModel)sm;
940+
} else if (sm instanceof SinglePixelPackedSampleModel sppsm) {
952941
long size = sppsm.getScanlineStride() * (sppsm.getHeight() - 1) +
953942
sppsm.getWidth();
954943
return size * ((elementSize + 7) / 8);
@@ -960,8 +949,7 @@ public static long getTileSize(SampleModel sm) {
960949
public static long getBandSize(SampleModel sm) {
961950
int elementSize = DataBuffer.getDataTypeSize(sm.getDataType());
962951

963-
if (sm instanceof ComponentSampleModel) {
964-
ComponentSampleModel csm = (ComponentSampleModel)sm;
952+
if (sm instanceof ComponentSampleModel csm) {
965953
int pixelStride = csm.getPixelStride();
966954
int scanlineStride = csm.getScanlineStride();
967955
long size = Math.min(pixelStride, scanlineStride);
@@ -1008,22 +996,19 @@ public static String convertObjectToString(Object obj) {
1008996
return "";
1009997

1010998
String s = "";
1011-
if (obj instanceof byte[]) {
1012-
byte[] bArray = (byte[])obj;
999+
if (obj instanceof byte[] bArray) {
10131000
for (int i = 0; i < bArray.length; i++)
10141001
s += bArray[i] + " ";
10151002
return s;
10161003
}
10171004

1018-
if (obj instanceof int[]) {
1019-
int[] iArray = (int[])obj;
1005+
if (obj instanceof int[] iArray) {
10201006
for (int i = 0; i < iArray.length; i++)
10211007
s += iArray[i] + " " ;
10221008
return s;
10231009
}
10241010

1025-
if (obj instanceof short[]) {
1026-
short[] sArray = (short[])obj;
1011+
if (obj instanceof short[] sArray) {
10271012
for (int i = 0; i < sArray.length; i++)
10281013
s += sArray[i] + " " ;
10291014
return s;
@@ -1081,10 +1066,9 @@ public static final boolean imageIsContiguous(RenderedImage image) {
10811066
sm = image.getSampleModel();
10821067
}
10831068

1084-
if (sm instanceof ComponentSampleModel) {
1069+
if (sm instanceof ComponentSampleModel csm) {
10851070
// Ensure image rows samples are stored contiguously
10861071
// in a single bank.
1087-
ComponentSampleModel csm = (ComponentSampleModel)sm;
10881072

10891073
if (csm.getPixelStride() != csm.getNumBands()) {
10901074
return false;

src/java.desktop/share/classes/com/sun/imageio/plugins/gif/GIFImageReaderSpi.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -76,11 +76,10 @@ public String getDescription(Locale locale) {
7676
}
7777

7878
public boolean canDecodeInput(Object input) throws IOException {
79-
if (!(input instanceof ImageInputStream)) {
79+
if (!(input instanceof ImageInputStream stream)) {
8080
return false;
8181
}
8282

83-
ImageInputStream stream = (ImageInputStream)input;
8483
byte[] b = new byte[6];
8584
stream.mark();
8685
boolean full = ReaderUtil.tryReadFully(stream, b);

0 commit comments

Comments
 (0)