1 package org.apache.maven.plugin.coreit;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.plugin.AbstractMojo;
23 import org.apache.maven.plugin.MojoExecutionException;
24 import org.apache.maven.plugin.MojoFailureException;
25
26 import java.io.File;
27 import java.util.HashMap;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Properties;
32
33
34
35
36
37
38
39
40
41
42
43 public class InstanceofMojo
44 extends AbstractMojo
45 {
46
47
48
49
50
51
52 private File instanceofPropertiesFile;
53
54
55
56
57
58
59
60 private String className;
61
62
63
64
65
66
67 private String[] objectExpressions;
68
69
70
71
72
73
74 private List components;
75
76
77
78
79
80
81
82 private Object project;
83
84
85
86
87
88
89 public void execute()
90 throws MojoExecutionException, MojoFailureException
91 {
92 Class type;
93 try
94 {
95 getLog().info( "[MAVEN-CORE-IT-LOG] Loading class " + className );
96 type = getClass().getClassLoader().loadClass( className );
97 getLog().info( "[MAVEN-CORE-IT-LOG] Loaded class from " + type.getClassLoader() );
98 }
99 catch ( ClassNotFoundException e )
100 {
101 throw new MojoExecutionException( "Failed to load type " + className, e );
102 }
103
104 Properties instanceofProperties = new Properties();
105
106 if ( objectExpressions != null && objectExpressions.length > 0 )
107 {
108 Map contexts = new HashMap();
109 contexts.put( "project", project );
110 contexts.put( "pom", project );
111
112 for ( int i = 0; i < objectExpressions.length; i++ )
113 {
114 String expression = objectExpressions[i];
115 getLog().info( "[MAVEN-CORE-IT-LOG] Evaluating expression " + expression );
116 Object object = ExpressionUtil.evaluate( expression, contexts );
117 getLog().info( "[MAVEN-CORE-IT-LOG] Checking object " + object );
118 if ( object != null )
119 {
120 getLog().info( "[MAVEN-CORE-IT-LOG] Loaded class " + object.getClass().getName() );
121 getLog().info( "[MAVEN-CORE-IT-LOG] Loaded class from " + object.getClass().getClassLoader() );
122 }
123 instanceofProperties.setProperty( expression.replace( '/', '.' ),
124 Boolean.toString( type.isInstance( object ) ) );
125 }
126 }
127
128 if ( components != null && !components.isEmpty() )
129 {
130 for ( Iterator it = components.iterator(); it.hasNext(); )
131 {
132 Object object = it.next();
133 getLog().info( "[MAVEN-CORE-IT-LOG] Checking component " + object );
134 getLog().info( "[MAVEN-CORE-IT-LOG] Loaded class " + object.getClass().getName() );
135 getLog().info( "[MAVEN-CORE-IT-LOG] Loaded class from " + object.getClass().getClassLoader() );
136 instanceofProperties.setProperty( object.getClass().getName(),
137 Boolean.toString( type.isInstance( object ) ) );
138 }
139 }
140
141 getLog().info( "[MAVEN-CORE-IT-LOG] Creating output file " + instanceofPropertiesFile );
142
143 PropertiesUtil.write( instanceofPropertiesFile, instanceofProperties );
144
145 getLog().info( "[MAVEN-CORE-IT-LOG] Created output file " + instanceofPropertiesFile );
146 }
147
148 }