View Javadoc
1   package org.apache.maven.plugin.coreit;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugin.AbstractMojo;
23  import org.apache.maven.plugin.Mojo;
24  import org.apache.maven.plugin.MojoExecutionException;
25  
26  import java.io.File;
27  import java.util.Properties;
28  
29  /**
30   * Checks whether API classes exported by the Maven core are assignment-compatible with types loaded from the plugin
31   * class loader. In other words, checks that types shared with the core realm are imported into the plugin realm.
32   *
33   * @author Benjamin Bentmann
34   * @goal assignment-compatible
35   * @phase initialize
36   */
37  public class AssignmentCompatibleMojo
38      extends AbstractMojo
39  {
40  
41      /**
42       * The path to the properties file used to track the results of the assignment compatibility tests.
43       *
44       * @parameter property="clsldr.assigncompatPropertiesFile"
45       */
46      private File assigncompatPropertiesFile;
47  
48      /**
49       * The qualified names of the types to check.
50       *
51       * @parameter
52       */
53      private String[] classNames;
54  
55      /**
56       * Runs this mojo.
57       *
58       * @throws MojoExecutionException If the output file could not be created.
59       */
60      public void execute()
61          throws MojoExecutionException
62      {
63          ClassLoader coreRealm = Mojo.class.getClassLoader();
64          ClassLoader pluginRealm = getClass().getClassLoader();
65  
66          if ( coreRealm == pluginRealm )
67          {
68              throw new MojoExecutionException( "Unexpected class loader hierarchy, could not detect core realm" );
69          }
70  
71          Properties properties = new Properties();
72  
73          if ( classNames != null )
74          {
75              for ( String className : classNames )
76              {
77                  String result;
78  
79                  getLog().info( "[MAVEN-CORE-IT-LOG] Loading class " + className );
80  
81                  try
82                  {
83                      Class type = pluginRealm.loadClass( className );
84                      result = getKey( type );
85                  }
86                  catch ( ClassNotFoundException e )
87                  {
88                      result = "";
89                  }
90                  properties.setProperty( "plugin." + className, result );
91                  getLog().info( "[MAVEN-CORE-IT-LOG]   plugin: " + result );
92  
93                  try
94                  {
95                      Class type = coreRealm.loadClass( className );
96                      result = getKey( type );
97                  }
98                  catch ( ClassNotFoundException e )
99                  {
100                     result = "";
101                 }
102                 properties.setProperty( "core." + className, result );
103                 getLog().info( "[MAVEN-CORE-IT-LOG]   core  : " + result );
104             }
105         }
106 
107         getLog().info( "[MAVEN-CORE-IT-LOG] Creating output file " + assigncompatPropertiesFile );
108 
109         PropertiesUtil.write( assigncompatPropertiesFile, properties );
110 
111         getLog().info( "[MAVEN-CORE-IT-LOG] Created output file " + assigncompatPropertiesFile );
112     }
113 
114     private String getKey( Class type )
115     {
116         return type.hashCode() + "@" + type.getClassLoader().hashCode();
117     }
118 
119 }