This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-bcel.git
The following commit(s) were added to refs/heads/master by this push:
new 7f10dbb Force unsigned short for LineNumber.toString() (#118)
7f10dbb is described below
commit 7f10dbbb6f1f909394d4c1f7667d9b0a34d21a25
Author: Mark Roberts <[email protected]>
AuthorDate: Sat Apr 2 04:49:09 2022 -0700
Force unsigned short for LineNumber.toString() (#118)
* force unsigned short for LineNumber.toString()
* updates in response to code review
* correct spacing
---
.../java/org/apache/bcel/classfile/LineNumber.java | 6 ++--
src/test/java/org/apache/bcel/PLSETestCase.java | 32 ++++++++++++++---
.../java/org/apache/bcel/data/LargeMethod.java | 40 ++++++++++++++++++++++
3 files changed, 70 insertions(+), 8 deletions(-)
diff --git a/src/main/java/org/apache/bcel/classfile/LineNumber.java
b/src/main/java/org/apache/bcel/classfile/LineNumber.java
index b5cb9e3..24c4b02 100644
--- a/src/main/java/org/apache/bcel/classfile/LineNumber.java
+++ b/src/main/java/org/apache/bcel/classfile/LineNumber.java
@@ -96,7 +96,7 @@ public final class LineNumber implements Cloneable, Node {
* @return Corresponding source line
*/
public int getLineNumber() {
- return 0xffff & lineNumber;
+ return lineNumber & 0xffff;
}
@@ -104,7 +104,7 @@ public final class LineNumber implements Cloneable, Node {
* @return PC in code
*/
public int getStartPC() {
- return 0xffff & startPc;
+ return startPc & 0xffff;
}
@@ -129,7 +129,7 @@ public final class LineNumber implements Cloneable, Node {
*/
@Override
public String toString() {
- return "LineNumber(" + startPc + ", " + lineNumber + ")";
+ return "LineNumber(" + getStartPC() + ", " + getLineNumber() + ")";
}
diff --git a/src/test/java/org/apache/bcel/PLSETestCase.java
b/src/test/java/org/apache/bcel/PLSETestCase.java
index 6b1951f..a8e90df 100644
--- a/src/test/java/org/apache/bcel/PLSETestCase.java
+++ b/src/test/java/org/apache/bcel/PLSETestCase.java
@@ -20,6 +20,8 @@ package org.apache.bcel;
import org.apache.bcel.classfile.Code;
import org.apache.bcel.classfile.JavaClass;
+import org.apache.bcel.classfile.LineNumber;
+import org.apache.bcel.classfile.LineNumberTable;
import org.apache.bcel.classfile.LocalVariable;
import org.apache.bcel.classfile.LocalVariableTable;
import org.apache.bcel.classfile.Method;
@@ -34,6 +36,7 @@ import org.apache.bcel.generic.Type;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
public class PLSETestCase extends AbstractTestCase
{
@@ -44,7 +47,7 @@ public class PLSETestCase extends AbstractTestCase
@Test
public void testB208() throws ClassNotFoundException
{
- final JavaClass clazz =
getTestClass(PACKAGE_BASE_NAME+".data.PLSETestClass");
+ final JavaClass clazz = getTestClass(PACKAGE_BASE_NAME +
".data.PLSETestClass");
final ClassGen gen = new ClassGen(clazz);
final ConstantPoolGen pool = gen.getConstantPool();
final Method m = gen.getMethodAt(1);
@@ -61,7 +64,7 @@ public class PLSETestCase extends AbstractTestCase
@Test
public void testB79() throws ClassNotFoundException
{
- final JavaClass clazz =
getTestClass(PACKAGE_BASE_NAME+".data.PLSETestClass");
+ final JavaClass clazz = getTestClass(PACKAGE_BASE_NAME +
".data.PLSETestClass");
final ClassGen gen = new ClassGen(clazz);
final ConstantPoolGen pool = gen.getConstantPool();
final Method m = gen.getMethodAt(2);
@@ -80,7 +83,7 @@ public class PLSETestCase extends AbstractTestCase
@Test
public void testB262() throws ClassNotFoundException
{
- final JavaClass clazz =
getTestClass(PACKAGE_BASE_NAME+".data.PLSETestEnum");
+ final JavaClass clazz = getTestClass(PACKAGE_BASE_NAME +
".data.PLSETestEnum");
final ClassGen gen = new ClassGen(clazz);
final ConstantPoolGen pool = gen.getConstantPool();
// get the values() method
@@ -102,7 +105,7 @@ public class PLSETestCase extends AbstractTestCase
@Test
public void testB295() throws Exception
{
- final JavaClass clazz =
getTestClass(PACKAGE_BASE_NAME+".data.PLSETestClass2");
+ final JavaClass clazz = getTestClass(PACKAGE_BASE_NAME +
".data.PLSETestClass2");
final ClassGen cg = new ClassGen(clazz);
final ConstantPoolGen pool = cg.getConstantPool();
final Method m = cg.getMethodAt(1); // 'main'
@@ -117,13 +120,32 @@ public class PLSETestCase extends AbstractTestCase
}
/**
+ * BCEL-361: LineNumber.toString() treats code offset as signed
+ */
+ @Test
+ public void testB361() throws Exception
+ {
+ final JavaClass clazz = getTestClass(PACKAGE_BASE_NAME +
".data.LargeMethod");
+ final Method[] methods = clazz.getMethods();
+ final Method m = methods[0];
+ //System.out.println(m.getName());
+ final Code code = m.getCode();
+ final LineNumberTable lnt = code.getLineNumberTable();
+ final LineNumber[] lineNumbers = lnt.getLineNumberTable();
+ final String data = lineNumbers[lineNumbers.length - 1].toString();
+ //System.out.println(data);
+ //System.out.println(data.contains("-"));
+ assertFalse(data.contains("-"), "code offsets must be positive");
+ }
+
+ /**
* Test to improve BCEL tests code coverage for classfile/Utility.java.
*/
@Test
public void testCoverage() throws ClassNotFoundException,
java.io.IOException
{
// load a class with a wide variety of byte codes - including
tableswitch and lookupswitch
- final JavaClass clazz =
getTestClass(PACKAGE_BASE_NAME+".data.ConstantPoolX");
+ final JavaClass clazz = getTestClass(PACKAGE_BASE_NAME +
".data.ConstantPoolX");
for (final Method m: clazz.getMethods()) {
final String signature = m.getSignature();
Utility.methodTypeToSignature(Utility.methodSignatureReturnType(signature),
diff --git a/src/test/java/org/apache/bcel/data/LargeMethod.java
b/src/test/java/org/apache/bcel/data/LargeMethod.java
new file mode 100644
index 0000000..66281f3
--- /dev/null
+++ b/src/test/java/org/apache/bcel/data/LargeMethod.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.bcel.data;
+
+ // Due to the way try finally is implemented in the standard java compiler
+ // from Oracle, this class generates a huge (>32767 code bytes) <init>
method.
+ // Verified with javac versions 1.8.0_261, 11.0.10 and 17.0.1.
+ class LargeMethod {{
+ int a;
+ try {a=0;} finally {
+ try {a=0;} finally {
+ try {a=0;} finally {
+ try {a=0;} finally {
+ try {a=0;} finally {
+ try {a=0;} finally {
+ try {a=0;} finally {
+ try {a=0;} finally {
+ try {a=0;} finally {
+ try {a=0;} finally {
+ try {a=0;} finally {
+ try {a=0;} finally {
+ a=0;
+ }}}}}}}}}}}}
+ }}