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