Skip to content
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

Code Cleanup: Java 16 instanceof pattern matching #707

Merged
merged 1 commit into from
Aug 12, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ public String toString() {

@Override
public boolean equals(Object obj) {
if (obj instanceof ApiAnnotations) {
ApiAnnotations desc = (ApiAnnotations) obj;
if (obj instanceof ApiAnnotations desc) {
return this.bits == desc.bits;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,7 @@ private void restoreNode(ProjectApiDescription apiDesc, Element element, Manifes
break;
}
case IApiXmlConstants.ELEMENT_FIELD:
if (parentNode.element instanceof IReferenceTypeDescriptor) {
IReferenceTypeDescriptor type = (IReferenceTypeDescriptor) parentNode.element;
if (parentNode.element instanceof IReferenceTypeDescriptor type) {
int vis = getInt(element, IApiXmlConstants.ATTR_VISIBILITY);
int res = getInt(element, IApiXmlConstants.ATTR_RESTRICTIONS);
String name = element.getAttribute(IApiXmlConstants.ATTR_NAME);
Expand All @@ -366,8 +365,7 @@ private void restoreNode(ProjectApiDescription apiDesc, Element element, Manifes
}
break;
case IApiXmlConstants.ELEMENT_METHOD:
if (parentNode.element instanceof IReferenceTypeDescriptor) {
IReferenceTypeDescriptor type = (IReferenceTypeDescriptor) parentNode.element;
if (parentNode.element instanceof IReferenceTypeDescriptor type) {
int vis = getInt(element, IApiXmlConstants.ATTR_VISIBILITY);
int res = getInt(element, IApiXmlConstants.ATTR_RESTRICTIONS);
String name = element.getAttribute(IApiXmlConstants.ATTR_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ public boolean isIncludeMinimum() {

@Override
public boolean equals(Object obj) {
if (obj instanceof BundleVersionRange) {
BundleVersionRange range = (BundleVersionRange) obj;
if (obj instanceof BundleVersionRange range) {
return fRange.equals(range.fRange);
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ public Key(int t, int m) {

@Override
public boolean equals(Object obj) {
if (obj instanceof Key) {
Key other = (Key) obj;
if (obj instanceof Key other) {
return type == other.type && member == other.member;
}
return super.equals(obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ public RequiredComponentDescription(String id, IVersionRange range, boolean isOp

@Override
public boolean equals(Object obj) {
if (obj instanceof RequiredComponentDescription) {
RequiredComponentDescription desc = (RequiredComponentDescription) obj;
if (obj instanceof RequiredComponentDescription desc) {
return fId.equals(desc.fId) && fRange.equals(desc.fRange);
}
return super.equals(obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ public boolean considerReference(IReference reference, IProgressMonitor monitor)
} catch (CoreException ce) {
// do nothing, skip it
}
if (member instanceof IApiMethod) {
IApiMethod method = (IApiMethod) member;
if (member instanceof IApiMethod method) {
if (method.isDefaultMethod()) {
return considerReference(reference, monitor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,7 @@ public List<IApiProblem> createProblems(IProgressMonitor monitor) {
IApiComponent component = reference.getMember().getApiComponent();
try {
IApiProblem problem = null;
if (component instanceof ProjectComponent) {
ProjectComponent ppac = (ProjectComponent) component;
if (component instanceof ProjectComponent ppac) {
IJavaProject project = ppac.getJavaProject();
problem = createProblem(reference, project);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -629,10 +629,9 @@ public boolean contains(ISchedulingRule rule) {

@Override
public boolean isConflicting(ISchedulingRule rule) {
if (!(rule instanceof ApiAnalysisJobRule)) {
if (!(rule instanceof ApiAnalysisJobRule other)) {
return false;
}
ApiAnalysisJobRule other = (ApiAnalysisJobRule) rule;
return project.equals(other.project);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2666,8 +2666,7 @@ public void checkBaselineMismatch(IApiBaseline baseline, IApiBaseline workspaceB
* @return Java project or <code>null</code>
*/
private IJavaProject getJavaProject(IApiComponent component) {
if (component instanceof ProjectComponent) {
ProjectComponent pp = (ProjectComponent) component;
if (component instanceof ProjectComponent pp) {
return pp.getJavaProject();
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,7 @@ public boolean visit(IResourceDelta delta) throws CoreException {
if (component != null) {
try {
IApiAnnotations annotations = component.getApiDescription().resolveAnnotations(Factory.typeDescriptor(type.replace('/', '.')));
if (annotations instanceof TypeAnnotations) {
TypeAnnotations ta = (TypeAnnotations) annotations;
if (annotations instanceof TypeAnnotations ta) {
if (ta.getBuildStamp() == BuildStamps.getBuildStamp(resource.getProject())) {
// note description change in
// addition to structure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@ public void setOwningComponent(IApiComponent component) {
* not a {@link PluginProjectApiComponent}
*/
private IProject getProject(IApiComponent component) {
if (component instanceof ProjectComponent) {
ProjectComponent comp = (ProjectComponent) component;
if (component instanceof ProjectComponent comp) {
return comp.getJavaProject().getProject();
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,7 @@ && getDefaultDefined(type, name, desc, false) != null) {
@Override
public void visitInvokeDynamicInsn(String name, String desc, Handle bsm, Object... bsmArgs) {
for (Object arg : bsmArgs) {
if (arg instanceof Handle) {
Handle handle = (Handle) arg;
if (arg instanceof Handle handle) {
Type declaringType = Type.getObjectType(handle.getOwner());
Reference reference = ReferenceExtractor.this.addMethodReference(declaringType, handle.getName(), handle.getDesc(), IReference.REF_VIRTUALMETHOD, 0);
if (reference != null) {
Expand Down Expand Up @@ -564,14 +563,12 @@ public void visitLocalVariable(String name, String desc, String signature, Label

@Override
public void visitLdcInsn(Object cst) {
if (cst instanceof Type) {
Type type = (Type) cst;
if (cst instanceof Type type) {
Reference reference = ReferenceExtractor.this.addTypeReference(type, IReference.REF_CONSTANTPOOL);
if (reference != null) {
this.linePositionTracker.addLocation(reference);
}
} else if (cst instanceof String) {
String str = (String) cst;
} else if (cst instanceof String str) {
this.stringLiteral = (Util.EMPTY_STRING.equals(str) ? null : str);
}
}
Expand Down Expand Up @@ -762,15 +759,13 @@ public void computeLineNumbers() {
remainingCatchLabelInfos = remaingEntriesTemp;
}
}
} else if (current instanceof Reference) {
Reference ref = (Reference) current;
} else if (current instanceof Reference ref) {
if (ref.getLineNumber() == -1) {
ref.setLineNumber(currentLineNumber);
} else {
currentLineNumber = ref.getLineNumber();
}
} else if (current instanceof LineInfo) {
LineInfo lineInfo = (LineInfo) current;
} else if (current instanceof LineInfo lineInfo) {
currentLineNumber = lineInfo.line;
}
}
Expand Down Expand Up @@ -810,8 +805,7 @@ public int compareTo(Object o) {

@Override
public boolean equals(Object obj) {
if (obj instanceof LineInfo) {
LineInfo lineInfo2 = (LineInfo) obj;
if (obj instanceof LineInfo lineInfo2) {
return this.line == lineInfo2.line && this.label.equals(lineInfo2.label);
}
return super.equals(obj);
Expand Down Expand Up @@ -841,8 +835,7 @@ public LocalLineNumberMarker(int line, int varIndex) {

@Override
public boolean equals(Object obj) {
if (obj instanceof LocalLineNumberMarker) {
LocalLineNumberMarker marker = (LocalLineNumberMarker) obj;
if (obj instanceof LocalLineNumberMarker marker) {
return this.lineNumber == marker.lineNumber && this.varIndex == marker.varIndex;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,7 @@ public List<IApiProblem> createProblems(IProgressMonitor monitor) {
try {
IApiProblem problem = null;
IApiComponent component = reference.getMember().getApiComponent();
if (component instanceof ProjectComponent) {
ProjectComponent ppac = (ProjectComponent) component;
if (component instanceof ProjectComponent ppac) {
IJavaProject project = ppac.getJavaProject();
problem = createProblem(reference, project);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,7 @@ protected int getParentModifiers(ASTNode node) {
if (node == null) {
return 0;
}
if (node instanceof AbstractTypeDeclaration) {
AbstractTypeDeclaration type = (AbstractTypeDeclaration) node;
if (node instanceof AbstractTypeDeclaration type) {
return type.getModifiers();
}
return getParentModifiers(node.getParent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,9 @@ public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (!(obj instanceof Delta)) {
if (!(obj instanceof Delta other)) {
return false;
}
Delta other = (Delta) obj;
if (this.elementType != other.elementType) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public String toString() {

@Override
public boolean equals(Object obj) {
if (obj instanceof IFieldDescriptor) {
IFieldDescriptor field = (IFieldDescriptor) obj;
if (obj instanceof IFieldDescriptor field) {
return getName().equals(field.getName()) && getEnclosingType().equals(field.getEnclosingType());
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ public String toString() {

@Override
public boolean equals(Object obj) {
if (obj instanceof IMethodDescriptor) {
IMethodDescriptor method = (IMethodDescriptor) obj;
if (obj instanceof IMethodDescriptor method) {
return getName().equals(method.getName()) && getEnclosingType().equals(method.getEnclosingType()) && getSignature().equals(method.getSignature());
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ public String toString() {

@Override
public boolean equals(Object obj) {
if (obj instanceof IPackageDescriptor) {
IPackageDescriptor pkg = (IPackageDescriptor) obj;
if (obj instanceof IPackageDescriptor pkg) {
return getName().equals(pkg.getName());
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ public synchronized String getQualifiedName() {

@Override
public boolean equals(Object obj) {
if (obj instanceof IReferenceTypeDescriptor) {
IReferenceTypeDescriptor refType = (IReferenceTypeDescriptor) obj;
if (obj instanceof IReferenceTypeDescriptor refType) {
return getQualifiedName().equals(refType.getQualifiedName());
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,7 @@ protected void addComponent(IApiComponent component) {
}

fComponentsById.put(component.getSymbolicName(), component);
if (component instanceof ProjectComponent) {
ProjectComponent projectApiComponent = (ProjectComponent) component;
if (component instanceof ProjectComponent projectApiComponent) {
if (this.fComponentsByProjectNames == null) {
this.fComponentsByProjectNames = new HashMap<>();
}
Expand Down Expand Up @@ -805,8 +804,7 @@ public void setName(String name) {

@Override
public boolean equals(Object obj) {
if (obj instanceof IApiBaseline) {
IApiBaseline baseline = (IApiBaseline) obj;
if (obj instanceof IApiBaseline baseline) {
return this.getName().equals(baseline.getName());
}
return super.equals(obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ public String getPackageName() {

@Override
public boolean equals(Object obj) {
if (obj instanceof IApiElement) {
IApiElement element = (IApiElement) obj;
if (obj instanceof IApiElement element) {
if (element.getType() == this.getType()) {
return enclosingTypesEqual(this, element) && getName().equals(element.getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,7 @@ private void reOrganizeComponents(IApiComponent[] components) throws CoreExcepti
if (javaEE != null) {
for (int i = 0; i < components.length; i++) {
IApiComponent iComponent = components[i];
if (iComponent instanceof SystemLibraryApiComponent) {
SystemLibraryApiComponent sysCom = (SystemLibraryApiComponent) iComponent;
if (iComponent instanceof SystemLibraryApiComponent sysCom) {
if (sysCom.getSymbolicName().equals(javaEE)) {
// swap i and 0;
IApiComponent temp = components[i];
Expand Down Expand Up @@ -495,8 +494,7 @@ public IMemberDescriptor getHandle() {

@Override
public boolean equals(Object obj) {
if (obj instanceof IApiType) {
IApiType type = (IApiType) obj;
if (obj instanceof IApiType type) {
if (getApiComponent() == null) {
return type.getApiComponent() == null && getName().equals(type.getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import java.util.stream.Stream;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.internal.compiler.util.JRTUtil;

Check warning on line 31 in apitools/org.eclipse.pde.api.tools/src/org/eclipse/pde/api/tools/internal/model/ArchiveApiTypeContainer.java

View check run for this annotation

Jenkins - eclipse-pde / Java Compiler

tycho-compiler:compile

NORMAL:
import org.eclipse.pde.api.tools.internal.provisional.model.ApiTypeContainerVisitor;
import org.eclipse.pde.api.tools.internal.provisional.model.IApiElement;
import org.eclipse.pde.api.tools.internal.provisional.model.IApiTypeContainer;
Expand Down Expand Up @@ -81,8 +81,7 @@

@Override
public boolean equals(Object obj) {
if (obj instanceof ArchiveApiTypeRoot) {
ArchiveApiTypeRoot classFile = (ArchiveApiTypeRoot) obj;
if (obj instanceof ArchiveApiTypeRoot classFile) {
return this.getName().equals(classFile.getName());
}
return false;
Expand Down Expand Up @@ -168,7 +167,7 @@
}
return jrtFileSystem.getPath("modules");
} else {
return JRTUtil.getJarFileSystem(path).getPath("/");

Check warning on line 170 in apitools/org.eclipse.pde.api.tools/src/org/eclipse/pde/api/tools/internal/model/ArchiveApiTypeContainer.java

View check run for this annotation

Jenkins - eclipse-pde / Java Compiler

tycho-compiler:compile

NORMAL:
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,7 @@ public boolean isValidBundle() throws CoreException {

@Override
public boolean equals(Object obj) {
if (obj instanceof BundleComponent) {
BundleComponent comp = (BundleComponent) obj;
if (obj instanceof BundleComponent comp) {
return getName().equals(comp.getName()) && getSymbolicName().equals(comp.getSymbolicName()) && getVersion().equals(comp.getVersion());
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ public MethodKey(String typename, String name, String sig, boolean considertypen

@Override
public boolean equals(Object obj) {
if (obj instanceof MethodKey) {
MethodKey key = (MethodKey) obj;
if (obj instanceof MethodKey key) {
return fSelector.equals(key.fSelector) && signaturesEqual(fSig, key.fSig) && (fConsiderTypename ? fTypename.equals(key.fTypename) : true);
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ public int hashCode() {

@Override
public boolean equals(Object obj) {
if (obj instanceof IApiTypeRoot) {
IApiTypeRoot file = (IApiTypeRoot) obj;
if (obj instanceof IApiTypeRoot file) {
return getName().equals(file.getTypeName());
}
return super.equals(obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ public int compareTo(Object o) {

@Override
public boolean equals(Object obj) {
if (obj instanceof ArchiveApiTypeRoot) {
ArchiveApiTypeRoot classFile = (ArchiveApiTypeRoot) obj;
if (obj instanceof ArchiveApiTypeRoot classFile) {
return this.getName().equals(classFile.getName());
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;

Check warning on line 25 in apitools/org.eclipse.pde.api.tools/src/org/eclipse/pde/api/tools/internal/model/TypeStructureBuilder.java

View check run for this annotation

Jenkins - eclipse-pde / Java Compiler

tycho-compiler:compile

NORMAL:
import org.eclipse.osgi.util.NLS;
import org.eclipse.pde.api.tools.internal.model.StubArchiveApiTypeContainer.ArchiveApiTypeRoot;
import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin;
Expand Down Expand Up @@ -78,7 +78,7 @@
// component
if ((laccess & Opcodes.ACC_DEPRECATED) != 0) {
laccess &= ~Opcodes.ACC_DEPRECATED;
laccess |= ClassFileConstants.AccDeprecated;

Check warning on line 81 in apitools/org.eclipse.pde.api.tools/src/org/eclipse/pde/api/tools/internal/model/TypeStructureBuilder.java

View check run for this annotation

Jenkins - eclipse-pde / Java Compiler

tycho-compiler:compile

NORMAL:
}
fType = new ApiType(fComponent, name.replace('/', '.'), simpleSig.toString(), signature, laccess, enclosingName, fFile);
if (superName != null) {
Expand Down Expand Up @@ -133,7 +133,7 @@
int laccess = access;
if ((access & Opcodes.ACC_DEPRECATED) != 0) {
laccess &= ~Opcodes.ACC_DEPRECATED;
laccess |= ClassFileConstants.AccDeprecated;

Check warning on line 136 in apitools/org.eclipse.pde.api.tools/src/org/eclipse/pde/api/tools/internal/model/TypeStructureBuilder.java

View check run for this annotation

Jenkins - eclipse-pde / Java Compiler

tycho-compiler:compile

NORMAL:
}
fType.addField(name, desc, signature, laccess, value);
return null;
Expand All @@ -145,7 +145,7 @@
int laccess = access;
if ((laccess & Opcodes.ACC_DEPRECATED) != 0) {
laccess &= ~Opcodes.ACC_DEPRECATED;
laccess |= ClassFileConstants.AccDeprecated;

Check warning on line 148 in apitools/org.eclipse.pde.api.tools/src/org/eclipse/pde/api/tools/internal/model/TypeStructureBuilder.java

View check run for this annotation

Jenkins - eclipse-pde / Java Compiler

tycho-compiler:compile

NORMAL:
}
if (exceptions != null && exceptions.length > 0) {
names = new String[exceptions.length];
Expand Down Expand Up @@ -282,8 +282,7 @@
*/
public static void setEnclosingMethod(IApiType enclosingType, ApiType currentAnonymousLocalType) {
IApiTypeRoot typeRoot = enclosingType.getTypeRoot();
if (typeRoot instanceof AbstractApiTypeRoot) {
AbstractApiTypeRoot abstractApiTypeRoot = (AbstractApiTypeRoot) typeRoot;
if (typeRoot instanceof AbstractApiTypeRoot abstractApiTypeRoot) {
EnclosingMethodSetter visitor = new EnclosingMethodSetter(new ClassNode(), currentAnonymousLocalType.getName());
try {
ClassReader classReader = new ClassReader(abstractApiTypeRoot.getContents());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,7 @@ public String[] getMessageArguments() {

@Override
public boolean equals(Object obj) {
if (obj instanceof IApiProblem) {
IApiProblem problem = (IApiProblem) obj;
if (obj instanceof IApiProblem problem) {
if (problem.getId() == fId && argumentsEqual(problem.getMessageArguments())) {
String resourcePath = problem.getResourcePath();
if (resourcePath == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ public String getComponentId() {

@Override
public boolean equals(Object obj) {
if (obj instanceof IApiProblemFilter) {
IApiProblemFilter filter = (IApiProblemFilter) obj;
if (obj instanceof IApiProblemFilter filter) {
return elementsEqual(filter.getComponentId(), fComponentId) && filter.getUnderlyingProblem().equals(fProblem);
} else if (obj instanceof IApiProblem) {
return fProblem.equals(obj);
Expand Down
Loading
Loading