Skip to content

Commit

Permalink
Code Cleanup: Java 16 instanceof pattern matching in pde.ui
Browse files Browse the repository at this point in the history
  • Loading branch information
alshamams committed Sep 22, 2023
1 parent f8a1dc3 commit 5e9c236
Show file tree
Hide file tree
Showing 196 changed files with 453 additions and 885 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,7 @@ public String getObjectText(IPluginLibrary obj) {

public String getObjectText(ISchemaObject obj) {
StringBuilder text = new StringBuilder(obj.getName());
if (obj instanceof ISchemaRepeatable) {
ISchemaRepeatable rso = (ISchemaRepeatable) obj;
if (obj instanceof ISchemaRepeatable rso) {
boolean unbounded = rso.getMaxOccurs() == Integer.MAX_VALUE;
int maxOccurs = rso.getMaxOccurs();
int minOccurs = rso.getMinOccurs();
Expand Down Expand Up @@ -863,8 +862,7 @@ public Image getObjectImage(ExportPackageObject obj) {

public Image getObjectImage(PackageObject obj) {
int flags = 0;
if (obj instanceof ImportPackageObject) {
ImportPackageObject importPackageObject = (ImportPackageObject) obj;
if (obj instanceof ImportPackageObject importPackageObject) {
if (importPackageObject.isOptional()) {
flags |= F_OPTIONAL;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ public static void logException(Throwable e, final String title, String message)
e = ((InvocationTargetException) e).getTargetException();
}
IStatus status = null;
if (e instanceof CoreException) {
// Re-use status only if it has attached exception with the stack trace
CoreException ce = (CoreException) e;
if (e instanceof CoreException ce) {
// Re-use status only if it has attached exception with the stack
// trace
if (ce.getStatus().getException() != null) {
status = ce.getStatus();
}
Expand Down Expand Up @@ -187,9 +187,8 @@ public static void log(Throwable e) {
e = ((InvocationTargetException) e).getTargetException();
}
IStatus status = null;
if (e instanceof CoreException) {
if (e instanceof CoreException ce) {
// Re-use status only if it has attached exception with the stack trace
CoreException ce = (CoreException) e;
if (ce.getStatus().getException() != null) {
status = ce.getStatus();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ public boolean visit(PackageDeclaration packageDeclaration) {
}
List<OSGiAnnotationProcessor> processors = getPackageProcessors(packageDeclaration.getName().toString());
for (Object item : packageDeclaration.annotations()) {
if (item instanceof Annotation) {
Annotation annotation = (Annotation) item;
if (item instanceof Annotation annotation) {
IAnnotationBinding annotationBinding = annotation.resolveAnnotationBinding();
if (annotationBinding == null) {
// not resolvable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,13 @@ public boolean select(Viewer viewer, Object parentElement, Object element) {
}

protected static String getText(Object obj) {
if (obj instanceof Command) {
Command com = (Command) obj;
if (obj instanceof Command com) {
try {
return com.getName();
} catch (NotDefinedException e) {
return com.getId();
}
} else if (obj instanceof Category) {
Category cat = (Category) obj;
} else if (obj instanceof Category cat) {
try {
return cat.getName();
} catch (NotDefinedException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ public IStructureComparator locate(Object path, Object input) {

@Override
public String getContents(Object node, boolean ignoreWhitespace) {
if (node instanceof IStreamContentAccessor) {
IStreamContentAccessor sca = (IStreamContentAccessor) node;
if (node instanceof IStreamContentAccessor sca) {
try {
return readString(sca);
} catch (CoreException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ protected void createChange(BundleModel model) {
String markerHeader = marker.getAttribute("header", ICoreConstants.ECLIPSE_AUTOSTART); //$NON-NLS-1$
IBundle bundle = model.getBundle();
IManifestHeader header = bundle.getManifestHeader(markerHeader);
if (header instanceof LazyStartHeader) {
LazyStartHeader lheader = (LazyStartHeader) header;
if (header instanceof LazyStartHeader lheader) {
String exceptions = lheader.getAttribute("exceptions"); //$NON-NLS-1$
if (TargetPlatformHelper.getTargetVersion() >= 3.4) {
bundle.setHeader(Constants.BUNDLE_ACTIVATIONPOLICY, Constants.ACTIVATION_LAZY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public AddActivationPolicyResolution(int type, IMarker marker) {
@Override
protected void createChange(BundleModel model) {
IBundle bundle = model.getBundle();
if (bundle instanceof Bundle) {
Bundle bun = (Bundle) bundle;
if (bundle instanceof Bundle bun) {
IManifestHeader header = bun.getManifestHeader(Constants.BUNDLE_ACTIVATIONPOLICY);
if (header == null) {
bundle.setHeader(Constants.BUNDLE_ACTIVATIONPOLICY, Constants.ACTIVATION_LAZY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public IMarker[] findOtherMarkers(IMarker[] markers) {
@Override
protected void createChange(BundleModel model) {
IBundle bundle = model.getBundle();
if (bundle instanceof Bundle) {
Bundle bun = (Bundle) bundle;
if (bundle instanceof Bundle bun) {
IManifestHeader header = bun.getManifestHeader(ICoreConstants.AUTOMATIC_MODULE_NAME);
if (header == null) {
IManifestHeader headerName = bun.getManifestHeader(Constants.BUNDLE_SYMBOLICNAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public String getLabel() {
@Override
protected void createChange(BundleModel model) {
IBundle bundle = model.getBundle();
if (bundle instanceof Bundle) {
Bundle bun = (Bundle) bundle;
if (bundle instanceof Bundle bun) {
ExportPackageHeader header = (ExportPackageHeader) bun.getManifestHeader(Constants.EXPORT_PACKAGE);
if (header == null) {
bundle.setHeader(Constants.EXPORT_PACKAGE, ""); //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public String getLabel() {
@Override
protected void createChange(BundleModel model) {
IBundle bundle = model.getBundle();
if (bundle instanceof Bundle) {
Bundle bun = (Bundle) bundle;
if (bundle instanceof Bundle bun) {
ExportPackageHeader header = (ExportPackageHeader) bun.getManifestHeader(Constants.EXPORT_PACKAGE);
if (header == null) {
bundle.setHeader(Constants.EXPORT_PACKAGE, ""); //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ public String getLabel() {
@Override
protected void createChange(IBaseModel model) {
IEditorPart part = PDEPlugin.getActivePage().getActiveEditor();
if (part instanceof ManifestEditor) {
ManifestEditor editor = (ManifestEditor) part;
if (part instanceof ManifestEditor editor) {
IBaseModel base = editor.getAggregateModel();
if (base instanceof IBundlePluginModelBase) {
IBundlePluginModelBase pluginModel = (IBundlePluginModelBase) base;
if (base instanceof IBundlePluginModelBase pluginModel) {
NewExtensionPointWizard wizard = new NewExtensionPointWizard(pluginModel.getUnderlyingResource().getProject(), pluginModel, editor) {
@Override
public boolean performFinish() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ public String getLabel() {
@Override
protected void createChange(IBaseModel model) {
IEditorPart part = PDEPlugin.getActivePage().getActiveEditor();
if (part instanceof ManifestEditor) {
ManifestEditor editor = (ManifestEditor) part;
if (part instanceof ManifestEditor editor) {
IBaseModel base = editor.getAggregateModel();
if (base instanceof IBundlePluginModelBase) {
IBundlePluginModelBase pluginModel = (IBundlePluginModelBase) base;
if (base instanceof IBundlePluginModelBase pluginModel) {
NewExtensionWizard wizard = new NewExtensionWizard(pluginModel.getUnderlyingResource().getProject(), pluginModel, editor) {
@Override
public boolean performFinish() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public String getLabel() {
@Override
protected void createChange(BundleModel model) {
IBundle bundle = model.getBundle();
if (bundle instanceof Bundle) {
Bundle bun = (Bundle) bundle;
if (bundle instanceof Bundle bun) {
IManifestHeader header = bun.getManifestHeader(Constants.BUNDLE_SYMBOLICNAME);
if (header instanceof BundleSymbolicNameHeader) {
if (fisDirective && TargetPlatformHelper.getTargetVersion() >= 3.1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ public ChooseClassXMLResolution(int resolutionType, IMarker marker) {
@Override
protected void createChange(IPluginModelBase model) {
Object object = findNode(model);
if (!(object instanceof PluginAttribute))
if (!(object instanceof PluginAttribute attrib))
return;
PluginAttribute attrib = (PluginAttribute) object;
IDocumentElementNode element = attrib.getEnclosingElement();
String type = PDEJavaHelperUI.selectType(fResource, IJavaElementSearchConstants.CONSIDER_CLASSES_AND_INTERFACES);
if (type != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ public CreateClassXMLResolution(int resolutionType, IMarker marker) {
@Override
protected void createChange(IPluginModelBase model) {
Object object = findNode(model);
if (!(object instanceof PluginAttribute))
if (!(object instanceof PluginAttribute attr))
return;

PluginAttribute attr = (PluginAttribute) object;
String name = TextUtil.trimNonAlphaChars(attr.getValue()).replace('$', '.');
IProject project = model.getUnderlyingResource().getProject();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,8 @@ private void fixMarker(IMarker marker) {

@Override
public boolean equals(Object obj) {
if (!(obj instanceof MultiFixResolution))
if (!(obj instanceof MultiFixResolution multiFix))
return false;
MultiFixResolution multiFix = (MultiFixResolution) obj;
try {
String categoryId = (String) multiFix.fMarker.getAttribute(PDEMarkerFactory.CAT_ID);
if (categoryId == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ protected void createChange(IPluginModelBase model) {
((IPluginParent) parent).remove(pluginObject);
else if (parent instanceof PluginBaseNode)
((PluginBaseNode) parent).remove(pluginObject);
else if (pluginObject instanceof PluginAttribute) {
PluginAttribute attr = (PluginAttribute) pluginObject;
else if (pluginObject instanceof PluginAttribute attr) {
attr.getEnclosingElement().setXMLAttribute(attr.getName(), null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public RemoveUnknownExecEnvironments(int type, IMarker marker) {
@Override
protected void createChange(BundleModel model) {
IManifestHeader header = model.getBundle().getManifestHeader(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT);
if (header instanceof RequiredExecutionEnvironmentHeader) {
RequiredExecutionEnvironmentHeader reqHeader = (RequiredExecutionEnvironmentHeader) header;
if (header instanceof RequiredExecutionEnvironmentHeader reqHeader) {
ExecutionEnvironment[] bundleEnvs = reqHeader.getEnvironments();
IExecutionEnvironment[] systemEnvs = JavaRuntime.getExecutionEnvironmentsManager().getExecutionEnvironments();
for (ExecutionEnvironment bundleEnv : bundleEnvs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ public String getLabel() {
@Override
protected void createChange(BundleModel model) {
IManifestHeader header = model.getBundle().getManifestHeader(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT);
if (header instanceof RequiredExecutionEnvironmentHeader) {
RequiredExecutionEnvironmentHeader reqHeader = (RequiredExecutionEnvironmentHeader) header;
if (header instanceof RequiredExecutionEnvironmentHeader reqHeader) {
ExecutionEnvironment[] bundleEnvs = reqHeader.getEnvironments();
IExecutionEnvironment[] systemEnvs = JavaRuntime.getExecutionEnvironmentsManager()
.getExecutionEnvironments();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ public String getLabel() {
@Override
protected void createChange(BundleModel model) {
IBundle bundle = model.getBundle();
if (bundle instanceof Bundle) {
Bundle bun = (Bundle) bundle;
if (bundle instanceof Bundle bun) {
IManifestHeader header = bun.getManifestHeader(Constants.BUNDLE_SYMBOLICNAME);
if (header instanceof BundleSymbolicNameHeader) {
((BundleSymbolicNameHeader) header).fixUnsupportedDirective();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,7 @@ private Map<String, ExportPackageDescription> findValidPackagesContainingSimpleT
@Override
public void acceptSearchMatch(SearchMatch aMatch) throws CoreException {
Object element = aMatch.getElement();
if (element instanceof IType) {
IType type = (IType) element;
if (element instanceof IType type) {
// Only try to import types we can access (Bug 406232)
if (Flags.isPublic(type.getFlags())) {
if (!currentJavaProject.equals(type.getJavaProject())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,8 @@ public Change perform(IProgressMonitor pm) throws CoreException {
PDEModelUtility.modifyModel(new ModelModification(getProject()) {
@Override
protected void modifyModel(IBaseModel model, IProgressMonitor monitor) throws CoreException {
if (!(model instanceof IPluginModelBase))
if (!(model instanceof IPluginModelBase base))
return;
IPluginModelBase base = (IPluginModelBase) model;
String[] pluginIdStrings = null;
if ("JUnit 5 bundles".equals(getChangeObject())) { //$NON-NLS-1$
pluginIdStrings = getJUnit5Bundles();
Expand Down Expand Up @@ -319,16 +318,14 @@ public Change perform(IProgressMonitor pm) throws CoreException {
PDEModelUtility.modifyModel(new ModelModification(getProject()) {
@Override
protected void modifyModel(IBaseModel model, IProgressMonitor monitor) throws CoreException {
if (!(model instanceof IBundlePluginModelBase))
if (!(model instanceof IBundlePluginModelBase base))
return;
IBundlePluginModelBase base = (IBundlePluginModelBase) model;
IBundle bundle = base.getBundleModel().getBundle();
String pkgId = ((ExportPackageDescription) getChangeObject()).getName();
IManifestHeader header = bundle.getManifestHeader(Constants.IMPORT_PACKAGE);
if (header == null) {
bundle.setHeader(Constants.IMPORT_PACKAGE, pkgId);
} else if (header instanceof ImportPackageHeader) {
ImportPackageHeader ipHeader = (ImportPackageHeader) header;
} else if (header instanceof ImportPackageHeader ipHeader) {
int manifestVersion = BundlePluginBase.getBundleManifestVersion(bundle);
String versionAttr = (manifestVersion < 2) ? ICoreConstants.PACKAGE_SPECIFICATION_VERSION : Constants.VERSION_ATTRIBUTE;
ImportPackageObject impObject = new ImportPackageObject((ManifestHeader) header, (ExportPackageDescription) getChangeObject(), versionAttr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public class FeatureSelectionDialog extends FilteredItemsSelectionDialog {
private final class FeatureDetailsLabelProvider extends LabelProvider {
@Override
public String getText(Object element) {
if (element instanceof IFeatureModel) {
IFeatureModel featureModel = (IFeatureModel) element;
if (element instanceof IFeatureModel featureModel) {
if (filter.matchesFeatureId(featureModel)) {
return NLS.bind(PDEUIMessages.FeatureSelectionDialog_IdMatched, featureModel.getFeature().getId());
}
Expand Down Expand Up @@ -81,8 +80,7 @@ public boolean isConsistentItem(Object item) {

@Override
public boolean matchItem(Object item) {
if (item instanceof IFeatureModel) {
IFeatureModel model = (IFeatureModel) item;
if (item instanceof IFeatureModel model) {
if (matchesFeatureId(model))
return true;
return matchesPluginId(model) != null;
Expand Down Expand Up @@ -135,8 +133,7 @@ private int getId(Object element) {
}

private int compareSimilarObjects(Object o1, Object o2) {
if (o1 instanceof IFeatureModel && o2 instanceof IFeatureModel) {
IFeatureModel ipmb1 = (IFeatureModel) o1;
if (o1 instanceof IFeatureModel ipmb1 && o2 instanceof IFeatureModel) {
IFeatureModel ipmb2 = (IFeatureModel) o2;
return compareFeatures(ipmb1.getFeature(), ipmb2.getFeature());
}
Expand Down Expand Up @@ -215,8 +212,7 @@ protected void fillContentProvider(AbstractContentProvider contentProvider, Item

@Override
public String getElementName(Object item) {
if (item instanceof IFeatureModel) {
IFeatureModel model = (IFeatureModel) item;
if (item instanceof IFeatureModel model) {
return model.getFeature().getId();
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ public boolean isConsistentItem(Object item) {
@Override
public boolean matchItem(Object item) {
String id = null;
if (item instanceof IPluginModelBase) {
IPluginModelBase model = (IPluginModelBase) item;
if (item instanceof IPluginModelBase model) {
id = model.getPluginBase().getId();
}

Expand Down Expand Up @@ -106,8 +105,7 @@ private int getId(Object element) {
}

private int compareSimilarObjects(Object o1, Object o2) {
if (o1 instanceof IPluginModelBase && o2 instanceof IPluginModelBase) {
IPluginModelBase ipmb1 = (IPluginModelBase) o1;
if (o1 instanceof IPluginModelBase ipmb1 && o2 instanceof IPluginModelBase) {
IPluginModelBase ipmb2 = (IPluginModelBase) o2;
return comparePlugins(ipmb1.getPluginBase(), ipmb2.getPluginBase());
}
Expand Down Expand Up @@ -277,8 +275,7 @@ protected IDialogSettings getDialogSettings() {

@Override
public String getElementName(Object item) {
if (item instanceof IPluginModelBase) {
IPluginModelBase model = (IPluginModelBase) item;
if (item instanceof IPluginModelBase model) {
return model.getPluginBase().getId();
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,7 @@ public void selectionChanged(SelectionChangedEvent event) {
fOutlineSelection = true;
try {
ISelection selection = event.getSelection();
if (selection.isEmpty() == false && selection instanceof IStructuredSelection) {
IStructuredSelection ssel = (IStructuredSelection) selection;
if (selection.isEmpty() == false && selection instanceof IStructuredSelection ssel) {
Object item = ssel.getFirstElement();
selectionChanged(item);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ public JarEntryEditorInput(IStorage jarEntryFile) {
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!(obj instanceof JarEntryEditorInput))
if (!(obj instanceof JarEntryEditorInput other))
return false;
JarEntryEditorInput other = (JarEntryEditorInput) obj;
return fJarEntryFile.equals(other.fJarEntryFile);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ public ViewerComparator createDefaultOutlineComparator() {
return new ViewerComparator() {
@Override
public int compare(Viewer viewer, Object e1, Object e2) {
if ((e1 instanceof IDocumentKey) && (e2 instanceof IDocumentKey)) {
IDocumentKey key1 = (IDocumentKey) e1;
if ((e1 instanceof IDocumentKey key1) && (e2 instanceof IDocumentKey)) {
IDocumentKey key2 = (IDocumentKey) e2;
return Integer.compare(key1.getOffset(), key2.getOffset());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,9 @@ protected String[] getTypeNames() {

@Override
protected void javaToNative(Object data, TransferData transferData) {
if (!(data instanceof Object[])) {
if (!(data instanceof Object[] objects)) {
return;
}
Object[] objects = (Object[]) data;
int count = objects.length;

try (ByteArrayOutputStream out = new ByteArrayOutputStream();
Expand Down
Loading

0 comments on commit 5e9c236

Please sign in to comment.