001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: LazyLoadMethodCollector.java 3685 2007-03-08 13:00:42Z gbevin $
007: */
008: package com.uwyn.rife.database.querymanagers.generic.instrument;
009:
010: import com.uwyn.rife.asm.*;
011:
012: import java.util.Collections;
013: import java.util.HashMap;
014: import java.util.Map;
015:
016: class LazyLoadMethodCollector implements ClassVisitor {
017: private Map<String, String> mMethods = null;
018: private AnnotationVisitor mAnnotationVisitor = new LazyLoadNoOpAnnotationVisitor();
019:
020: LazyLoadMethodCollector() {
021: }
022:
023: public MethodVisitor visitMethod(int access, String name,
024: String desc, String signature, String[] exceptions) {
025: // only consider public non static and non synthetic methods
026: if ((access & Opcodes.ACC_PUBLIC) != 0
027: && (access & Opcodes.ACC_STATIC) == 0
028: && (access & Opcodes.ACC_SYNTHETIC) == 0) {
029: if (name.length() > 3) {
030: Type return_type = Type.getReturnType(desc);
031: Type[] argument_types = Type.getArgumentTypes(desc);
032:
033: boolean add = false;
034:
035: // only consider getters without arguments and an object return type
036: // that is not part of the JDK java.lang package
037: if (name.startsWith("get")
038: && 0 == argument_types.length
039: && Type.OBJECT == return_type.getSort()
040: && !return_type.getClassName().startsWith(
041: "java.lang.")) {
042: add = true;
043: }
044: // only consider setters with one argument type that is not part of the
045: // JDK java.lang package and a void return type
046: else if (name.startsWith("set")
047: && 1 == argument_types.length
048: && Type.VOID == return_type.getSort()
049: && !argument_types[0].getClassName()
050: .startsWith("java.lang.")) {
051: add = true;
052: }
053:
054: if (add) {
055: if (null == mMethods) {
056: mMethods = new HashMap<String, String>();
057: }
058:
059: mMethods.put(name, desc);
060: }
061: }
062: }
063:
064: return null;
065: }
066:
067: public Map<String, String> getMethods() {
068: if (null == mMethods) {
069: return Collections.EMPTY_MAP;
070: }
071:
072: return mMethods;
073: }
074:
075: public void visit(int version, int access, String name,
076: String signature, String super Name, String[] interfaces) {
077: }
078:
079: public void visitSource(String source, String debug) {
080: }
081:
082: public void visitOuterClass(String owner, String name, String desc) {
083: }
084:
085: public AnnotationVisitor visitAnnotation(String desc,
086: boolean visible) {
087: return mAnnotationVisitor;
088: }
089:
090: public void visitAttribute(Attribute attr) {
091: }
092:
093: public void visitInnerClass(String name, String outerName,
094: String innerName, int access) {
095: }
096:
097: public FieldVisitor visitField(int access, String name,
098: String desc, String signature, Object value) {
099: return null;
100: }
101:
102: public void visitEnd() {
103: }
104:
105: private class LazyLoadNoOpAnnotationVisitor implements
106: AnnotationVisitor {
107: public void visit(String name, Object value) {
108: }
109:
110: public void visitEnum(String name, String desc, String value) {
111: }
112:
113: public AnnotationVisitor visitAnnotation(String name,
114: String desc) {
115: return this ;
116: }
117:
118: public AnnotationVisitor visitArray(String name) {
119: return this ;
120: }
121:
122: public void visitEnd() {
123: }
124: }
125: }
|