summaryrefslogtreecommitdiffstats
path: root/src/main/java/net/sf/antcontrib/design/InstructionVisitor.java
blob: eb4c92016050e7e32342c2a1653e6d132cdc7cba (plain)
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
/*
 * Copyright (c) 2001-2004 Ant-Contrib project.  All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.sf.antcontrib.design;

import org.apache.bcel.generic.ANEWARRAY;
import org.apache.bcel.generic.CHECKCAST;
import org.apache.bcel.generic.ConstantPoolGen;
import org.apache.bcel.generic.EmptyVisitor;
import org.apache.bcel.generic.INSTANCEOF;
import org.apache.bcel.generic.INVOKESTATIC;
import org.apache.bcel.generic.LoadInstruction;
import org.apache.bcel.generic.NEW;
import org.apache.bcel.generic.PUTSTATIC;
import org.apache.bcel.generic.Type;
import org.apache.tools.ant.Project;



public class InstructionVisitor extends EmptyVisitor {

	
	private ConstantPoolGen poolGen;
	private Log log;
	private Design design;
	
	/**
	 * @param poolGen
	 * @param v
	 */
	public InstructionVisitor(ConstantPoolGen poolGen, Log log, Design d) {
		this.poolGen = poolGen;
		this.log = log;
		this.design = d;
	}
	
	public void visitCHECKCAST(CHECKCAST c) {
		Type t = c.getType(poolGen);
		log.log("         instr(checkcast)="+t, Project.MSG_DEBUG);
		String type = t.toString();

		design.checkClass(type);		
	}
	
	public void visitLoadInstruction(LoadInstruction l) {
		//log.log(" visit load", Project.MSG_DEBUG);
		Type t = l.getType(poolGen);
		log.log("         instr(loadinstr)="+t, Project.MSG_DEBUG);
		String type = t.toString();

		design.checkClass(type);	
	}

	public void visitNEW(NEW n) {
		Type t= n.getType(poolGen);
		log.log("         instr(new)="+t, Project.MSG_DEBUG);
		String type = t.toString();

		design.checkClass(type);
	}
	
	public void visitANEWARRAY(ANEWARRAY n) {
		Type t = n.getType(poolGen);
		log.log("         instr(anewarray)="+t, Project.MSG_DEBUG);
		String type = t.toString();

		design.checkClass(type);
	}
	
	public void visitINSTANCEOF(INSTANCEOF i) {
		Type t = i.getType(poolGen);
		log.log("         instr(instanceof)="+t, Project.MSG_DEBUG);
		String type = t.toString();

		design.checkClass(type);
	}
	public void visitINVOKESTATIC(INVOKESTATIC s) {
		String t = s.getClassName(poolGen);
		log.log("         instr(invokestatic)="+t, Project.MSG_DEBUG);

		design.checkClass(t);
	}
	
	public void visitPUTSTATIC(PUTSTATIC s) {
		String one = s.getClassName(poolGen);
		String two = s.getFieldName(poolGen);
		String three = s.getName(poolGen);
		String four = s.getSignature(poolGen);
		String five = s.getClassType(poolGen)+"";
		String six = s.getFieldType(poolGen)+"";
		log.log("         instr(putstatic)a="+one, Project.MSG_DEBUG);
		log.log("         instr(putstatic)b="+two, Project.MSG_DEBUG);
		log.log("         instr(putstatic)c="+three, Project.MSG_DEBUG);
		log.log("         instr(putstatic)d="+four, Project.MSG_DEBUG);
		log.log("         instr(putstatic)e="+five, Project.MSG_DEBUG);
		log.log("         instr(putstatic)f="+six, Project.MSG_DEBUG);
		
		String className = s.getFieldName(poolGen);
		if("staticField".equals(className))
			return;
		
		if(className.startsWith("class$") || className.startsWith("array$"))
			;
		else return;
		
		log.log("         instr(putstatic)1="+className, Project.MSG_DEBUG);
		className = className.substring(6, className.length());
		log.log("         instr(putstatic)2="+className, Project.MSG_DEBUG);		
		className = className.replace('$', '.');
		log.log("         instr(putstatic)3="+className, Project.MSG_DEBUG);

		design.checkClass(className);
	}
}