def BuildClassFilesAndHash(jarfile,ignoreDirs = [],ignoreFiles = []): print 'Building ClassFiles' zip = zipfile.ZipFile(jarfile) files = [] for name in zip.namelist(): isokay = True for x in ignoreDirs: if name.startswith(x): isokay = False break for x in ignoreFiles: if name.endswith(x): isokay = False break if isokay == True: files.append(name) for name in files: if name.endswith('.class'): hasher = hashlib.md5() t = zip.open(name) temp = ClassFile(t) ClassFiles[name.replace(".class","")]=temp t = zip.open(name) hasher.update(t.read()) Hashes[name.replace(".class","")]=hasher.hexdigest() t.close() elif not name.endswith("/"): t = zip.open(name) hasher = hashlib.md5() hasher.update(t.read()) FingerPrint["other"][name] = hasher.hexdigest()
def read_class(self, classname): if is_classfile_ext(classname): raise Exception('Invalid classname "{}" - omit ".class"'.format(classname)) class_names = self.classfiles() if classname not in class_names: raise KeyError('{} not found in jar.'.format(classname)) entry_name = format_classfile_ext(classname) return ClassFile(self.open_zip.open(entry_name, self.mode))
def test_sourcefile_write(): """ Ensure SourceFileAttribute can be written and read back. """ cf_one = ClassFile.create('SourceFileTest') cf_one.attributes.create( SourceFileAttribute, sourcefile=cf_one.constants.create_utf8('SourceFileTest.java') ) fout = StringIO() cf_one.save(fout) fin = StringIO(fout.getvalue()) cf_two = ClassFile(fin) source_file = cf_two.attributes.find_one(name='SourceFile') assert(source_file.sourcefile.value == 'SourceFileTest.java')
#!/usr/bin/env python # -*- coding: utf8 -*- """ An example showing how to create a "Hello World" class from scratch. """ from jawa import ClassFile from jawa.assemble import assemble if __name__ == "__main__": cf = ClassFile.create("HelloWorld") main = cf.methods.create("main", "([Ljava/lang/String;)V", code=True) main.access_flags.acc_static = True main.code.max_locals = 1 main.code.max_stack = 2 main.code.assemble( assemble( [ ("getstatic", cf.constants.create_field_ref("java/lang/System", "out", "Ljava/io/PrintStream;")), ("ldc", cf.constants.create_string("Hello World!")), ( "invokevirtual", cf.constants.create_method_ref("java/io/PrintStream", "println", "(Ljava/lang/String;)V"), ), ("return",), ] ) ) with open("HelloWorld.class", "wb") as fout:
""" An example showing how to create fields on a new class. """ from jawa import ClassFile if __name__ == '__main__': cf = ClassFile.create('HelloWorld') # Creating a field from a field name and descriptor field = cf.fields.create('BeerCount', 'I') # A convienience shortcut for creating static fields. field = cf.fields.create_static('HelloWorld', 'Ljava/lang/String;', cf.constants.create_string('Hello World!')) with open('HelloWorld.class', 'wb') as fout: cf.save(fout)
""" An example showing how to create fields on a new class. """ from jawa import ClassFile if __name__ == '__main__': cf = ClassFile.create('HelloWorld') # Creating a field from a field name and descriptor field = cf.fields.create('BeerCount', 'I') # A convienience shortcut for creating static fields. field = cf.fields.create_static( 'HelloWorld', 'Ljava/lang/String;', cf.constants.create_string('Hello World!') ) with open('HelloWorld.class', 'wb') as fout: cf.save(fout)