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 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
/* libjclass - Library for reading java class files
* Copyright (C) 2003 Nicos Panayides
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Id: class.c,v 1.30 2004/03/21 05:04:10 anarxia Exp $
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include <jclass/class.h>
#include <jclass/jstring.h>
#include <jclass/class_loader.h>
#include <jclass/open.h>
/*
*
* libjclass API reference
* The goal of the project is to provide an easy way to extract
* information from java class files.
*
* Credits
* Information that made this project possible was obtained from:\n
*
* File Format
* "The JavaTM Virtual Machine Specification", Second Edition\n
* by Tim Lindholm and Frank Yellin\n
* Copyright(c) 1999 Sun Microsystems Inc.\n
* Available at:
* http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html
*
* Instructions
* "Java Virtual Machine Online Instruction Reference"\n
* by Jon Meyer and Troy Downing, O'Reilly Associates\n
* Available at:
* http://mrl.nyu.edu/~meyer/jvmref/ref-Java.html
*/
/**
* jclass_class_new
* @filename: The filename or classname for the class.
* @classpath: The classpath to use to locate the class.
*
* Initializes a new JavaClass struct with info from the given file/class.
* If parsing fails it returns NULL.
* Use jclass_class_free() to free the class.
*
* Returns: A JavaClass struct allocated with malloc.
*/
JavaClass*
jclass_class_new(const char* filename, const ClassPath *classpath)
{
JavaClass* new_class = NULL;
FILE* classfile;
ClassFile* class_file_info;
int is_filename;
if(filename == NULL)
return NULL;
is_filename = ((strlen(filename) > 6) && !strcmp(".class", &filename[strlen(filename) - 6]) || (!strcmp(filename, "-")));
if(!is_filename)
{
class_file_info = jclass_classloader_get_class_file(filename, classpath);
if(class_file_info->data != NULL)
{
new_class = jclass_class_new_from_buffer(class_file_info->data);
free(class_file_info->data);
}
else if(class_file_info->file_ptr != NULL)
{
new_class = jclass_class_new_from_file(class_file_info->file_ptr);
}
else
new_class = NULL;
free(class_file_info);
}
else
{
classfile = my_open(filename, "rb");
new_class = jclass_class_new_from_file(classfile);
}
return new_class;
}
/**
* jclass_class_free
* @javaclass: The JavaClass struct to free.
*
* Frees a JavaClass struct.
*/
void jclass_class_free(JavaClass* class_struct)
{
int i;
int j;
if(class_struct->constant_pool != NULL)
jclass_cp_free(class_struct->constant_pool);
if(class_struct->interfaces != NULL)
free(class_struct->interfaces);
if(class_struct->methods != NULL)
{
for(i=0;i< class_struct->methods_count;i++)
{
for(j=0;j< class_struct->methods[i].attributes_count;j++)
{
if(class_struct->methods[i].attributes[j].contents != NULL)
free(class_struct->methods[i].attributes[j].contents);
}
if(class_struct->methods[i].attributes != NULL)
free(class_struct->methods[i].attributes);
}
free(class_struct->methods);
}
if(class_struct->fields != NULL)
{
for(i=0;i< class_struct->fields_count;i++)
{
for(j=0;j<class_struct->fields[i].attributes_count;j++)
{
if(class_struct->fields[i].attributes[j].contents != NULL)
free(class_struct->fields[i].attributes[j].contents);
}
if(class_struct->fields[i].attributes != NULL)
free(class_struct->fields[i].attributes);
}
free(class_struct->fields);
}
if(class_struct->attributes != NULL)
{
for(i=0;i< class_struct->attributes_count;i++)
{
if(class_struct->attributes[i].contents != NULL)
free(class_struct->attributes[i].contents);
}
free(class_struct->attributes);
}
free(class_struct);
}
/**
* jclass_class_get_vm_spec
* @javaclass: The class to get the VM spec for.
*
* Gives the minimum VM spec needed to run this class.
* The function returns a pointer to a constant string.
* Do not free it!
*
* Returns: A statically allocated string.
*/
const char* jclass_class_get_vm_spec(JavaClass* class_struct)
{
char* vm_spec;
static char* spec_string[] = { "unknown", "1.1", "1.2", "1.3", "1.4", "1.5", "1.6" };
if(class_struct == NULL)
return NULL;
switch(class_struct->major_version)
{
case 45:
vm_spec = spec_string[1];
break;
case 46:
vm_spec = spec_string[2];
break;
case 47:
vm_spec = spec_string[3];
break;
case 48:
vm_spec = spec_string[4];
break;
case 49:
vm_spec = spec_string[5];
break;
case 50:
vm_spec = spec_string[6];
break;
default:
vm_spec = spec_string[0];
}
return vm_spec;
}
/**
* jclass_class_get_class_name
* @javaclass: The JavaClass that we want the class name for.
*
* Gives the fully qualified name of the class.
*
* Returns: A string allocated with malloc.
*/
char* jclass_class_get_class_name(JavaClass* class_struct)
{
char* class_name;
if (class_struct == NULL)
return NULL;
if(class_struct->constant_pool == NULL)
return NULL;
class_name = jclass_cp_get_class_name(class_struct->constant_pool, class_struct->constant_pool->this_class, 0);
return class_name;
}
/**
* jclass_class_get_super_class_name
* @javaclass: The class that we want the super class name for.
*
* Gives the fully qualified name of the super class
* for the given class.
*
* Returns: A string allocated with malloc.
*/
char* jclass_class_get_super_class_name(JavaClass* class_struct)
{
char* class_name;
if (class_struct == NULL)
return NULL;
if(class_struct->constant_pool == NULL)
return NULL;
class_name = jclass_cp_get_class_name(class_struct->constant_pool, class_struct->constant_pool->super_class, 0);
return class_name;
}
/**
* jclass_class_get_sourcefile_name
* @javaclass: The class.
*
* Gives the name of the source file used to compile this class.
* If the class does not have a SourceFile attribute it returns NULL.
*
* Returns: A string allocated with malloc.
*/
char* jclass_class_get_sourcefile_name(JavaClass* class_struct)
{
AttributeContainer* attribute;
ConstantPool* cpool;
char* filename;
uint16_t i;
if (class_struct == NULL)
return NULL;
filename = NULL;
cpool = class_struct->constant_pool;
attribute = class_struct->attributes;
for(i = 0; i < class_struct->attributes_count; i++)
{
if(jclass_attribute_container_has_attribute(&(attribute[i]), "SourceFile", cpool))
{
SourceFileAttribute* sourcefile = jclass_sourcefile_attribute_new(&attribute[i]);
filename = jclass_cp_get_constant_value(cpool, sourcefile->filename_index, INT_IS_INT);
jclass_sourcefile_attribute_free(sourcefile);
break;
}
}
return filename;
}
/**
* jclass_class_get_interfaces
* @class_struct: The class to get its interfaces.
*
* Gives a null terminated array with the names of all interfaces implemented
* by the given class. If the class does not implement anything it returns
* NULL.
*
* @Since: 0.4
*
* Returns: A string allocated with malloc.
*/
char **jclass_class_get_interfaces(JavaClass* class_struct)
{
char **interface_name;
uint16_t count, no_interfaces;
no_interfaces = class_struct->interfaces_count;
if (no_interfaces == 0)
return NULL;
interface_name = (char**) malloc(sizeof(char*) * (no_interfaces+1));
for(count = 0; count < no_interfaces; count++)
{
interface_name[count] = jclass_cp_get_class_name(class_struct->constant_pool,
class_struct->interfaces[count], 0);
}
interface_name[count] = NULL;
return interface_name;
}
/**
* jclass_class_get_package_name
* @javaclass: The class to get its package name.
*
* Gives the name of the package this class is part of.
* If the class is not in a package it returns NULL.
*
* Returns: A string allocated with malloc.
*/
char* jclass_class_get_package_name(JavaClass* javaclass)
{
char* class_name;
char* package_name;
if (javaclass == NULL)
return NULL;
class_name = jclass_class_get_class_name(javaclass);
package_name = jclass_get_package_from_class_name(class_name);
free(class_name);
return package_name;
}
|