001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.tools.javah;
019:
020: import org.apache.bcel.classfile.ConstantValue;
021: import org.apache.bcel.classfile.Field;
022: import org.apache.bcel.generic.Type;
023: import org.apache.harmony.tools.Mangler;
024:
025: /**
026: * This class is a wrapper of BCEL's Field class.
027: *
028: * This class depends on Apache Byte Code Engineering Library (BCEL) 5.0 or
029: * later. Please see http://jakarta.apache.org/bcel for more information
030: * about this library.
031: */
032: public class ClazzField {
033:
034: /**
035: * An owner class.
036: */
037: private Clazz clazz;
038:
039: /**
040: * A wrapped field.
041: */
042: private Field wrappedField;
043:
044: /**
045: * Constructs a <code>ClazzField</code> object.
046: *
047: * @param clazz - an owner.
048: * @param field - a wrapped Field object.
049: */
050: public ClazzField(Clazz clazz, Field wrappedField) {
051: this .clazz = clazz;
052: this .wrappedField = wrappedField;
053: }
054:
055: /**
056: * Returns a type of the wrapped field.
057: *
058: * @return a field type.
059: */
060: private Type getType() {
061: return Type.getReturnType(wrappedField.getSignature());
062: }
063:
064: /**
065: * Returns a name of the wrapped field.
066: *
067: * @return a field name.
068: */
069: public String getName() {
070: return wrappedField.getName();
071: }
072:
073: /**
074: * Returns a mangled name of a wrapped field.
075: *
076: * @return a mangled field name.
077: */
078: public String getMangledName() {
079: return Mangler.mangleFieldName(clazz.getName()) + "_"
080: + Mangler.mangleFieldName(getName());
081: }
082:
083: /**
084: * Returns a string representation of a native value
085: * based on a wrapped field value.
086: *
087: * @return a string that represents a wrapped field value
088: * as a native data type.
089: * @throws Exception - if the wrapped field value is inaccessible.
090: */
091: public String getNativeValue() throws Exception {
092: return ClazzField.getNativeValue(getType(), wrappedField
093: .getConstantValue());
094: }
095:
096: /**
097: * Returns a string that represents a field part of
098: * a JNI-style header file.
099: */
100: public String toString() {
101: String n = System.getProperty("line.separator");
102: StringBuffer result = new StringBuffer();
103:
104: String mangledName = getMangledName();
105: try {
106: String field = "#undef " + mangledName + n + "#define "
107: + mangledName + " " + getNativeValue() + n;
108:
109: // We add the field string only if there was no exception.
110: result.append(field);
111: result.append(n);
112: } catch (Exception e) {
113: result.append("/* Static field " + getName()
114: + " is not accessible */" + n);
115: result.append(n);
116: }
117:
118: return result.toString();
119: }
120:
121: /**
122: * Returns a string representation of a given object native value.
123: *
124: * @param type - a Class object that wraps a data type.
125: * @param value - an object that wraps a value of a primitive data type.
126: * @return a string that represents a native data type.
127: */
128: public static String getNativeValue(Type type, ConstantValue value) {
129: StringBuffer result = new StringBuffer();
130:
131: if (type == Type.INT) {
132: result.append(value.toString()).append('L');
133: } else if (type == Type.BYTE) {
134: result.append(value.toString()).append('L');
135: } else if (type == Type.LONG) {
136: result.append(value.toString()).append("LL");
137: } else if (type == Type.FLOAT) {
138: result.append(value.toString()).append('f');
139: } else if (type == Type.DOUBLE) {
140: result.append(value.toString());
141: } else if (type == Type.SHORT) {
142: result.append(value.toString()).append('L');
143: } else if (type == Type.CHAR) {
144: result.append(value.toString()).append('L');
145: } else if (type == Type.BOOLEAN) {
146: result.append(value.toString()).append('L');
147: }
148:
149: return result.toString();
150: }
151: }
|