[Joafip-svn] SF.net SVN: joafip:[2918] trunk/joafip/src
Brought to you by:
luc_peuvrier
|
From: <luc...@us...> - 2011-10-14 06:00:38
|
Revision: 2918
http://joafip.svn.sourceforge.net/joafip/?rev=2918&view=rev
Author: luc_peuvrier
Date: 2011-10-14 06:00:31 +0000 (Fri, 14 Oct 2011)
Log Message:
-----------
direct access field access control ok
Modified Paths:
--------------
trunk/joafip/src/main/java/net/sf/joafip/store/entity/proxy/OpcodeNode.java
trunk/joafip/src/main/java/net/sf/joafip/store/entity/proxy/StackElement.java
trunk/joafip/src/main/java/net/sf/joafip/store/service/bytecode/proxy/CheckMethodVisitor.java
trunk/joafip/src/test/java/net/sf/joafip/store/service/bytecode/proxy/ForTestCheckMethodVisitor.java
trunk/joafip/src/test/java/net/sf/joafip/store/service/bytecode/proxy/TestCheckMethodVisitor.java
trunk/joafip/src/test/java/net/sf/joafip/store/service/bytecode/proxy/ToCheckClass.java
Modified: trunk/joafip/src/main/java/net/sf/joafip/store/entity/proxy/OpcodeNode.java
===================================================================
--- trunk/joafip/src/main/java/net/sf/joafip/store/entity/proxy/OpcodeNode.java 2011-10-12 03:04:21 UTC (rev 2917)
+++ trunk/joafip/src/main/java/net/sf/joafip/store/entity/proxy/OpcodeNode.java 2011-10-14 06:00:31 UTC (rev 2918)
@@ -38,6 +38,8 @@
@NotStorableClass
public class OpcodeNode {
+ private static final String TAB = " ";
+
protected final static OpcodeInterpreter OPCODE_INTERPRETER = OpcodeInterpreter
.getInstance();
@@ -94,9 +96,20 @@
public void setBeforeExecutionStackElement(
final StackElement beforeExecutionStackElement) {
- assert this.beforeExecutionStackElement == null : "to set to\n"
- + beforeExecutionStackElement + "\n and is\n"
- + this.beforeExecutionStackElement;
+ // stack change accepted
+ // assert this.beforeExecutionStackElement == null
+ // ||
+ // this.beforeExecutionStackElement.equals(beforeExecutionStackElement)
+ // : OPCODE_INTERPRETER
+ // .mnemonic(opcode)
+ // + " line "
+ // + lineNumber
+ // + " @"
+ // + address
+ // + " to set to\n"
+ // + beforeExecutionStackElement
+ // + "\n and is\n"
+ // + this.beforeExecutionStackElement;
this.beforeExecutionStackElement = beforeExecutionStackElement;
}
@@ -137,8 +150,12 @@
@Override
public String toString() {
final StringBuilder stringBuilder = new StringBuilder();
- if (label != null) {
- stringBuilder.append(label.toString());
+ if (label == null) {
+ stringBuilder.append(TAB);
+ } else {
+ final String labelToString = label.toString();
+ stringBuilder.append(labelToString);
+ stringBuilder.append(TAB.substring(labelToString.length()));
}
stringBuilder.append('@');
stringBuilder.append(address);
@@ -159,12 +176,15 @@
stringBuilder.append(' ');
}
stringBuilder.append('\n');
+ stringBuilder.append(TAB);
if (beforeExecutionStackElement == null) {
stringBuilder.append('?');
} else {
stringBuilder.append(beforeExecutionStackElement.toString());
}
- stringBuilder.append("\n=> ");
+ stringBuilder.append('\n');
+ stringBuilder.append(TAB);
+ stringBuilder.append("=> ");
if (afterExecutionStackElement == null) {
stringBuilder.append('?');
} else {
Modified: trunk/joafip/src/main/java/net/sf/joafip/store/entity/proxy/StackElement.java
===================================================================
--- trunk/joafip/src/main/java/net/sf/joafip/store/entity/proxy/StackElement.java 2011-10-12 03:04:21 UTC (rev 2917)
+++ trunk/joafip/src/main/java/net/sf/joafip/store/entity/proxy/StackElement.java 2011-10-14 06:00:31 UTC (rev 2918)
@@ -67,6 +67,37 @@
}
@Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((type == null) ? 0 : type.hashCode());
+ return result;
+ }
+
+ @SuppressWarnings("PMD")
+ @Override
+ public boolean equals(final Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (getClass() != obj.getClass()) {
+ return false;
+ }
+ StackElement other = (StackElement) obj;
+ if (type != other.type) {
+ return false;
+ }
+ if (previous == null) {
+ return true;
+ } else {
+ return previous.equals(other.previous);
+ }
+ }
+
+ @Override
public String toString() {
final String typeName;
if (thisReference) {
Modified: trunk/joafip/src/main/java/net/sf/joafip/store/service/bytecode/proxy/CheckMethodVisitor.java
===================================================================
--- trunk/joafip/src/main/java/net/sf/joafip/store/service/bytecode/proxy/CheckMethodVisitor.java 2011-10-12 03:04:21 UTC (rev 2917)
+++ trunk/joafip/src/main/java/net/sf/joafip/store/service/bytecode/proxy/CheckMethodVisitor.java 2011-10-14 06:00:31 UTC (rev 2918)
@@ -26,6 +26,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
+import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -37,6 +38,7 @@
import net.sf.joafip.asm.Opcodes;
import net.sf.joafip.logger.JoafipLogger;
import net.sf.joafip.store.entity.proxy.DirectAccessInfo;
+import net.sf.joafip.store.entity.proxy.EnumStackEltType;
import net.sf.joafip.store.entity.proxy.OpcodeNode;
import net.sf.joafip.store.entity.proxy.OpcodeNodeAloadThis;
import net.sf.joafip.store.entity.proxy.OpcodeNodeDup;
@@ -64,6 +66,9 @@
@NotStorableClass
public class CheckMethodVisitor implements MethodVisitor, Opcodes {
+ private static final StackElement HANDLER_START_STACK_ELEMENT = new StackElement(
+ null, EnumStackEltType.REF);
+
private final static JoafipLogger LOGGER = JoafipLogger
.getLogger(CheckMethodVisitor.class);
@@ -106,6 +111,7 @@
@Override
public void visitEnd() {
+ listener.codeVisisted(methodAbsoluteName);
try {
for (Map.Entry<OpcodeNode, Set<Label>> entry : gotoMap.entrySet()) {
final OpcodeNode opcodeNode = entry.getKey();
@@ -123,9 +129,10 @@
// FIXMELUC _______________________________cas du jsr/ret
- for (OpcodeNode opcodeNode : startOpcodeNode
- .getNextOpcodeNodeList()) {
- check(opcodeNode);
+ final List<OpcodeNode> nextOpcodeNodeList = startOpcodeNode
+ .getNextOpcodeNodeList();
+ for (OpcodeNode opcodeNode : nextOpcodeNodeList) {
+ check(opcodeNode, null);
}
for (Label label : handlerEntrySet) {
@@ -134,9 +141,8 @@
throw new EnhanceException("undefined label "
+ label.toString());
}
- check(handlerOpcode);
+ check(handlerOpcode, HANDLER_START_STACK_ELEMENT);
}
- listener.codeVisisted(methodAbsoluteName);
} catch (Exception exception) {
final String unassembled = listener.unassembled(methodAbsoluteName);
throw new RuntimeEnhanceException(new EnhanceException(
@@ -153,13 +159,14 @@
}
}
- private void check(final OpcodeNode startOpcodeNode)
- throws EnhanceException {
+ private void check(final OpcodeNode startOpcodeNode,
+ final StackElement startStackElement) throws EnhanceException {
final Deque<OpcodeNode> toVisitQue = new LinkedList<OpcodeNode>();
final Deque<StackElement> stackElementQue = new LinkedList<StackElement>();
final Set<OpcodeNode> visited = new HashSet<OpcodeNode>();
OpcodeNode currentOpcodeNode = startOpcodeNode;
- StackElement stackElement = null;
+ StackElement stackElement = startStackElement;
+ visited.add(currentOpcodeNode);
while (currentOpcodeNode != null) {
listener.currentOpcode(currentOpcodeNode);
currentOpcodeNode.setBeforeExecutionStackElement(stackElement);
@@ -173,7 +180,7 @@
toVisitQue.add(next);
stackElementQue.add(stackElement);
}
- // FIXMELUC __________________________stack change
+ // stack change accepted
// else if (stackElement !=
// next.getBeforeExecutionStackElement()) {
// throw new EnhanceException("stack change:\n" + stackElement
@@ -289,7 +296,10 @@
@Override
public void visitJumpInsn(final int opcode, final Label label) {
- // TODO Auto-generated method stub
+ // FIXMELUC ______________________for test
+ if (opcode == JSR) {
+ System.out.println(); // NOPMD
+ }
currentOpcodeNode = new OpcodeNode(opcode, currentAddress++,
currentLineNumber, currentOpcodeNode);
addGoto(currentOpcodeNode, label);
@@ -301,6 +311,7 @@
@Override
public void visitLabel(final Label label) {
+ assert visitedLabel == null;
visitedLabel = label;
}
@@ -355,7 +366,7 @@
@Override
public void visitTryCatchBlock(final Label start, final Label end,
final Label handler, final String type) {
- // TODO Auto-generated method stub
+ handlerEntrySet.add(handler);
}
@Override
Modified: trunk/joafip/src/test/java/net/sf/joafip/store/service/bytecode/proxy/ForTestCheckMethodVisitor.java
===================================================================
--- trunk/joafip/src/test/java/net/sf/joafip/store/service/bytecode/proxy/ForTestCheckMethodVisitor.java 2011-10-12 03:04:21 UTC (rev 2917)
+++ trunk/joafip/src/test/java/net/sf/joafip/store/service/bytecode/proxy/ForTestCheckMethodVisitor.java 2011-10-14 06:00:31 UTC (rev 2918)
@@ -22,9 +22,6 @@
*/
package net.sf.joafip.store.service.bytecode.proxy;
-import net.sf.joafip.store.service.bytecode.proxy.CheckMethodVisitor;
-import net.sf.joafip.store.service.bytecode.proxy.ICheckMethodListener;
-
/**
*
* @author luc peuvrier
Modified: trunk/joafip/src/test/java/net/sf/joafip/store/service/bytecode/proxy/TestCheckMethodVisitor.java
===================================================================
--- trunk/joafip/src/test/java/net/sf/joafip/store/service/bytecode/proxy/TestCheckMethodVisitor.java 2011-10-12 03:04:21 UTC (rev 2917)
+++ trunk/joafip/src/test/java/net/sf/joafip/store/service/bytecode/proxy/TestCheckMethodVisitor.java 2011-10-14 06:00:31 UTC (rev 2918)
@@ -27,6 +27,7 @@
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
+import java.util.Set;
import net.sf.joafip.AbstractJoafipTestCase;
import net.sf.joafip.TestException;
@@ -57,6 +58,8 @@
private transient String methodDescriptor;
+ private transient String absoluteMethodName;
+
public TestCheckMethodVisitor() throws TestException {
super();
}
@@ -77,6 +80,7 @@
checkMethodListener = null;// NOPMD
methodName = null;// NOPMD
methodDescriptor = null;// NOPMD
+ absoluteMethodName = null;// NOPMD
super.tearDown();
}
@@ -178,6 +182,14 @@
checkMethodListener.hasDirectAccess());
}
+ public void testWhileNokMethod() throws NoSuchMethodException, IOException,
+ EnhanceException {
+ check("whileNokMethod", new Class[] { ToCheckClass.class });
+ assertTrue("whileNokMethod() must not be ok\n" + directAccess(),
+ checkMethodListener.hasDirectAccess());
+ assertDirectAccess(new int[] { 104 });
+ }
+
public void testWhileMethod() throws NoSuchMethodException, IOException,
EnhanceException {
check("whileMethod", new Class[] { Class.class });
@@ -185,28 +197,77 @@
checkMethodListener.hasDirectAccess());
}
+ public void testTryCatchMethod() throws NoSuchMethodException, IOException,
+ EnhanceException {
+ check("tryCatchMethod", new Class[] { ToCheckClass.class });
+ assertTrue("tryCatchMethod() must not be ok\n" + directAccess(),
+ checkMethodListener.hasDirectAccess());
+ assertDirectAccess(new int[] { 127 });
+ }
+
+ public void testTryCatchNopMethod() throws NoSuchMethodException,
+ IOException, EnhanceException {
+ check("tryCatchNopMethod", new Class[] {});
+ assertFalse("tryCatchNopMethod() must be ok\n" + directAccess(),
+ checkMethodListener.hasDirectAccess());
+ }
+
+ public void testIndirectMethod() throws NoSuchMethodException, IOException,
+ EnhanceException {
+ check("indirectMethod", new Class[] {});
+ assertTrue("indirectMethod() must not be ok\n" + directAccess(),
+ checkMethodListener.hasDirectAccess());
+ assertDirectAccess(new int[] { 142 });
+ }
+
+ public void testFinallyMethod() throws NoSuchMethodException, IOException,
+ EnhanceException {
+ check("finallyMethod", new Class[] { ToCheckClass.class });
+ assertTrue("finallyMethod() must not be ok\n" + directAccess(),
+ checkMethodListener.hasDirectAccess());
+ assertDirectAccess(new int[] { 149 });
+ }
+
+ private void assertDirectAccess(final int[] lineNumbers) {
+ final int numberOfError = lineNumbers.length;
+ final Set<DirectAccessInfo> directAccessSet = checkMethodListener
+ .getDirectAccessSet();
+ assertEquals("bad number of error", numberOfError,
+ directAccessSet.size());
+ for (int lineNumber : lineNumbers) {
+ final DirectAccessInfo key =
+ /**/new DirectAccessInfo(absoluteMethodName, null, lineNumber);// NOPMD
+ assertTrue("expected error " + key, directAccessSet.contains(key));
+ }
+ }
+
private String directAccess() {
final StringBuilder stringBuilder = new StringBuilder();
for (DirectAccessInfo directAccessInfo : checkMethodListener
.getDirectAccessSet()) {
stringBuilder.append(directAccessInfo.toString());
stringBuilder.append('\n');
- stringBuilder.append(checkMethodListener
- .unassembled(directAccessInfo.getMethodAbsoluteName()));
- stringBuilder.append('\n');
+ // stringBuilder.append(checkMethodListener
+ // .unassembled(directAccessInfo.getMethodAbsoluteName()));
+ // stringBuilder.append('\n');
}
return stringBuilder.toString();
}
- private void check(final String name, final Class<?>[] parameterTypes)
+ private void check(final String methodName, final Class<?>[] parameterTypes)
throws NoSuchMethodException, IOException, EnhanceException {
- if (name == null) {
+ if (methodName == null) {
setConstructorTotest(parameterTypes);
} else {
- setMethodTotest(name, parameterTypes);
+ setMethodTotest(methodName, parameterTypes);
}
+ final String toCheckClassName = ToCheckClass.class.getName().replace(
+ '.', '/');
+ absoluteMethodName = toCheckClassName + "#"
+ + (methodName == null ? "<init>" : methodName)
+ + methodDescriptor;
final InputStream inputStream = ClassLoader
- .getSystemResourceAsStream("net/sf/joafip/store/service/bytecode/proxy/ToCheckClass.class");
+ .getSystemResourceAsStream(toCheckClassName + ".class");
final ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
int byteValue;
while ((byteValue = inputStream.read()) != -1) {// NOPMD
Modified: trunk/joafip/src/test/java/net/sf/joafip/store/service/bytecode/proxy/ToCheckClass.java
===================================================================
--- trunk/joafip/src/test/java/net/sf/joafip/store/service/bytecode/proxy/ToCheckClass.java 2011-10-12 03:04:21 UTC (rev 2917)
+++ trunk/joafip/src/test/java/net/sf/joafip/store/service/bytecode/proxy/ToCheckClass.java 2011-10-14 06:00:31 UTC (rev 2918)
@@ -101,7 +101,7 @@
public void whileNokMethod(final ToCheckClass toCheckClass) {
int count = 0;
while (count < 5) {
- toCheckClass.globalCount++;
+ toCheckClass.globalCount++;// direct access
count++;
}
}
@@ -119,4 +119,34 @@
@SuppressWarnings("PMD")
private void operation(final Class<?> clazz) {
}
+
+ public void tryCatchMethod(final ToCheckClass toCheckClass) {
+ try {
+ operation(getClass());
+ } catch (Exception exception) {
+ toCheckClass.globalCount++;// direct access
+ }
+ }
+
+ public void tryCatchNopMethod() {
+ try {
+ incStaticCount();
+ } catch (Exception exception) {// NOPMD
+ // ignore
+ }
+ operation(getClass());
+ }
+
+ public Class<?> indirectMethod() {
+ final ToCheckClass toCheckClass = this;
+ return toCheckClass.root;// NOPMD
+ }
+
+ public void finallyMethod(final ToCheckClass toCheckClass) {
+ try {
+ operation(getClass());
+ } finally {
+ toCheckClass.globalCount++;// direct access
+ }
+ }
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|