Skip to content

Commit

Permalink
Code Cleanup: Java 16 instanceof pattern matching in pde.core
Browse files Browse the repository at this point in the history
  • Loading branch information
alshamams committed Aug 17, 2023
1 parent 7b86df0 commit a88a5ec
Show file tree
Hide file tree
Showing 62 changed files with 94 additions and 196 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ public NameVersionDescriptor(String id, String version, String type) {

@Override
public boolean equals(Object obj) {
if (obj instanceof NameVersionDescriptor) {
NameVersionDescriptor iud = (NameVersionDescriptor) obj;
if (obj instanceof NameVersionDescriptor iud) {
if (fId.equals(iud.fId)) {
return (fVersion != null && fVersion.equals(iud.fVersion)) || (fVersion == null && iud.fVersion == null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ public static Set<BundleDescription> findRequirementsClosure(Collection<BundleDe
BundleRevision provider = wire.getCapability().getRevision();
// Use revision of required capability to support the case if
// fragments contribute new packages to their host's API.
if (provider instanceof BundleDescription && (includeOptional || !isOptional(wire.getRequirement()))) {
BundleDescription requiredBundle = (BundleDescription) provider;
if (provider instanceof BundleDescription requiredBundle && (includeOptional || !isOptional(wire.getRequirement()))) {
addNewRequiredBundle(requiredBundle, closure, pending);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,9 @@ private synchronized IFeatureModelDelta processEvent(IModelProviderEvent e) {
if ((e.getEventTypes() & IModelProviderEvent.MODELS_REMOVED) != 0) {
IModel[] removed = e.getRemovedModels();
for (int i = 0; i < removed.length; i++) {
if (!(removed[i] instanceof IFeatureModel)) {
if (!(removed[i] instanceof IFeatureModel model)) {
continue;
}
IFeatureModel model = (IFeatureModel) removed[i];
FeatureTable.Idver idver = fActiveModels.remove(model);
if (idver != null) {
// may need to activate another model
Expand All @@ -295,10 +294,9 @@ private synchronized IFeatureModelDelta processEvent(IModelProviderEvent e) {
if ((e.getEventTypes() & IModelProviderEvent.MODELS_ADDED) != 0) {
IModel[] added = e.getAddedModels();
for (int i = 0; i < added.length; i++) {
if (!(added[i] instanceof IFeatureModel)) {
if (!(added[i] instanceof IFeatureModel model)) {
continue;
}
IFeatureModel model = (IFeatureModel) added[i];
if (model.getUnderlyingResource() != null) {
FeatureTable.Idver idver = fActiveModels.add(model);
delta.add(model, IFeatureModelDelta.ADDED);
Expand Down Expand Up @@ -339,11 +337,9 @@ private synchronized IFeatureModelDelta processEvent(IModelProviderEvent e) {
if ((e.getEventTypes() & IModelProviderEvent.MODELS_CHANGED) != 0) {
IModel[] changed = e.getChangedModels();
for (int i = 0; i < changed.length; i++) {
if (!(changed[i] instanceof IFeatureModel)) {
if (!(changed[i] instanceof IFeatureModel model)) {
continue;
}
IFeatureModel model = (IFeatureModel) changed[i];

String id = model.getFeature().getId();
String version = model.getFeature().getVersion();

Expand Down Expand Up @@ -373,10 +369,9 @@ private synchronized IFeatureModelDelta processEvent(IModelProviderEvent e) {
if ((e.getEventTypes() & IModelProviderEvent.MODELS_CHANGED) != 0) {
IModel[] changed = e.getChangedModels();
for (int i = 0; i < changed.length; i++) {
if (!(changed[i] instanceof IFeatureModel)) {
if (!(changed[i] instanceof IFeatureModel model)) {
continue;
}
IFeatureModel model = (IFeatureModel) changed[i];
if (!delta.contains(model, IFeatureModelDelta.ADDED | IFeatureModelDelta.REMOVED)) {
delta.add(model, IFeatureModelDelta.CHANGED);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ private boolean isInterestingDelta(IJavaElementDelta delta) {
return true;
}

if (kind == IJavaElementDelta.CHANGED && element instanceof IPackageFragmentRoot) {
IPackageFragmentRoot root = (IPackageFragmentRoot) element;
if (kind == IJavaElementDelta.CHANGED && element instanceof IPackageFragmentRoot root) {
return root.isArchive();
}
return false;
Expand All @@ -111,8 +110,7 @@ private boolean isInterestingDelta(IJavaElementDelta delta) {
private boolean ignoreDelta(IJavaElementDelta delta) {
try {
IJavaElement element = delta.getElement();
if (element instanceof IPackageFragmentRoot) {
IPackageFragmentRoot root = (IPackageFragmentRoot) element;
if (element instanceof IPackageFragmentRoot root) {
IClasspathEntry entry = root.getRawClasspathEntry();
if (entry != null && entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,9 @@ public IExtension[] findExtensions(String extensionPointId, boolean activeOnly)

// make sure we return the right IPluginModelBase when we have multiple versions of a plug-in Id
private IPluginModelBase getPlugin(IContributor icontributor, boolean searchAll) {
if (!(icontributor instanceof RegistryContributor)) {
if (!(icontributor instanceof RegistryContributor contributor)) {
return null;
}
RegistryContributor contributor = (RegistryContributor) icontributor;
long bundleId = Long.parseLong(contributor.getActualId());
BundleDescription desc = PDECore.getDefault().getModelManager().getState().getState().getBundle(Long.parseLong(contributor.getActualId()));
if (desc != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ private boolean handleDelta(IJavaElementDelta delta) {
}
return true;
}
if (element instanceof IJavaProject) {
IJavaProject project = (IJavaProject) element;
if (element instanceof IJavaProject project) {
if (project.getElementName().equals(PROXY_PROJECT_NAME)) {
if (delta.getKind() == IJavaElementDelta.REMOVED) {
synchronized (fPluginIdSet) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ public ModelChange(IModel model, int type) {

@Override
public boolean equals(Object obj) {
if (obj instanceof ModelChange) {
ModelChange change = (ModelChange) obj;
if (obj instanceof ModelChange change) {
IProject project = change.model.getUnderlyingResource().getProject();
int type = change.type;
return model.getUnderlyingResource().getProject().equals(project) && this.type == type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ private void addHeaderValue(Annotation annotation) {

@Override
public void apply(IBaseModel model) {
if (model instanceof IBundlePluginModelBase) {
IBundlePluginModelBase pluginModel = (IBundlePluginModelBase) model;
if (model instanceof IBundlePluginModelBase pluginModel) {
IBundleModel bundleModel = pluginModel.getBundleModel();
IBundle bundle = bundleModel.getBundle();
for (Entry<String, String> entry : headers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,15 @@ public void apply(IBaseModel model) {
}

private static ExportPackageObject getExportPackage(IBaseModel model, String packageName) {
if (model instanceof IBundlePluginModelBase) {
IBundlePluginModelBase pluginModel = (IBundlePluginModelBase) model;
if (model instanceof IBundlePluginModelBase pluginModel) {
IBundleModel bundleModel = pluginModel.getBundleModel();
IBundle bundle = bundleModel.getBundle();
IManifestHeader header = bundle.getManifestHeader(Constants.EXPORT_PACKAGE);
if (header == null) {
bundle.setHeader(Constants.EXPORT_PACKAGE, packageName);
header = bundle.getManifestHeader(Constants.EXPORT_PACKAGE);
}
if (header instanceof ExportPackageHeader) {
ExportPackageHeader exportPackageHeader = (ExportPackageHeader) header;
if (header instanceof ExportPackageHeader exportPackageHeader) {
ExportPackageObject packageObject = exportPackageHeader.getPackage(packageName);
if (packageObject == null) {
return exportPackageHeader.addPackage(packageName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ static Optional<String> stringValue(Expression expression) {
*/
static Stream<Expression> expressions(Expression expression) {
Expression unwrap = value(expression).orElse(expression);
if (unwrap instanceof ArrayInitializer) {
ArrayInitializer arrayInitializer = (ArrayInitializer) unwrap;
if (unwrap instanceof ArrayInitializer arrayInitializer) {
return arrayInitializer.expressions().stream().filter(Expression.class::isInstance)
.map(Expression.class::cast);
}
Expand Down Expand Up @@ -98,20 +97,17 @@ static Optional<Expression> value(Expression annotation) {
* optional if no such member exits.
*/
static Optional<Expression> member(Expression annotation, String memberName) {
if (annotation instanceof NormalAnnotation) {
NormalAnnotation normalAnnotation = (NormalAnnotation) annotation;
if (annotation instanceof NormalAnnotation normalAnnotation) {
for (Object value : normalAnnotation.values()) {
if (value instanceof MemberValuePair) {
MemberValuePair pair = (MemberValuePair) value;
if (value instanceof MemberValuePair pair) {
SimpleName name = pair.getName();
if (name != null && name.toString().equals(memberName)) {
return Optional.ofNullable(pair.getValue());
}
}
}
}
if (annotation instanceof SingleMemberAnnotation && "value".equals(memberName)) { //$NON-NLS-1$
SingleMemberAnnotation singleMemberAnnotation = (SingleMemberAnnotation) annotation;
if (annotation instanceof SingleMemberAnnotation singleMemberAnnotation && "value".equals(memberName)) { //$NON-NLS-1$
return Optional.ofNullable(singleMemberAnnotation.getValue());
}
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@ private static boolean hasRelevantDelta(IResourceDelta delta) throws CoreExcepti
@Override
public boolean visit(IResourceDelta delta) throws CoreException {
IResource resource = delta.getResource();
if (resource instanceof IFile) {
IFile file = (IFile) resource;
if (resource instanceof IFile file) {
String name = file.getName();
if (name.endsWith(CLASS_EXTENSION) || file.getName().equals(BndProject.INSTRUCTIONS_FILE)
|| name.equals(ICoreConstants.MANIFEST_FILENAME)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ public static List<IClasspathEntry> getClasspathEntries(Project project, IWorksp
@SuppressWarnings("deprecation")
IContainer[] containers = root.findContainersForLocation(IPath.fromOSString(base.getAbsolutePath()));
for (IContainer container : containers) {
if (container instanceof IProject) {
IProject p = (IProject) container;
if (container instanceof IProject p) {
entries.add(JavaCore.newProjectEntry(p.getFullPath()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public void resourceChanged(IResourceChangeEvent event) {
@Override
public boolean visit(IResourceDelta delta) throws CoreException {
IResource resource = delta.getResource();
if (resource instanceof IFile) {
IFile file = (IFile) resource;
if (resource instanceof IFile file) {
if (BndProject.INSTRUCTIONS_FILE.equals(file.getName())
&& BndProject.isBndProject(file.getProject())) {
updateProjects.add(file.getProject());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,9 @@ static class BuildProblem {

@Override
public boolean equals(Object obj) {
if (!(obj instanceof BuildProblem)) {
if (!(obj instanceof BuildProblem bp)) {
return false;
}
BuildProblem bp = (BuildProblem) obj;
if (!fEntryName.equals(bp.fEntryName)) {
return false;
}
Expand Down Expand Up @@ -1235,10 +1234,9 @@ private void reportErrors(BuildModel bm) {
}

private int getLineNumber(IBuildEntry ibe, String tokenString) {
if (!(ibe instanceof BuildEntry)) {
if (!(ibe instanceof BuildEntry be)) {
return 0;
}
BuildEntry be = (BuildEntry) ibe;
IDocument doc = ((BuildModel) be.getModel()).getDocument();
try {
int buildEntryLineNumber = doc.getLineOfOffset(be.getOffset()) + 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,7 @@ private void validateExportPackages() {
IHeader header = getHeader(Constants.EXPORT_PACKAGE);

// check for missing exported packages
if (fModel instanceof IBundlePluginModelBase) {
IBundlePluginModelBase bundleModel = (IBundlePluginModelBase) fModel;
if (fModel instanceof IBundlePluginModelBase bundleModel) {
IBundle bundle = bundleModel.getBundleModel().getBundle();
IManifestHeader bundleClasspathheader = bundle.getManifestHeader(Constants.BUNDLE_CLASSPATH);

Expand All @@ -251,8 +250,7 @@ private void validateExportPackages() {
if (ManifestUtils.isImmediateRoot(root)) {
IJavaElement[] javaElements = root.getChildren();
for (int j = 0; j < javaElements.length; j++) {
if (javaElements[j] instanceof IPackageFragment) {
IPackageFragment fragment = (IPackageFragment) javaElements[j];
if (javaElements[j] instanceof IPackageFragment fragment) {
String name = fragment.getElementName();
if (name.length() == 0) {
continue;
Expand Down Expand Up @@ -1728,12 +1726,11 @@ private void validateBundleLocalization() {
}

IResource res = PDEProject.getBundleRoot(fProject).findMember(location);
if (res == null || !(res instanceof IContainer)) {
if (res == null || !(res instanceof IContainer folder)) {
VirtualMarker marker = report(PDECoreMessages.BundleErrorReporter_localization_folder_not_exist, header.getLineNumber(), CompilerFlags.getFlag(fProject, CompilerFlags.P_UNKNOWN_RESOURCE), PDEMarkerFactory.CAT_OTHER);
addMarkerAttribute(marker, PDEMarkerFactory.compilerKey, CompilerFlags.P_UNKNOWN_RESOURCE);
return;
}
IContainer folder = (IContainer) res;
try {
IResource[] children = folder.members();
for (int i = 0; i < children.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,24 +281,20 @@ private static void updateChoiceElementChildren(ISchemaCompositor compositor, Ha
}

private static void processObjectMax(ISchemaObject schemaObject, HashSet<ElementOccurrenceResult> elementSet, HashMap<String, Integer> siblings, int multiplicityTracker, Element element) {
if (schemaObject instanceof ISchemaElement) {
ISchemaElement schemaElement = (ISchemaElement) schemaObject;
if (schemaObject instanceof ISchemaElement schemaElement) {
Element childElement = findChildElement(element, schemaElement.getName());
if (childElement != null) {
processElementMax(schemaElement, elementSet, siblings, multiplicityTracker, childElement);
}
} else if (schemaObject instanceof ISchemaCompositor) {
ISchemaCompositor sCompositor = (ISchemaCompositor) schemaObject;
} else if (schemaObject instanceof ISchemaCompositor sCompositor) {
processCompositorMax(sCompositor, elementSet, siblings, multiplicityTracker, element);
}
}

private static void processObjectMin(ISchemaObject schemaObject, HashSet<ElementOccurrenceResult> elementSet, HashMap<String, Integer> siblings, int multiplicityTracker) {
if (schemaObject instanceof ISchemaElement) {
ISchemaElement schemaElement = (ISchemaElement) schemaObject;
if (schemaObject instanceof ISchemaElement schemaElement) {
processElementMin(schemaElement, elementSet, siblings, multiplicityTracker);
} else if (schemaObject instanceof ISchemaCompositor) {
ISchemaCompositor sCompositor = (ISchemaCompositor) schemaObject;
} else if (schemaObject instanceof ISchemaCompositor sCompositor) {
processCompositorMin(sCompositor, elementSet, siblings, multiplicityTracker);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ public boolean visit(IResourceDelta delta) {
return true;
}

if (resource instanceof IFile) {
// see if this is it
IFile candidate = (IFile) resource;
if (resource instanceof IFile candidate) {
if (isSchemaFile(candidate)) {
// That's it, but only check it if it has been added or changed
if (delta.getKind() != IResourceDelta.REMOVED) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,7 @@ private void validateInternalExtensionAttribute(Element element, ISchemaElement
return;
}

if (schemaElement instanceof ISchemaRootElement) {
ISchemaRootElement rootElement = (ISchemaRootElement) schemaElement;
if (schemaElement instanceof ISchemaRootElement rootElement) {
String epid = schemaElement.getSchema().getPluginId();
if (fModel == null || fModel.getPluginBase() == null) {
return;
Expand Down Expand Up @@ -331,8 +330,7 @@ private void validateMaxElementMult(Element element, ISchemaElement schemaElemen
}

private void computeAllowedElements(ISchemaType type, HashSet<String> elementSet) {
if (type instanceof ISchemaComplexType) {
ISchemaComplexType complexType = (ISchemaComplexType) type;
if (type instanceof ISchemaComplexType complexType) {
ISchemaCompositor compositor = complexType.getCompositor();
if (compositor != null) {
computeAllowedElements(compositor, elementSet);
Expand All @@ -350,8 +348,7 @@ private void computeAllowedElements(ISchemaType type, HashSet<String> elementSet
private void computeAllowedElements(ISchemaCompositor compositor, HashSet<String> elementSet) {
ISchemaObject[] children = compositor.getChildren();
for (ISchemaObject child : children) {
if (child instanceof ISchemaObjectReference) {
ISchemaObjectReference ref = (ISchemaObjectReference) child;
if (child instanceof ISchemaObjectReference ref) {
ISchemaElement refElement = (ISchemaElement) ref.getReferencedObject();
if (refElement != null) {
elementSet.add(refElement.getName());
Expand Down Expand Up @@ -664,8 +661,7 @@ protected void validateRestrictionAttribute(Element element, Attr attr, ISchemaR
Object[] children = restriction.getChildren();
String value = attr.getValue();
for (Object child : children) {
if (child instanceof ISchemaEnumeration) {
ISchemaEnumeration enumeration = (ISchemaEnumeration) child;
if (child instanceof ISchemaEnumeration enumeration) {
if (enumeration.getName().equals(value)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,15 @@ public DeltaVisitor(IProgressMonitor monitor) {
public boolean visit(IResourceDelta delta) {
IResource resource = delta.getResource();

if (resource instanceof IProject) {
// Only check projects with feature nature
IProject project = (IProject) resource;
if (resource instanceof IProject project) {
try {
return (project.hasNature(PDE.FEATURE_NATURE));
} catch (CoreException e) {
PDECore.logException(e);
return false;
}
}
if (resource instanceof IFile) {
// see if this is it
IFile candidate = (IFile) resource;
if (resource instanceof IFile candidate) {
if (isManifestFile(candidate)) {
// That's it, but only check it if it has been added or changed
if (delta.getKind() != IResourceDelta.REMOVED) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,7 @@ private void validateUnpack(Element parent) {
return;
}

if (pModel instanceof IBundlePluginModel) {
IBundlePluginModel bModel = (IBundlePluginModel) pModel;
if (pModel instanceof IBundlePluginModel bModel) {
IManifestHeader header = bModel.getBundleModel().getBundle().getManifestHeader(ICoreConstants.ECLIPSE_BUNDLE_SHAPE);
if (header != null) {
String value = header.getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ public void validate(IProgressMonitor monitor) {
NodeList children = element.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
if (child instanceof Element) {
Element childElement = (Element) child;
if (child instanceof Element childElement) {
String name = childElement.getNodeName();
if (name != null && name.equals(ELEMENT)) {
String value = childElement.getAttribute(ATTR_NAME);
Expand Down
Loading

0 comments on commit a88a5ec

Please sign in to comment.