Debian Codegraph @

animal-sniffer-ant-tasks/src/main/java/org/codehaus/mojo/animal_sniffer/ant/CheckSignatureTask.java

raw
1 package org.codehaus.mojo.animal_sniffer.ant;
2
3 /*
4 * The MIT License
5 *
6 * Copyright (c) 2009, codehaus.org.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 *
26 */
27
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.HashSet;
34 import java.util.Iterator;
35 import java.util.List;
36 import java.util.Set;
37 import java.util.Vector;
38
39 import org.apache.tools.ant.BuildException;
40 import org.apache.tools.ant.Project;
41 import org.apache.tools.ant.Task;
42 import org.apache.tools.ant.types.Path;
43 import org.apache.tools.ant.types.Reference;
44 import org.apache.tools.ant.types.resources.FileResource;
45 import org.codehaus.mojo.animal_sniffer.ClassFileVisitor;
46 import org.codehaus.mojo.animal_sniffer.ClassListBuilder;
47 import org.codehaus.mojo.animal_sniffer.SignatureChecker;
48
49 /**
50 * Checks the files against the provided signature.
51 *
52 * @author connollys
53 * @since 1.3
54 */
55 public class CheckSignatureTask
56 extends Task
57 {
58
59 private File signature;
60
61 private Path classpath;
62
63 private Path sourcepath;
64
65 private Vector<Path> paths = new Vector<Path>();
66
67 private Vector<Ignore> ignores = new Vector<Ignore>();
68
69 private Vector<Annotation> annotations = new Vector<Annotation>();
70
71 public void addPath( Path path )
72 {
73 paths.add( path );
74 }
75
76 public Ignore createIgnore( )
77 {
78 final Ignore result = new Ignore();
79 ignores.add( result );
80 return result;
81 }
82
83 public Annotation createAnnotation()
84 {
85 final Annotation result = new Annotation();
86 annotations.add( result );
87 return result;
88 }
89
90 public void setSignature( File signature )
91 {
92 this.signature = signature;
93 }
94
95 public Path createClasspath()
96 {
97 log( "In createClasspath", Project.MSG_INFO );
98 if ( this.classpath == null )
99 {
100 this.classpath = new Path( getProject() );
101 }
102 return this.classpath.createPath();
103 }
104
105 public void setClasspath( Path classpath )
106 {
107 log( "In setClasspath", Project.MSG_INFO );
108 if ( this.classpath == null )
109 {
110 this.classpath = classpath;
111 }
112 else
113 {
114 this.classpath.append( classpath );
115 }
116 }
117
118 public void setClasspathRef( Reference r )
119 {
120 log( "In setClasspathRef", Project.MSG_INFO );
121 createClasspath().setRefid( r );
122 }
123
124 public Path createSourcepath()
125 {
126 log( "In createSourcepath", Project.MSG_INFO );
127 if ( this.sourcepath == null )
128 {
129 this.sourcepath = new Path( getProject() );
130 }
131 return this.sourcepath.createPath();
132 }
133
134 public void setSourcepath( Path sourcepath )
135 {
136 log( "In setSourcepath", Project.MSG_INFO );
137 if ( this.sourcepath == null )
138 {
139 this.sourcepath = sourcepath;
140 }
141 else
142 {
143 this.sourcepath.append( sourcepath );
144 }
145 }
146
147 public void setSourcepathRef( Reference r )
148 {
149 log( "In setSourcepathRef", Project.MSG_INFO );
150 createSourcepath().setRefid( r );
151 }
152
153 public void execute()
154 throws BuildException
155 {
156 validate();
157 try
158 {
159 log( "Checking unresolved references to " + signature, Project.MSG_INFO );
160
161 if ( !signature.isFile() )
162 {
163 throw new BuildException( "Could not find signature: " + signature );
164 }
165
166 final Set<String> ignoredPackages = buildPackageList();
167
168 for ( Ignore ignore: ignores )
169 {
170 if ( ignore == null || ignore.getClassName() == null )
171 {
172 continue;
173 }
174 ignoredPackages.add( ignore.getClassName().replace( '.', '/' ) );
175 }
176
177 final SignatureChecker signatureChecker =
178 new SignatureChecker( new FileInputStream( signature ), ignoredPackages, new AntLogger( this ) );
179
180 final List<File> tmp = new ArrayList<File>();
181 if (sourcepath != null) {
182 Iterator<?> i = sourcepath.iterator();
183 while ( i.hasNext() )
184 {
185 Object next = i.next();
186 if ( next instanceof FileResource )
187 {
188 final File file = ( (FileResource) next ).getFile();
189 tmp.add(file);
190 }
191 }
192 }
193 signatureChecker.setSourcePath(tmp);
194
195 final Collection<String> annotationTypes = new HashSet<String>();
196 for ( Annotation annotation : annotations )
197 {
198 if ( annotation != null && annotation.getClassName() != null )
199 {
200 annotationTypes.add( annotation.getClassName() );
201 }
202 }
203 signatureChecker.setAnnotationTypes( annotationTypes );
204
205 for ( Path path : paths )
206 {
207 final String[] files = path.list();
208 for ( int j = 0; j < files.length; j++ )
209 {
210 signatureChecker.process( new File( files[j] ) );
211 }
212 }
213
214 if ( signatureChecker.isSignatureBroken() )
215 {
216 throw new BuildException( "Signature errors found. Verify them and ignore them with the "
217 + "proper annotation if needed.", getLocation() );
218 }
219 }
220 catch ( IOException e )
221 {
222 throw new BuildException( "Failed to check signatures", e );
223 }
224 }
225
226 protected void validate()
227 {
228 if ( signature == null )
229 {
230 throw new BuildException( "signature not set" );
231 }
232 if ( paths.size() < 1 )
233 {
234 throw new BuildException( "path not set" );
235 }
236
237 }
238
239 /**
240 * List of packages defined in the application.
241 */
242 private Set<String> buildPackageList()
243 throws IOException
244 {
245 ClassListBuilder plb = new ClassListBuilder( new AntLogger( this ) );
246 apply( plb );
247 return plb.getPackages();
248 }
249
250 private void apply( ClassFileVisitor v )
251 throws IOException
252 {
253 for ( Path path : paths )
254 {
255 final String[] files = path.list();
256 for ( int j = 0; j < files.length; j++ )
257 {
258 log( "Ignoring the signatures from file to be checked: " + files[j], Project.MSG_INFO );
259 v.process( new File( files[j] ) );
260 }
261 }
262 if ( classpath != null )
263 {
264 Iterator<?> i = classpath.iterator();
265 while ( i.hasNext() )
266 {
267 Object next = i.next();
268 if ( next instanceof FileResource )
269 {
270 final File file = ( (FileResource) next ).getFile();
271 log( "Ignoring the signatures from classpath: " + file, Project.MSG_INFO );
272 v.process( file );
273 }
274 }
275 }
276 }
277 }