You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cz...@apache.org on 2018/09/16 16:25:16 UTC
svn commit: r1841024 - in /felix/trunk/tools/osgicheck-maven-plugin:
README.md pom.xml
src/main/java/org/apache/felix/maven/osgicheck/impl/CheckMojo.java
src/main/java/org/apache/felix/maven/osgicheck/impl/checks/ConsumerProviderTypeCheck.java
Author: cziegeler
Date: Sun Sep 16 16:25:16 2018
New Revision: 1841024
URL: http://svn.apache.org/viewvc?rev=1841024&view=rev
Log:
FELIX-5925 : Add check for packages with versions but without explicit provider/consumer type information
Added:
felix/trunk/tools/osgicheck-maven-plugin/src/main/java/org/apache/felix/maven/osgicheck/impl/checks/ConsumerProviderTypeCheck.java (with props)
Modified:
felix/trunk/tools/osgicheck-maven-plugin/README.md
felix/trunk/tools/osgicheck-maven-plugin/pom.xml
felix/trunk/tools/osgicheck-maven-plugin/src/main/java/org/apache/felix/maven/osgicheck/impl/CheckMojo.java
Modified: felix/trunk/tools/osgicheck-maven-plugin/README.md
URL: http://svn.apache.org/viewvc/felix/trunk/tools/osgicheck-maven-plugin/README.md?rev=1841024&r1=1841023&r2=1841024&view=diff
==============================================================================
--- felix/trunk/tools/osgicheck-maven-plugin/README.md (original)
+++ felix/trunk/tools/osgicheck-maven-plugin/README.md Sun Sep 16 16:25:16 2018
@@ -1,2 +1,20 @@
# osgicheck-maven-plugin
Maven Plugin checking several OSGi aspects of your project
+
+Add this plugin to your plugins section:
+
+```xml
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>osgicheck-maven-plugin</artifactId>
+ <version>0.0.1-SNAPSHOT</version>
+ <executions>
+ <execution>
+ <id>check-bundle</id>
+ <goals>
+ <goal>check</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+```
Modified: felix/trunk/tools/osgicheck-maven-plugin/pom.xml
URL: http://svn.apache.org/viewvc/felix/trunk/tools/osgicheck-maven-plugin/pom.xml?rev=1841024&r1=1841023&r2=1841024&view=diff
==============================================================================
--- felix/trunk/tools/osgicheck-maven-plugin/pom.xml (original)
+++ felix/trunk/tools/osgicheck-maven-plugin/pom.xml Sun Sep 16 16:25:16 2018
@@ -33,7 +33,7 @@
<properties>
<maven.version>3.5.0</maven.version>
- <felix.java.version>7</felix.java.version>
+ <felix.java.version>8</felix.java.version>
</properties>
<prerequisites>
@@ -82,6 +82,11 @@
<dependencies>
<dependency>
+ <groupId>org.ow2.asm</groupId>
+ <artifactId>asm-all</artifactId>
+ <version>5.2</version>
+ </dependency>
+ <dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>${maven.version}</version>
Modified: felix/trunk/tools/osgicheck-maven-plugin/src/main/java/org/apache/felix/maven/osgicheck/impl/CheckMojo.java
URL: http://svn.apache.org/viewvc/felix/trunk/tools/osgicheck-maven-plugin/src/main/java/org/apache/felix/maven/osgicheck/impl/CheckMojo.java?rev=1841024&r1=1841023&r2=1841024&view=diff
==============================================================================
--- felix/trunk/tools/osgicheck-maven-plugin/src/main/java/org/apache/felix/maven/osgicheck/impl/CheckMojo.java (original)
+++ felix/trunk/tools/osgicheck-maven-plugin/src/main/java/org/apache/felix/maven/osgicheck/impl/CheckMojo.java Sun Sep 16 16:25:16 2018
@@ -28,6 +28,7 @@ import java.util.Map;
import java.util.jar.Manifest;
import org.apache.commons.io.FileUtils;
+import org.apache.felix.maven.osgicheck.impl.checks.ConsumerProviderTypeCheck;
import org.apache.felix.maven.osgicheck.impl.checks.ImportExportCheck;
import org.apache.felix.maven.osgicheck.impl.checks.SCRCheck;
import org.apache.felix.maven.osgicheck.impl.featureutil.ManifestUtil;
@@ -147,7 +148,7 @@ public class CheckMojo extends AbstractM
final File dir = rootDir;
- final Class<?>[] checks = new Class<?>[] {ImportExportCheck.class, SCRCheck.class};
+ final Class<?>[] checks = new Class<?>[] {ImportExportCheck.class, SCRCheck.class, ConsumerProviderTypeCheck.class};
for(final Class<?> c : checks) {
final Check check = (Check)c.newInstance();
check.check(new CheckContext() {
Added: felix/trunk/tools/osgicheck-maven-plugin/src/main/java/org/apache/felix/maven/osgicheck/impl/checks/ConsumerProviderTypeCheck.java
URL: http://svn.apache.org/viewvc/felix/trunk/tools/osgicheck-maven-plugin/src/main/java/org/apache/felix/maven/osgicheck/impl/checks/ConsumerProviderTypeCheck.java?rev=1841024&view=auto
==============================================================================
--- felix/trunk/tools/osgicheck-maven-plugin/src/main/java/org/apache/felix/maven/osgicheck/impl/checks/ConsumerProviderTypeCheck.java (added)
+++ felix/trunk/tools/osgicheck-maven-plugin/src/main/java/org/apache/felix/maven/osgicheck/impl/checks/ConsumerProviderTypeCheck.java Sun Sep 16 16:25:16 2018
@@ -0,0 +1,148 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package org.apache.felix.maven.osgicheck.impl.checks;
+
+import static org.objectweb.asm.ClassReader.SKIP_CODE;
+import static org.objectweb.asm.ClassReader.SKIP_DEBUG;
+import static org.objectweb.asm.ClassReader.SKIP_FRAMES;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.felix.maven.osgicheck.impl.Check;
+import org.apache.felix.maven.osgicheck.impl.CheckContext;
+import org.apache.felix.maven.osgicheck.impl.featureutil.ManifestUtil;
+import org.apache.felix.maven.osgicheck.impl.featureutil.PackageInfo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.objectweb.asm.ClassReader;
+import org.objectweb.asm.tree.AnnotationNode;
+import org.objectweb.asm.tree.ClassNode;
+
+/**
+ * The following checks are performed:
+ * <ul>
+ * <li>If a package is exported, the interfaces must be marked with either ConsumerType or ProviderType
+ * </ul>
+ */
+public class ConsumerProviderTypeCheck implements Check {
+
+ @Override
+ public String getName() {
+ return "typecheck";
+ }
+
+ private boolean include(final File f) {
+ return f.isFile() && f.getName().endsWith(".class");
+ }
+
+ private boolean exclude(final File f) {
+ return f.getName().equals("package-info.class");
+ }
+
+ @Override
+ public void check(final CheckContext ctx) throws MojoExecutionException {
+ ctx.getLog().info("Checking exported packages for provider/consumer type");
+ final List<PackageInfo> exp = ManifestUtil.extractExportedPackages(ctx.getManifest());
+ for(final PackageInfo p : exp) {
+ final File packageDir = new File(ctx.getRootDir(), p.getName().replace(".", File.separator));
+ if ( !packageDir.exists() ) {
+ ctx.reportError("Package directory " + packageDir + " does not exist!");
+ } else {
+ ctx.getLog().info("Examining package " + p.getName() + "...");
+ for(final File f : packageDir.listFiles()) {
+ if ( include(f) && !exclude(f) ) {
+ final String className = f.getName().substring(0, f.getName().length() - 6);
+ processClass(ctx, f, className);
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * Scan a single class.
+ */
+ private void processClass(final CheckContext ctx, final File classFile, final String className) {
+ ctx.getLog().debug("Processing class " + className);
+ try (final InputStream input = new FileInputStream(classFile)) {
+ // get the class file for ASM
+ final ClassReader classReader = new ClassReader(input);
+ final ClassNode classNode = new ClassNode();
+
+ classReader.accept(classNode, SKIP_CODE | SKIP_DEBUG | SKIP_FRAMES);
+
+ // create descriptions
+ final List<String> annotations = extractAnnotation(classNode);
+ for(final String s : annotations) {
+ ctx.getLog().debug("Found annotation " + s );
+ }
+
+ int count = 0;
+ if ( annotations.contains("org.osgi.annotation.versioning.ProviderType") ) {
+ count++;
+ }
+ if ( annotations.contains("org.osgi.annotation.versioning.ConsumerType") ) {
+ count++;
+ }
+ if ( count == 0 ) {
+ ctx.reportError("Class " + className + " does neither declare ConsumerType nor ProducerType");
+ } else if ( count == 2 ) {
+ ctx.reportError("Class " + className + " declares ConsumerType and ProducerType");
+ }
+ } catch (final IllegalArgumentException | IOException ioe) {
+ ctx.reportError("Unable to scan annotations " + ioe.getMessage());
+ }
+ }
+
+ /**
+ * Extract annotations
+ */
+ private final List<String> extractAnnotation(final ClassNode classNode) {
+ final List<String> descriptions = new ArrayList<>();
+ for(final AnnotationNode n : getAllAnnotations(classNode.invisibleAnnotations)) {
+ this.parseAnnotation(descriptions, n);
+ }
+ for(final AnnotationNode n : getAllAnnotations(classNode.visibleAnnotations)) {
+ this.parseAnnotation(descriptions, n);
+ }
+
+ return descriptions;
+ }
+
+ @SuppressWarnings("unchecked")
+ private List<AnnotationNode> getAllAnnotations(@SuppressWarnings("rawtypes") final List annotationList) {
+ final List<AnnotationNode> resultList = new ArrayList<>();
+ if ( annotationList != null ) {
+ resultList.addAll(annotationList);
+ }
+ return resultList;
+ }
+
+ /**
+ * Parse annotation
+ */
+ private void parseAnnotation(final List<String> descriptions, final AnnotationNode annotation) {
+ // desc has the format 'L' + className.replace('.', '/') + ';'
+ final String name = annotation.desc.substring(1, annotation.desc.length() - 1).replace('/', '.');
+
+ descriptions.add(name);
+ }
+}
Propchange: felix/trunk/tools/osgicheck-maven-plugin/src/main/java/org/apache/felix/maven/osgicheck/impl/checks/ConsumerProviderTypeCheck.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: felix/trunk/tools/osgicheck-maven-plugin/src/main/java/org/apache/felix/maven/osgicheck/impl/checks/ConsumerProviderTypeCheck.java
------------------------------------------------------------------------------
svn:keywords = author date id revision rev url