From a0f3685a20bfc732ea9fb1d9407d9944ec6f2125 Mon Sep 17 00:00:00 2001 From: BJ Hargrave Date: Wed, 20 Jul 2016 13:16:02 -0400 Subject: [PATCH] Use org.osgi.util.function.Predicate Also prepare for org.osgi.util.function 1.1 which adds throws Exception to method signatures. Signed-off-by: BJ Hargrave --- .../org/bndtools/builder/BuildListeners.java | 23 +++++++++++-------- .../builder/ClasspathContainerFilter.java | 8 +++---- .../baseline/BaselineErrorHandler.java | 6 ++--- .../MemberValuePairLocationRetriever.java | 16 +++++++++---- .../src/bndtools/utils/SelectionUtils.java | 10 ++++---- .../src/org/bndtools/utils/Predicate.java | 15 ------------ 6 files changed, 37 insertions(+), 41 deletions(-) delete mode 100644 bndtools.utils/src/org/bndtools/utils/Predicate.java diff --git a/bndtools.builder/src/org/bndtools/builder/BuildListeners.java b/bndtools.builder/src/org/bndtools/builder/BuildListeners.java index b479f7865..1fea2ac10 100644 --- a/bndtools.builder/src/org/bndtools/builder/BuildListeners.java +++ b/bndtools.builder/src/org/bndtools/builder/BuildListeners.java @@ -78,9 +78,9 @@ public void removedService(ServiceReference reference, BuildListe public void fireBuildStarting(final IProject project) { this.project = project; - forEachListener(new Function() { + forEachListener(new Function() { @Override - public Object apply(BuildListener listener) { + public Void apply(BuildListener listener) { listener.buildStarting(project); return null; } @@ -90,9 +90,9 @@ public Object apply(BuildListener listener) { public void fireBuiltBundles(final IProject project, final IPath[] paths) { this.project = project; this.paths = paths; - forEachListener(new Function() { + forEachListener(new Function() { @Override - public Object apply(BuildListener listener) { + public Void apply(BuildListener listener) { listener.builtBundles(project, paths); return null; } @@ -100,19 +100,24 @@ public Object apply(BuildListener listener) { } public void fireReleased(final IProject project) { - forEachListener(new Function() { + forEachListener(new Function() { @Override - public Object apply(BuildListener listener) { + public Void apply(BuildListener listener) { listener.released(project); return null; } }); } - private void forEachListener(Function function) { + private void forEachListener(Function function) { synchronized (listeners) { - for (BuildListener listener : listeners) - function.apply(listener); + for (BuildListener listener : listeners) { + try { + function.apply(listener); + } catch (Exception e) { + logger.logError("BuildListener error", e); + } + } } } diff --git a/bndtools.builder/src/org/bndtools/builder/ClasspathContainerFilter.java b/bndtools.builder/src/org/bndtools/builder/ClasspathContainerFilter.java index 219793bfb..93a2c2c3d 100644 --- a/bndtools.builder/src/org/bndtools/builder/ClasspathContainerFilter.java +++ b/bndtools.builder/src/org/bndtools/builder/ClasspathContainerFilter.java @@ -1,19 +1,19 @@ package org.bndtools.builder; import org.bndtools.api.BndtoolsConstants; -import org.bndtools.utils.Predicate; import org.eclipse.jdt.core.IClasspathContainer; import org.eclipse.jdt.launching.JavaRuntime; - +import org.osgi.util.function.Predicate; /** * Filter for {@link IClasspathContainer} instances that removes the Bnd and JRE containers - * + * * @author Neil Bartlett */ class ClasspathContainerFilter implements Predicate { - public boolean select(IClasspathContainer container) { + @Override + public boolean test(IClasspathContainer container) { boolean result = true; if (BndtoolsConstants.BND_CLASSPATH_ID.equals(container.getPath())) { result = false; diff --git a/bndtools.builder/src/org/bndtools/builder/handlers/baseline/BaselineErrorHandler.java b/bndtools.builder/src/org/bndtools/builder/handlers/baseline/BaselineErrorHandler.java index 5e849f07b..57c898ab0 100644 --- a/bndtools.builder/src/org/bndtools/builder/handlers/baseline/BaselineErrorHandler.java +++ b/bndtools.builder/src/org/bndtools/builder/handlers/baseline/BaselineErrorHandler.java @@ -43,7 +43,7 @@ import org.eclipse.jface.text.contentassist.CompletionProposal; import org.eclipse.jface.text.contentassist.ICompletionProposal; import org.eclipse.ui.IMarkerResolution; -import org.osgi.util.function.Function; +import org.osgi.util.function.Predicate; import aQute.bnd.build.Project; import aQute.bnd.differ.Baseline.Info; @@ -148,9 +148,9 @@ ISourceRange findPackageInfoJavaVersionLocation(String packageName, ICompilation parser.setResolveBindings(true); CompilationUnit ast = (CompilationUnit) parser.createAST(null); - MemberValuePairLocationRetriever mvpRetriever = new MemberValuePairLocationRetriever(annot, new Function() { + MemberValuePairLocationRetriever mvpRetriever = new MemberValuePairLocationRetriever(annot, new Predicate() { @Override - public Boolean apply(String t) { + public boolean test(String t) { return ANNOTATION_VERSION_BND.equals(t) || ANNOTATION_VERSION_OSGI.equals(t); } }, "value"); diff --git a/bndtools.builder/src/org/bndtools/builder/utils/MemberValuePairLocationRetriever.java b/bndtools.builder/src/org/bndtools/builder/utils/MemberValuePairLocationRetriever.java index a6b5bcdb8..d2c00eadf 100644 --- a/bndtools.builder/src/org/bndtools/builder/utils/MemberValuePairLocationRetriever.java +++ b/bndtools.builder/src/org/bndtools/builder/utils/MemberValuePairLocationRetriever.java @@ -13,7 +13,7 @@ import org.eclipse.jdt.core.dom.SingleMemberAnnotation; import org.eclipse.jdt.core.dom.TypeDeclaration; import org.eclipse.jdt.core.dom.VariableDeclarationFragment; -import org.osgi.util.function.Function; +import org.osgi.util.function.Predicate; /** * Visitor that will "visit" an ASTNode and its children until it finds the expected MemberValue pair to retain its @@ -22,7 +22,7 @@ public class MemberValuePairLocationRetriever extends ASTVisitor { private final IAnnotation javaAnnotation; - private final Function annotationNameMatch; + private final Predicate annotationNameMatch; private final String memberName; private ISourceRange locatedSourceRange = null; @@ -30,7 +30,7 @@ public class MemberValuePairLocationRetriever extends ASTVisitor { /** * Constructor */ - public MemberValuePairLocationRetriever(final IAnnotation javaAnnotation, final Function annotationNameMatch, final String memberName) { + public MemberValuePairLocationRetriever(final IAnnotation javaAnnotation, final Predicate annotationNameMatch, final String memberName) { this.javaAnnotation = javaAnnotation; this.annotationNameMatch = annotationNameMatch; this.memberName = memberName; @@ -104,7 +104,13 @@ public boolean visit(SingleMemberAnnotation node) { final IAnnotationBinding annotationBinding = node.resolveAnnotationBinding(); if (annotationBinding != null) { final String nodeName = annotationBinding.getAnnotationType().getQualifiedName(); - if (this.annotationNameMatch.apply(nodeName)) { + boolean match; + try { + match = this.annotationNameMatch.test(nodeName); + } catch (Exception e) { + match = false; + } + if (match) { this.locatedSourceRange = new SourceRange(node.getValue().getStartPosition(), node.getValue().getLength()); } } @@ -137,4 +143,4 @@ public boolean visit(MemberValuePair node) { return false; } -} \ No newline at end of file +} diff --git a/bndtools.core/src/bndtools/utils/SelectionUtils.java b/bndtools.core/src/bndtools/utils/SelectionUtils.java index c187565dd..a1fff7eb9 100644 --- a/bndtools.core/src/bndtools/utils/SelectionUtils.java +++ b/bndtools.core/src/bndtools/utils/SelectionUtils.java @@ -16,14 +16,14 @@ import java.util.Iterator; import java.util.List; -import org.bndtools.utils.Predicate; import org.eclipse.core.runtime.IAdaptable; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; +import org.osgi.util.function.Predicate; public class SelectionUtils { - public static Collection getSelectionMembers(ISelection selection, Class clazz) { + public static Collection getSelectionMembers(ISelection selection, Class clazz) throws Exception { return getSelectionMembers(selection, clazz, null); } @@ -43,7 +43,7 @@ public static T adaptObject(Object obj, Class clazz) { return null; } - public static Collection getSelectionMembers(ISelection selection, Class clazz, Predicate< ? super T> filter) { + public static Collection getSelectionMembers(ISelection selection, Class clazz, Predicate< ? super T> filter) throws Exception { if (selection.isEmpty() || !(selection instanceof IStructuredSelection)) { return Collections.emptyList(); } @@ -56,14 +56,14 @@ public static Collection getSelectionMembers(ISelection selection, Class< if (clazz.isInstance(element)) { @SuppressWarnings("unchecked") T casted = (T) element; - if (filter == null || filter.select(casted)) { + if (filter == null || filter.test(casted)) { result.add(casted); } } else if (element instanceof IAdaptable) { @SuppressWarnings("unchecked") T adapted = (T) ((IAdaptable) element).getAdapter(clazz); if (adapted != null) { - if (filter == null || filter.select(adapted)) { + if (filter == null || filter.test(adapted)) { result.add(adapted); } } diff --git a/bndtools.utils/src/org/bndtools/utils/Predicate.java b/bndtools.utils/src/org/bndtools/utils/Predicate.java deleted file mode 100644 index 1894d89e8..000000000 --- a/bndtools.utils/src/org/bndtools/utils/Predicate.java +++ /dev/null @@ -1,15 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010 Neil Bartlett. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Neil Bartlett - initial API and implementation - ******************************************************************************/ -package org.bndtools.utils; - -public interface Predicate { - boolean select(T item); -}