001: package org.drools.base;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import org.drools.RuntimeDroolsException;
020: import org.drools.spi.FieldExtractor;
021: import org.drools.util.ClassUtils;
022: import org.drools.util.asm.ClassFieldInspector;
023:
024: /**
025: * This is the supertype for the ASM generated classes for accessing a field.
026: * @author Alexander Bagerman
027: */
028: abstract public class BaseClassFieldExtractor implements FieldExtractor {
029: private final int index;
030:
031: private final Class fieldType;
032:
033: private final ValueType valueType;
034:
035: /**
036: * This constructor is not supposed to be used from outside the class hirarchy
037: *
038: * @param index
039: * @param fieldType
040: * @param valueType
041: */
042: protected BaseClassFieldExtractor(final int index,
043: final Class fieldType, final ValueType valueType) {
044: this .index = index;
045: this .fieldType = fieldType;
046: this .valueType = valueType;
047: }
048:
049: /**
050: * This is the constructor to be used
051: *
052: * @param clazz
053: * @param fieldName
054: */
055: public BaseClassFieldExtractor(final Class clazz,
056: final String fieldName) {
057: try {
058: final ClassFieldInspector inspector = new ClassFieldInspector(
059: clazz);
060: this .index = ((Integer) inspector.getFieldNames().get(
061: fieldName)).intValue();
062: this .fieldType = (Class) inspector.getFieldTypes().get(
063: fieldName);
064: this .valueType = ValueType
065: .determineValueType(this .fieldType);
066: } catch (final Exception e) {
067: throw new RuntimeDroolsException(e);
068: }
069: }
070:
071: public int getIndex() {
072: return this .index;
073: }
074:
075: public Class getExtractToClass() {
076: return this .fieldType;
077: }
078:
079: public String getExtractToClassName() {
080: return ClassUtils.canonicalName(this .fieldType);
081: }
082:
083: public ValueType getValueType() {
084: return this .valueType;
085: }
086:
087: public boolean isGlobal() {
088: return false;
089: }
090:
091: public int hashCode() {
092: final int PRIME = 31;
093: int result = 1;
094: result = PRIME * result + this .fieldType.hashCode();
095: result = PRIME * result + this .index;
096: result = PRIME * result + this .valueType.hashCode();
097: return result;
098: }
099:
100: public boolean equals(final Object object) {
101: if (this == object) {
102: return true;
103: }
104: if (!(object instanceof BaseClassFieldExtractor)) {
105: return false;
106: }
107: final BaseClassFieldExtractor other = (BaseClassFieldExtractor) object;
108: return this.fieldType == other.fieldType
109: && this.index == other.index
110: && this.valueType.equals(other.valueType);
111: }
112: }
|