Skip to content

[PATCH] Pattern matching of instanceof in java.desktop shared code #26662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions src/java.desktop/share/classes/com/sun/beans/TypeResolver.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -164,14 +164,12 @@ public static Type resolve(Type actual, Type formal) {
? Array.newInstance((Class<?>) comp, 0).getClass()
: GenericArrayTypeImpl.make(comp);
}
if (formal instanceof ParameterizedType) {
ParameterizedType fpt = (ParameterizedType) formal;
if (formal instanceof ParameterizedType fpt) {
Type[] actuals = resolve(actual, fpt.getActualTypeArguments());
return ParameterizedTypeImpl.make(
(Class<?>) fpt.getRawType(), actuals, fpt.getOwnerType());
}
if (formal instanceof WildcardType) {
WildcardType fwt = (WildcardType) formal;
if (formal instanceof WildcardType fwt) {
Type[] upper = resolve(actual, fwt.getUpperBounds());
Type[] lower = resolve(actual, fwt.getLowerBounds());
return new WildcardTypeImpl(upper, lower);
Expand Down Expand Up @@ -233,26 +231,22 @@ public static Class<?> erase(Type type) {
if (type instanceof Class) {
return (Class<?>) type;
}
if (type instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) type;
if (type instanceof ParameterizedType pt) {
return (Class<?>) pt.getRawType();
}
if (type instanceof TypeVariable) {
TypeVariable<?> tv = (TypeVariable<?>)type;
if (type instanceof TypeVariable<?> tv) {
Type[] bounds = tv.getBounds();
return (0 < bounds.length)
? erase(bounds[0])
: Object.class;
}
if (type instanceof WildcardType) {
WildcardType wt = (WildcardType)type;
if (type instanceof WildcardType wt) {
Type[] bounds = wt.getUpperBounds();
return (0 < bounds.length)
? erase(bounds[0])
: Object.class;
}
if (type instanceof GenericArrayType) {
GenericArrayType gat = (GenericArrayType)type;
if (type instanceof GenericArrayType gat) {
return Array.newInstance(erase(gat.getGenericComponentType()), 0).getClass();
}
throw new IllegalArgumentException("Unknown Type kind: " + type.getClass());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -103,8 +103,7 @@ public Type[] getLowerBounds() {
*/
@Override
public boolean equals(Object object) {
if (object instanceof WildcardType) {
WildcardType type = (WildcardType) object;
if (object instanceof WildcardType type) {
return Arrays.equals(this.upperBounds, type.getUpperBounds())
&& Arrays.equals(this.lowerBounds, type.getLowerBounds());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -126,8 +126,7 @@ private Object getValue() {
if ((this.type == null) || isValid(owner)) {
return owner;
}
if (owner instanceof XMLDecoder) {
XMLDecoder decoder = (XMLDecoder) owner;
if (owner instanceof XMLDecoder decoder) {
owner = decoder.getOwner();
if (isValid(owner)) {
return owner;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -228,8 +228,7 @@ private static Method findGetter(Class<?> type, String name, Class<?>...args) th
if (method != null) {
return method;
}
} else if (pd instanceof IndexedPropertyDescriptor) {
IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
} else if (pd instanceof IndexedPropertyDescriptor ipd) {
Method method = ipd.getIndexedReadMethod();
if (method != null) {
return method;
Expand Down Expand Up @@ -259,8 +258,7 @@ private static Method findSetter(Class<?> type, String name, Class<?>...args) th
if (method != null) {
return method;
}
} else if (pd instanceof IndexedPropertyDescriptor) {
IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
} else if (pd instanceof IndexedPropertyDescriptor ipd) {
Method method = ipd.getIndexedWriteMethod();
if (method != null) {
return method;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,10 @@ public static Method findAccessibleMethod(Method method) throws NoSuchMethodExce
private static Method findAccessibleMethod(Method method, Type generic) throws NoSuchMethodException {
String name = method.getName();
Class<?>[] params = method.getParameterTypes();
if (generic instanceof Class) {
Class<?> type = (Class<?>) generic;
if (generic instanceof Class<?> type) {
return findAccessibleMethod(type.getMethod(name, params));
}
if (generic instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) generic;
if (generic instanceof ParameterizedType pt) {
Class<?> type = (Class<?>) pt.getRawType();
for (Method m : type.getMethods()) {
if (m.getName().equals(name)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -87,8 +87,7 @@ public boolean equals(Object object) {
if (this == object) {
return true;
}
if (object instanceof Signature) {
Signature signature = (Signature) object;
if (object instanceof Signature signature) {
return isEqual(signature.type, this.type)
&& isEqual(signature.name, this.name)
&& isEqual(signature.args, this.args);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -74,11 +74,10 @@ public String getDescription(Locale locale) {
}

public boolean canDecodeInput(Object source) throws IOException {
if (!(source instanceof ImageInputStream)) {
if (!(source instanceof ImageInputStream stream)) {
return false;
}

ImageInputStream stream = (ImageInputStream)source;
byte[] b = new byte[2];
stream.mark();
boolean full = ReaderUtil.tryReadFully(stream, b);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -367,8 +367,7 @@ public void write(IIOMetadata streamMetadata,
* model types but it might be non compatible with win98
* and friends.
*/
if (colorModel instanceof DirectColorModel) {
DirectColorModel dcm = (DirectColorModel)colorModel;
if (colorModel instanceof DirectColorModel dcm) {
rmask = dcm.getRedMask();
gmask = dcm.getGreenMask();
bmask = dcm.getBlueMask();
Expand Down Expand Up @@ -637,21 +636,16 @@ public void write(IIOMetadata streamMetadata,
int pos = 0;
int startX = srcRect.x - src.getSampleModelTranslateX();
int startY = srcRect.y - src.getSampleModelTranslateY();
if (sm instanceof ComponentSampleModel) {
ComponentSampleModel csm = (ComponentSampleModel)sm;
if (sm instanceof ComponentSampleModel csm) {
pos = csm.getOffset(startX, startY, 0);
for(int nb=1; nb < csm.getNumBands(); nb++) {
if (pos > csm.getOffset(startX, startY, nb)) {
pos = csm.getOffset(startX, startY, nb);
}
}
} else if (sm instanceof MultiPixelPackedSampleModel) {
MultiPixelPackedSampleModel mppsm =
(MultiPixelPackedSampleModel)sm;
} else if (sm instanceof MultiPixelPackedSampleModel mppsm) {
pos = mppsm.getOffset(startX, startY);
} else if (sm instanceof SinglePixelPackedSampleModel) {
SinglePixelPackedSampleModel sppsm =
(SinglePixelPackedSampleModel)sm;
} else if (sm instanceof SinglePixelPackedSampleModel sppsm) {
pos = sppsm.getOffset(startX, startY);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -142,9 +142,7 @@ public static final ColorModel createColorModel(SampleModel sampleModel) {
transparency,
dataType);
} else if (sampleModel.getNumBands() <= 4 &&
sampleModel instanceof SinglePixelPackedSampleModel) {
SinglePixelPackedSampleModel sppsm =
(SinglePixelPackedSampleModel)sampleModel;
sampleModel instanceof SinglePixelPackedSampleModel sppsm) {

int[] bitMasks = sppsm.getBitMasks();
int rmask = 0;
Expand Down Expand Up @@ -844,9 +842,7 @@ public static ColorModel createColorModel(ColorSpace colorSpace,
premultiplied,
transparency,
dataType);
} else if (sampleModel instanceof SinglePixelPackedSampleModel) {
SinglePixelPackedSampleModel sppsm =
(SinglePixelPackedSampleModel)sampleModel;
} else if (sampleModel instanceof SinglePixelPackedSampleModel sppsm) {

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

if (sm instanceof MultiPixelPackedSampleModel) {
MultiPixelPackedSampleModel mppsm =
(MultiPixelPackedSampleModel)sm;
if (sm instanceof MultiPixelPackedSampleModel mppsm) {
return mppsm.getSampleSize(0) * mppsm.getNumBands();
} else if (sm instanceof ComponentSampleModel) {
return sm.getNumBands() * elementSize;
Expand All @@ -918,14 +912,11 @@ public static int getElementSize(SampleModel sm) {
public static long getTileSize(SampleModel sm) {
int elementSize = DataBuffer.getDataTypeSize(sm.getDataType());

if (sm instanceof MultiPixelPackedSampleModel) {
MultiPixelPackedSampleModel mppsm =
(MultiPixelPackedSampleModel)sm;
if (sm instanceof MultiPixelPackedSampleModel mppsm) {
return (mppsm.getScanlineStride() * mppsm.getHeight() +
(mppsm.getDataBitOffset() + elementSize -1) / elementSize) *
((elementSize + 7) / 8);
} else if (sm instanceof ComponentSampleModel) {
ComponentSampleModel csm = (ComponentSampleModel)sm;
} else if (sm instanceof ComponentSampleModel csm) {
int[] bandOffsets = csm.getBandOffsets();
int maxBandOff = bandOffsets[0];
for (int i=1; i<bandOffsets.length; i++)
Expand All @@ -946,9 +937,7 @@ public static long getTileSize(SampleModel sm) {
for (int i=1; i<bankIndices.length; i++)
maxBandOff = Math.max(maxBandOff, bankIndices[i]);
return size * (maxBandOff + 1) * ((elementSize + 7) / 8);
} else if (sm instanceof SinglePixelPackedSampleModel) {
SinglePixelPackedSampleModel sppsm =
(SinglePixelPackedSampleModel)sm;
} else if (sm instanceof SinglePixelPackedSampleModel sppsm) {
long size = sppsm.getScanlineStride() * (sppsm.getHeight() - 1) +
sppsm.getWidth();
return size * ((elementSize + 7) / 8);
Expand All @@ -960,8 +949,7 @@ public static long getTileSize(SampleModel sm) {
public static long getBandSize(SampleModel sm) {
int elementSize = DataBuffer.getDataTypeSize(sm.getDataType());

if (sm instanceof ComponentSampleModel) {
ComponentSampleModel csm = (ComponentSampleModel)sm;
if (sm instanceof ComponentSampleModel csm) {
int pixelStride = csm.getPixelStride();
int scanlineStride = csm.getScanlineStride();
long size = Math.min(pixelStride, scanlineStride);
Expand Down Expand Up @@ -1008,22 +996,19 @@ public static String convertObjectToString(Object obj) {
return "";

String s = "";
if (obj instanceof byte[]) {
byte[] bArray = (byte[])obj;
if (obj instanceof byte[] bArray) {
for (int i = 0; i < bArray.length; i++)
s += bArray[i] + " ";
return s;
}

if (obj instanceof int[]) {
int[] iArray = (int[])obj;
if (obj instanceof int[] iArray) {
for (int i = 0; i < iArray.length; i++)
s += iArray[i] + " " ;
return s;
}

if (obj instanceof short[]) {
short[] sArray = (short[])obj;
if (obj instanceof short[] sArray) {
for (int i = 0; i < sArray.length; i++)
s += sArray[i] + " " ;
return s;
Expand Down Expand Up @@ -1081,10 +1066,9 @@ public static final boolean imageIsContiguous(RenderedImage image) {
sm = image.getSampleModel();
}

if (sm instanceof ComponentSampleModel) {
if (sm instanceof ComponentSampleModel csm) {
// Ensure image rows samples are stored contiguously
// in a single bank.
ComponentSampleModel csm = (ComponentSampleModel)sm;

if (csm.getPixelStride() != csm.getNumBands()) {
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -76,11 +76,10 @@ public String getDescription(Locale locale) {
}

public boolean canDecodeInput(Object input) throws IOException {
if (!(input instanceof ImageInputStream)) {
if (!(input instanceof ImageInputStream stream)) {
return false;
}

ImageInputStream stream = (ImageInputStream)input;
byte[] b = new byte[6];
stream.mark();
boolean full = ReaderUtil.tryReadFully(stream, b);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -184,8 +184,7 @@ private static byte[] createColorTable(ColorModel colorModel,
SampleModel sampleModel)
{
byte[] colorTable;
if (colorModel instanceof IndexColorModel) {
IndexColorModel icm = (IndexColorModel)colorModel;
if (colorModel instanceof IndexColorModel icm) {
int mapSize = icm.getMapSize();

/**
Expand Down Expand Up @@ -623,9 +622,7 @@ private void write(boolean writeHeader,

// in case of indexed image we should take care of
// transparent pixels
if (colorModel instanceof IndexColorModel) {
IndexColorModel icm =
(IndexColorModel)colorModel;
if (colorModel instanceof IndexColorModel icm) {
int index = icm.getTransparentPixel();
imageMetadata.transparentColorFlag = (index != -1);
if (imageMetadata.transparentColorFlag) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -75,8 +75,7 @@ class COMMarkerSegment extends MarkerSegment {
*/
COMMarkerSegment(Node node) throws IIOInvalidTreeException{
super(JPEG.COM);
if (node instanceof IIOMetadataNode) {
IIOMetadataNode ourNode = (IIOMetadataNode) node;
if (node instanceof IIOMetadataNode ourNode) {
data = (byte []) ourNode.getUserObject();
}
if (data == null) {
Expand Down
Loading