1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
import java.io.*;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.taskdefs.MatchingTask;
public class CopyrightInsertionTask extends MatchingTask
{
private static final String copyrightStringPart1_ = "Copyright (C) ";
private static final String copyrightStringPart2_ = "1997-2017";
private static final String copyrightStringPart3_ = " International Business Machines Corporation and others.";
private static final String copyrightString_ = copyrightStringPart1_ + copyrightStringPart2_ + copyrightStringPart3_;
private static final int copyrightStringLength_ = copyrightString_.length();
private boolean verbose_;
private File destDir_; // directory where the *.class files are located
private int numFilesProcessed_ = 0; // count of class files processed
private int numFilesStamped_ = 0; // count of class files stamped
private int numFilesAlreadyStamped_ = 0; // count of class files already stamped
public void setDestdir(File destDir)
{
destDir_ = destDir;
}
public void setVerbose(boolean verbose)
{
verbose_ = verbose;
}
// Executes the task.
public void execute() throws BuildException
{
DirectoryScanner scanner = getDirectoryScanner(destDir_);
String[] classFileNames = scanner.getIncludedFiles();
for (int i=0; i<classFileNames.length; ++i)
{
File classFile = new File(destDir_, classFileNames[i]);
if (verbose_) System.out.println("Processing "+classFileNames[i]);
insertCopyrightString(classFile);
numFilesProcessed_++;
}
System.out.println("Number of class files processed: " + numFilesProcessed_);
System.out.println("Number of class files stamped: " + numFilesStamped_);
System.out.println("Number of class files already stamped: " + numFilesAlreadyStamped_);
}
private void insertCopyrightString(File classFile) throws BuildException {
try
{
long start = System.currentTimeMillis();
DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(classFile)));
byte[] prePoolBytes = new byte[8];
dis.read(prePoolBytes);
int constantPoolCount = dis.readUnsignedShort();
Constant[] entries = new Constant[constantPoolCount];
// There is no 0th entry.
for (int i=1; i<constantPoolCount; ++i)
{
int tag = dis.readUnsignedByte();
entries[i] = new Constant(tag, dis);
entries[i].read();
String name = entries[i].getName();
if (name != null &&
name.length() == copyrightStringLength_ &&
name.startsWith(copyrightStringPart1_) && // tolerate different dates
name.endsWith(copyrightStringPart3_))
{
dis.close();
if (verbose_) System.out.println("Already exists. Found it at index "+i);
numFilesAlreadyStamped_++;
return;
}
if (tag == Constant.CONSTANT_Long_info ||
tag == Constant.CONSTANT_Double_info)
{
// These take up 2 entries in the constant pool.
//entries[++i] = new Constant(tag, dis);
++i;
}
}
int bytesLeft = dis.available();
byte[] postPoolBytes = new byte[bytesLeft];
dis.read(postPoolBytes);
dis.close();
// Add the copyright to the constant pool at the end.
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(classFile)));
dos.write(prePoolBytes, 0, prePoolBytes.length);
dos.writeShort(constantPoolCount+1);
for (int i=1; i<constantPoolCount; ++i)
{
entries[i].write(dos);
int tag = entries[i].getType();
if (tag == Constant.CONSTANT_Long_info ||
tag == Constant.CONSTANT_Double_info)
{
++i;
}
}
dos.writeByte(Constant.CONSTANT_Utf8_info);
dos.writeUTF(copyrightString_);
dos.write(postPoolBytes, 0, postPoolBytes.length);
dos.flush();
dos.close();
long end = System.currentTimeMillis();
if (verbose_) System.out.println("Stamped. Time: "+(end-start)+" ms");
numFilesStamped_++;
}
catch (Exception e) { throw new BuildException(e); }
}
final class Constant
{
public static final int CONSTANT_Class_info = 7;
public static final int CONSTANT_Fieldref_info = 9;
public static final int CONSTANT_Methodref_info = 10;
public static final int CONSTANT_InterfaceMethodref_info = 11;
public static final int CONSTANT_String_info = 8;
public static final int CONSTANT_Integer_info = 3;
public static final int CONSTANT_Float_info = 4;
public static final int CONSTANT_Long_info = 5;
public static final int CONSTANT_Double_info = 6;
public static final int CONSTANT_NameAndType_info = 12;
public static final int CONSTANT_Utf8_info = 1;
private int type_ = -1;
private DataInputStream dataIn_ = null;
private DataOutputStream dataOut_ = null;
private int data1_ = -1;
private int data2_ = -1;
private int[] bytes_ = null;
private String name_ = null;
public Constant(int type, DataInputStream dataIn)
{
type_ = type;
dataIn_ = dataIn;
}
public void read()
throws IOException
{
switch(type_)
{
case CONSTANT_Utf8_info:
data1_ = read(2);
bytes_ = new int[data1_];
for (int i=0; i<data1_; ++i)
{
bytes_[i] = read(1);
}
parseName();
break;
case CONSTANT_Class_info:
case CONSTANT_String_info:
data1_ = read(2);
break;
case CONSTANT_Double_info:
case CONSTANT_Long_info:
// These take up 2 entries in the constant pool.
// The 2nd one is unusable. This has been noted as
// a bad design choice by the JVM makers.
data1_ = read(4);
data2_ = read(4);
break;
case CONSTANT_Float_info:
case CONSTANT_Integer_info:
data1_ = read(4);
break;
case CONSTANT_Fieldref_info:
case CONSTANT_InterfaceMethodref_info:
case CONSTANT_Methodref_info:
case CONSTANT_NameAndType_info:
data1_ = read(2);
data2_ = read(2);
break;
default:
throw new IllegalArgumentException("wrong type: "+type_);
}
}
public void write(DataOutputStream dataOut)
throws IOException
{
dataOut_ = dataOut;
write(1, type_);
switch(type_)
{
case CONSTANT_Utf8_info:
write(2, data1_);
for (int i=0; i<data1_; ++i)
{
write(1, bytes_[i]);
}
break;
case CONSTANT_Class_info:
case CONSTANT_String_info:
write(2, data1_);
break;
case CONSTANT_Double_info:
case CONSTANT_Long_info:
write(4, data1_);
write(4, data2_);
break;
case CONSTANT_Float_info:
case CONSTANT_Integer_info:
write(4, data1_);
break;
case CONSTANT_Fieldref_info:
case CONSTANT_InterfaceMethodref_info:
case CONSTANT_Methodref_info:
case CONSTANT_NameAndType_info:
write(2, data1_);
write(2, data2_);
break;
default:
throw new IllegalArgumentException("wrong type: "+type_);
}
}
public int read(int size)
throws IOException
{
switch(size)
{
case 1:
return dataIn_.readUnsignedByte();
case 2:
return dataIn_.readUnsignedShort();
case 4:
return dataIn_.readInt();
default:
throw new IllegalArgumentException("number of bytes to read: "+size);
}
}
public void write(int size, int data)
throws IOException
{
switch(size)
{
case 1:
dataOut_.writeByte(data);
break;
case 2:
dataOut_.writeShort(data);
break;
case 4:
dataOut_.writeInt(data);
break;
default:
throw new IllegalArgumentException("number of bytes to write: "+size);
}
}
public int getType()
{
return type_;
}
public String getName()
{
if (type_ == CONSTANT_Utf8_info && name_ == null)
{
parseName();
}
return name_;
}
private void parseName()
{
int length = data1_;
int[] bytes = bytes_;
if (bytes == null) return;
StringBuffer name = new StringBuffer();
int i=0;
while (i<length)
{
int ch;
byte a = (byte)bytes[i];
validate(a);
if (a > 0x7F) // not a single-byte char
{
i++;
byte b = (byte)bytes[i];
validate(b);
if (b > 0xBF) // not a double-byte char
{
i++;
byte c = (byte)bytes[i];
validate(c);
ch = ((a & 0xf) << 12) + ((b & 0x3f) << 6) + (c & 0x3f);
}
else // is a double-byte
{
ch = ((a & 0x1f) << 6) + (b & 0x3f);
}
}
else // is a single-byte
{
ch = a;
}
name.append((char)ch);
i++;
}
name_ = name.toString();
}
private /*static*/ void validate(byte x)
{
if (x==0x0 || x>=0xf0)
throw new NullPointerException("Invalid byte: "+x);
}
}
}
|