001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.classreader;
034:
035: import java.io.*;
036:
037: import org.apache.log4j.*;
038:
039: public class AttributeFactory {
040: private static final String CONSTANT_VALUE = "ConstantValue";
041: private static final String CODE = "Code";
042: private static final String EXCEPTIONS = "Exceptions";
043: private static final String INNER_CLASSES = "InnerClasses";
044: private static final String SYNTHETIC = "Synthetic";
045: private static final String SOURCE_FILE = "SourceFile";
046: private static final String LINE_NUMBER_TABLE = "LineNumberTable";
047: private static final String LOCAL_VARIABLE_TABLE = "LocalVariableTable";
048: private static final String DEPRECATED = "Deprecated";
049:
050: public static Attribute_info create(Classfile classfile,
051: Visitable owner, DataInputStream in) throws IOException {
052: Attribute_info result = null;
053:
054: int nameIndex = in.readUnsignedShort();
055: if (nameIndex > 0) {
056: Object entry = classfile.getConstantPool().get(nameIndex);
057:
058: if (entry instanceof UTF8_info) {
059: String name = ((UTF8_info) entry).getValue();
060: Logger.getLogger(AttributeFactory.class).debug(
061: "Attribute name index: " + nameIndex + " ("
062: + name + ")");
063:
064: if (CONSTANT_VALUE.equals(name)) {
065: result = new ConstantValue_attribute(classfile,
066: owner, in);
067: } else if (CODE.equals(name)) {
068: result = new Code_attribute(classfile, owner, in);
069: } else if (EXCEPTIONS.equals(name)) {
070: result = new Exceptions_attribute(classfile, owner,
071: in);
072: } else if (INNER_CLASSES.equals(name)) {
073: result = new InnerClasses_attribute(classfile,
074: owner, in);
075: } else if (SYNTHETIC.equals(name)) {
076: result = new Synthetic_attribute(classfile, owner,
077: in);
078: } else if (SOURCE_FILE.equals(name)) {
079: result = new SourceFile_attribute(classfile, owner,
080: in);
081: } else if (LINE_NUMBER_TABLE.equals(name)) {
082: result = new LineNumberTable_attribute(classfile,
083: owner, in);
084: } else if (LOCAL_VARIABLE_TABLE.equals(name)) {
085: result = new LocalVariableTable_attribute(
086: classfile, owner, in);
087: } else if (DEPRECATED.equals(name)) {
088: result = new Deprecated_attribute(classfile, owner,
089: in);
090: } else {
091: Logger.getLogger(AttributeFactory.class).warn(
092: "Unknown attribute name \"" + name + "\"");
093: result = new Custom_attribute(name, classfile,
094: owner, in);
095: }
096: } else {
097: Logger.getLogger(AttributeFactory.class).debug(
098: "Attribute name: " + entry);
099:
100: Logger.getLogger(AttributeFactory.class).warn(
101: "Unknown attribute with invalid name");
102: result = new Custom_attribute(classfile, owner, in);
103: }
104: } else {
105: Logger.getLogger(AttributeFactory.class).debug(
106: "Attribute name index: " + nameIndex);
107:
108: Logger.getLogger(AttributeFactory.class).warn(
109: "Unknown attribute with no name");
110: result = new Custom_attribute(classfile, owner, in);
111: }
112:
113: return result;
114: }
115: }
|