KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > core > CheckDebugAttributes


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.core;
12
13 import java.io.IOException JavaDoc;
14 import java.util.Enumeration JavaDoc;
15 import java.util.zip.ZipEntry JavaDoc;
16 import java.util.zip.ZipFile JavaDoc;
17
18 import org.apache.tools.ant.BuildException;
19 import org.apache.tools.ant.Task;
20 import org.eclipse.jdt.core.util.IClassFileReader;
21 import org.eclipse.jdt.core.util.ICodeAttribute;
22 import org.eclipse.jdt.core.util.IMethodInfo;
23 import org.eclipse.jdt.internal.antadapter.AntAdapterMessages;
24
25 /**
26  * <p>An Ant task to find out if a class file or a jar contains debug attributes. If this is the case,
27  * the property contains the value "has debug" after the call.
28  * </p>
29  * <p>
30  * <code>&lt;eclipse.checkDebugAttributes property="hasDebug" file="${basedir}/bin/p/A.class"/&gt;</code>
31  * </p>
32  * <p>
33  * For more information on Ant check out the website at http://jakarta.apache.org/ant/ .
34  * </p>
35  *
36  * This is not intended to be subclassed by users.
37  * @since 2.0
38  */

39 public final class CheckDebugAttributes extends Task {
40
41     private String JavaDoc file;
42     private String JavaDoc property;
43     
44     public void execute() throws BuildException {
45         if (this.file == null) {
46             throw new BuildException(AntAdapterMessages.getString("checkDebugAttributes.file.argument.cannot.be.null")); //$NON-NLS-1$
47
}
48         if (this.property == null) {
49             throw new BuildException(AntAdapterMessages.getString("checkDebugAttributes.property.argument.cannot.be.null")); //$NON-NLS-1$
50
}
51         try {
52             boolean hasDebugAttributes = false;
53             if (org.eclipse.jdt.internal.compiler.util.Util.isArchiveFileName(this.file)) {
54                 ZipFile JavaDoc jarFile = new ZipFile JavaDoc(this.file);
55                 for (Enumeration JavaDoc entries = jarFile.entries(); !hasDebugAttributes && entries.hasMoreElements(); ) {
56                     ZipEntry JavaDoc entry = (ZipEntry JavaDoc) entries.nextElement();
57                     if (org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(entry.getName())) {
58                         IClassFileReader classFileReader = ToolFactory.createDefaultClassFileReader(this.file, entry.getName(), IClassFileReader.ALL);
59                         hasDebugAttributes = checkClassFile(classFileReader);
60                     }
61                 }
62             } else if (org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(this.file)) {
63                 IClassFileReader classFileReader = ToolFactory.createDefaultClassFileReader(this.file, IClassFileReader.ALL);
64                 hasDebugAttributes = checkClassFile(classFileReader);
65             } else {
66                 throw new BuildException(AntAdapterMessages.getString("checkDebugAttributes.file.argument.must.be.a.classfile.or.a.jarfile")); //$NON-NLS-1$
67
}
68             if (hasDebugAttributes) {
69                 getProject().setUserProperty(this.property, "has debug"); //$NON-NLS-1$
70
}
71         } catch (IOException JavaDoc e) {
72             throw new BuildException(AntAdapterMessages.getString("checkDebugAttributes.ioexception.occured") + this.file); //$NON-NLS-1$
73
}
74     }
75     
76     private boolean checkClassFile(IClassFileReader classFileReader) {
77         IMethodInfo[] methodInfos = classFileReader.getMethodInfos();
78         for (int i = 0, max = methodInfos.length; i < max; i++) {
79             ICodeAttribute codeAttribute = methodInfos[i].getCodeAttribute();
80             if (codeAttribute != null && codeAttribute.getLineNumberAttribute() != null) {
81                 return true;
82             }
83         }
84         return false;
85     }
86
87     public void setFile(String JavaDoc value) {
88         this.file = value;
89     }
90     
91     public void setProperty(String JavaDoc value) {
92         this.property = value;
93     }
94 }
95
Popular Tags