This is an automated email from the ASF dual-hosted git repository. sseifert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-bnd-models.git
commit 0e11beb1435229fc18310abf5e4a8719952e4d2d Author: Stefan Seifert <stefanseif...@users.noreply.github.com> AuthorDate: Mon Jun 24 14:59:31 2024 +0200 SLING-12360 apply spotless code formatting --- pom.xml | 2 +- .../sling/bnd/models/ModelsScannerPlugin.java | 57 ++++++++++------------ src/test/java/dummy/example/pkg1/Model1.java | 6 +-- src/test/java/dummy/example/pkg1/Model2.java | 6 +-- src/test/java/dummy/example/pkg1/sub1/Model3.java | 6 +-- src/test/java/dummy/example/pkg2/Model4.java | 6 +-- src/test/java/dummy/example/pkg2/Model5.java | 6 +-- .../models/AbstractModelsScannerPluginTest.java | 37 +++++++------- .../bnd/models/GenerateClassesHeaderTest.java | 18 +++---- .../bnd/models/GeneratePackagesHeaderTest.java | 19 +++----- 10 files changed, 70 insertions(+), 93 deletions(-) diff --git a/pom.xml b/pom.xml index 314b7d7..9e8f1bc 100644 --- a/pom.xml +++ b/pom.xml @@ -28,8 +28,8 @@ </parent> <artifactId>org.apache.sling.bnd.models</artifactId> - <packaging>jar</packaging> <version>1.0.1-SNAPSHOT</version> + <packaging>jar</packaging> <name>Apache Sling Models bnd Plugin</name> <description>This bnd plugin detected all Sling Models classes and adds a bundle header 'Sling-Model-Classes' containing their names.</description> diff --git a/src/main/java/org/apache/sling/bnd/models/ModelsScannerPlugin.java b/src/main/java/org/apache/sling/bnd/models/ModelsScannerPlugin.java index e53047c..c6f8f8e 100644 --- a/src/main/java/org/apache/sling/bnd/models/ModelsScannerPlugin.java +++ b/src/main/java/org/apache/sling/bnd/models/ModelsScannerPlugin.java @@ -25,9 +25,6 @@ import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; -import org.apache.commons.lang3.BooleanUtils; -import org.apache.commons.lang3.StringUtils; - import aQute.bnd.osgi.Analyzer; import aQute.bnd.osgi.Clazz; import aQute.bnd.osgi.Clazz.QUERY; @@ -35,26 +32,29 @@ import aQute.bnd.osgi.Instruction; import aQute.bnd.service.AnalyzerPlugin; import aQute.bnd.service.Plugin; import aQute.service.reporter.Reporter; +import org.apache.commons.lang3.BooleanUtils; +import org.apache.commons.lang3.StringUtils; /** * Scans the classpath of the bundle for Sling Models classes. * All class names found are stored in a bundle header for processing them at runtime and reading their metadata. */ public class ModelsScannerPlugin implements AnalyzerPlugin, Plugin { - + static final String MODELS_ANNOTATION_CLASS = "org.apache.sling.models.annotations.Model"; - + static final String MODELS_PACKAGES_HEADER = "Sling-Model-Packages"; static final String MODELS_CLASSES_HEADER = "Sling-Model-Classes"; - - // max length of manifest header value 65535 bytes (see http://docs.oracle.com/javase/8/docs/technotes/guides/jar/jar.html) + + // max length of manifest header value 65535 bytes (see + // http://docs.oracle.com/javase/8/docs/technotes/guides/jar/jar.html) // fall back to packages header when class names string gets too long static final int MODELS_CLASSES_HEADER_MAXLENGTH = 60000; - + static final String PROPERTY_GENERATE_PACKAGES_HEADER = "generatePackagesHeader"; - + private Reporter reporter; - private Map<String,String> properties; + private Map<String, String> properties; @Override public void setProperties(Map<String, String> map) throws Exception { @@ -68,7 +68,7 @@ public class ModelsScannerPlugin implements AnalyzerPlugin, Plugin { @Override public boolean analyzeJar(Analyzer analyzer) throws Exception { - + // process only if no models packages or class header was set if (analyzer.get(MODELS_PACKAGES_HEADER) == null && analyzer.get(MODELS_CLASSES_HEADER) == null) { @@ -79,30 +79,27 @@ public class ModelsScannerPlugin implements AnalyzerPlugin, Plugin { if (!classNames.isEmpty()) { if (getBooleanProperty(PROPERTY_GENERATE_PACKAGES_HEADER)) { generatePackagesHeader(analyzer, classNames); - } - else { + } else { generateClassesHeader(analyzer, classNames); } } - } - + // we did not change any classes - no need to re-analyze return false; } - + private void generateClassesHeader(Analyzer analyzer, Collection<String> classNames) { String classNameHeader = StringUtils.join(classNames, ","); if (classNameHeader.length() <= MODELS_CLASSES_HEADER_MAXLENGTH) { analyzer.set(MODELS_CLASSES_HEADER, classNameHeader); - } - else { + } else { generatePackagesHeader(analyzer, classNames); } } - + private void generatePackagesHeader(Analyzer analyzer, Collection<String> classNames) { - + // get all package names SortedSet<String> packages = new TreeSet<>(); for (String className : classNames) { @@ -110,7 +107,7 @@ public class ModelsScannerPlugin implements AnalyzerPlugin, Plugin { packages.add(StringUtils.substringBeforeLast(className, ".")); } } - + // eliminate package names for which parent packages exist (they are included automatically) Set<String> packagesToRemove = new HashSet<>(); for (String packageName : packages) { @@ -119,25 +116,23 @@ public class ModelsScannerPlugin implements AnalyzerPlugin, Plugin { } } packages.removeAll(packagesToRemove); - + analyzer.set(MODELS_PACKAGES_HEADER, StringUtils.join(packages, ",")); } - + private boolean includesParentPackage(Set<String> packages, String packageName) { if (StringUtils.contains(packageName, ".")) { String parentPackageName = StringUtils.substringBeforeLast(packageName, "."); if (packages.contains(parentPackageName)) { return true; - } - else { + } else { return includesParentPackage(packages, parentPackageName); } - } - else { + } else { return false; } } - + /** * Get all classes that implement the given annotation via bnd Analyzer. * @param analyzer Analyzer @@ -154,16 +149,14 @@ public class ModelsScannerPlugin implements AnalyzerPlugin, Plugin { classNames.add(clazz.getClassName().getFQN()); } } - } - catch (Exception ex) { + } catch (Exception ex) { reporter.exception(ex, "Error querying for classes with annotation: " + annotationClassName); } return classNames; } - + private boolean getBooleanProperty(String propertyName) { String value = properties != null ? properties.get(propertyName) : null; return BooleanUtils.toBoolean(value); } - } diff --git a/src/test/java/dummy/example/pkg1/Model1.java b/src/test/java/dummy/example/pkg1/Model1.java index 4cd8f9b..ce03ab9 100644 --- a/src/test/java/dummy/example/pkg1/Model1.java +++ b/src/test/java/dummy/example/pkg1/Model1.java @@ -20,7 +20,5 @@ package dummy.example.pkg1; import org.apache.sling.models.annotations.Model; -@Model(adaptables=Object.class) -public class Model1 { - -} +@Model(adaptables = Object.class) +public class Model1 {} diff --git a/src/test/java/dummy/example/pkg1/Model2.java b/src/test/java/dummy/example/pkg1/Model2.java index 3e8ae03..097b255 100644 --- a/src/test/java/dummy/example/pkg1/Model2.java +++ b/src/test/java/dummy/example/pkg1/Model2.java @@ -20,7 +20,5 @@ package dummy.example.pkg1; import org.apache.sling.models.annotations.Model; -@Model(adaptables=Object.class) -public class Model2 { - -} +@Model(adaptables = Object.class) +public class Model2 {} diff --git a/src/test/java/dummy/example/pkg1/sub1/Model3.java b/src/test/java/dummy/example/pkg1/sub1/Model3.java index 7accb4c..be67475 100644 --- a/src/test/java/dummy/example/pkg1/sub1/Model3.java +++ b/src/test/java/dummy/example/pkg1/sub1/Model3.java @@ -20,7 +20,5 @@ package dummy.example.pkg1.sub1; import org.apache.sling.models.annotations.Model; -@Model(adaptables=Object.class) -public class Model3 { - -} +@Model(adaptables = Object.class) +public class Model3 {} diff --git a/src/test/java/dummy/example/pkg2/Model4.java b/src/test/java/dummy/example/pkg2/Model4.java index 5bdb120..6a485fd 100644 --- a/src/test/java/dummy/example/pkg2/Model4.java +++ b/src/test/java/dummy/example/pkg2/Model4.java @@ -20,7 +20,5 @@ package dummy.example.pkg2; import org.apache.sling.models.annotations.Model; -@Model(adaptables=Object.class) -public class Model4 { - -} +@Model(adaptables = Object.class) +public class Model4 {} diff --git a/src/test/java/dummy/example/pkg2/Model5.java b/src/test/java/dummy/example/pkg2/Model5.java index a160d99..d012e04 100644 --- a/src/test/java/dummy/example/pkg2/Model5.java +++ b/src/test/java/dummy/example/pkg2/Model5.java @@ -20,7 +20,5 @@ package dummy.example.pkg2; import org.apache.sling.models.annotations.Model; -@Model(adaptables=Object.class) -public class Model5 { - -} +@Model(adaptables = Object.class) +public class Model5 {} diff --git a/src/test/java/org/apache/sling/bnd/models/AbstractModelsScannerPluginTest.java b/src/test/java/org/apache/sling/bnd/models/AbstractModelsScannerPluginTest.java index 901e6b0..0095e2a 100644 --- a/src/test/java/org/apache/sling/bnd/models/AbstractModelsScannerPluginTest.java +++ b/src/test/java/org/apache/sling/bnd/models/AbstractModelsScannerPluginTest.java @@ -18,11 +18,6 @@ */ package org.apache.sling.bnd.models; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.fail; -import static org.mockito.Mockito.mock; - import java.io.File; import java.util.Arrays; import java.util.HashMap; @@ -31,32 +26,36 @@ import java.util.Map; import java.util.Set; import java.util.jar.Manifest; +import aQute.bnd.osgi.Builder; +import aQute.bnd.osgi.Jar; +import aQute.bnd.service.Plugin; +import aQute.service.reporter.Reporter; import org.apache.commons.lang3.StringUtils; import org.junit.After; import org.junit.Before; import org.junit.runner.RunWith; import org.mockito.junit.MockitoJUnitRunner; -import aQute.bnd.osgi.Builder; -import aQute.bnd.osgi.Jar; -import aQute.bnd.service.Plugin; -import aQute.service.reporter.Reporter; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; @RunWith(MockitoJUnitRunner.class) public abstract class AbstractModelsScannerPluginTest { - + protected Builder builder; @Before public final void setUp() throws Exception { builder = new Builder(); - + Jar classesDirJar = new Jar("test.jar", new File("target/test-classes")); classesDirJar.setManifest(new Manifest()); builder.setJar(classesDirJar); - - builder.setSourcepath(new File[] { new File("src/test/java") } ); - + + builder.setSourcepath(new File[] {new File("src/test/java")}); + Plugin plugin = new ModelsScannerPlugin(); plugin.setReporter(mock(Reporter.class)); plugin.setProperties(getProperties()); @@ -70,20 +69,20 @@ public abstract class AbstractModelsScannerPluginTest { } builder.close(); } - - protected Map<String,String> getProperties() { + + protected Map<String, String> getProperties() { return new HashMap<>(); } - + protected final void assertHeaderMissing(Jar jar, String headerName) throws Exception { assertNull(jar.getManifest().getMainAttributes().getValue(headerName)); } protected final void assertHeader(Jar jar, String headerName, String... headerValues) throws Exception { Set<String> expectedValues = new HashSet<>(Arrays.asList(headerValues)); - String[] actual = StringUtils.split(jar.getManifest().getMainAttributes().getValue(headerName), ","); + String[] actual = + StringUtils.split(jar.getManifest().getMainAttributes().getValue(headerName), ","); Set<String> actualValues = new HashSet<>(Arrays.asList(actual)); assertEquals(expectedValues, actualValues); } - } diff --git a/src/test/java/org/apache/sling/bnd/models/GenerateClassesHeaderTest.java b/src/test/java/org/apache/sling/bnd/models/GenerateClassesHeaderTest.java index 52076a9..bb8e664 100644 --- a/src/test/java/org/apache/sling/bnd/models/GenerateClassesHeaderTest.java +++ b/src/test/java/org/apache/sling/bnd/models/GenerateClassesHeaderTest.java @@ -18,27 +18,27 @@ */ package org.apache.sling.bnd.models; -import static org.apache.sling.bnd.models.ModelsScannerPlugin.MODELS_CLASSES_HEADER; -import static org.apache.sling.bnd.models.ModelsScannerPlugin.*; - +import aQute.bnd.osgi.Jar; import org.junit.Test; -import aQute.bnd.osgi.Jar; +import static org.apache.sling.bnd.models.ModelsScannerPlugin.*; +import static org.apache.sling.bnd.models.ModelsScannerPlugin.MODELS_CLASSES_HEADER; public class GenerateClassesHeaderTest extends AbstractModelsScannerPluginTest { @Test public void testBuild() throws Exception { Jar jar = builder.build(); - + assertHeaderMissing(jar, MODELS_PACKAGES_HEADER); - - assertHeader(jar, MODELS_CLASSES_HEADER, + + assertHeader( + jar, + MODELS_CLASSES_HEADER, "dummy.example.pkg1.Model1", "dummy.example.pkg1.Model2", "dummy.example.pkg1.sub1.Model3", "dummy.example.pkg2.Model4", - "dummy.example.pkg2.Model5"); + "dummy.example.pkg2.Model5"); } - } diff --git a/src/test/java/org/apache/sling/bnd/models/GeneratePackagesHeaderTest.java b/src/test/java/org/apache/sling/bnd/models/GeneratePackagesHeaderTest.java index 72e59d1..5e1ecbc 100644 --- a/src/test/java/org/apache/sling/bnd/models/GeneratePackagesHeaderTest.java +++ b/src/test/java/org/apache/sling/bnd/models/GeneratePackagesHeaderTest.java @@ -18,22 +18,21 @@ */ package org.apache.sling.bnd.models; -import static org.apache.sling.bnd.models.ModelsScannerPlugin.MODELS_CLASSES_HEADER; -import static org.apache.sling.bnd.models.ModelsScannerPlugin.MODELS_PACKAGES_HEADER; -import static org.apache.sling.bnd.models.ModelsScannerPlugin.PROPERTY_GENERATE_PACKAGES_HEADER; - import java.util.HashMap; import java.util.Map; +import aQute.bnd.osgi.Jar; import org.junit.Test; -import aQute.bnd.osgi.Jar; +import static org.apache.sling.bnd.models.ModelsScannerPlugin.MODELS_CLASSES_HEADER; +import static org.apache.sling.bnd.models.ModelsScannerPlugin.MODELS_PACKAGES_HEADER; +import static org.apache.sling.bnd.models.ModelsScannerPlugin.PROPERTY_GENERATE_PACKAGES_HEADER; public class GeneratePackagesHeaderTest extends AbstractModelsScannerPluginTest { @Override protected Map<String, String> getProperties() { - Map<String,String> props = new HashMap<>(); + Map<String, String> props = new HashMap<>(); props.put(PROPERTY_GENERATE_PACKAGES_HEADER, "true"); return props; } @@ -41,13 +40,9 @@ public class GeneratePackagesHeaderTest extends AbstractModelsScannerPluginTest @Test public void testBuild() throws Exception { Jar jar = builder.build(); - - assertHeader(jar, MODELS_PACKAGES_HEADER, - "dummy.example.pkg1", - "dummy.example.pkg2"); + + assertHeader(jar, MODELS_PACKAGES_HEADER, "dummy.example.pkg1", "dummy.example.pkg2"); assertHeaderMissing(jar, MODELS_CLASSES_HEADER); - } - }