From 8ddaf505ae033aae2ed71e900b53159ce640f37c Mon Sep 17 00:00:00 2001 From: Benedikt Ritter Date: Tue, 7 Jun 2016 17:13:56 +0000 Subject: Revert changes introduced in BCEL-222 (rev 1694911). In particular this means that the package coordinates have been changed back from org.apache.commons.bcel6 to org.apache.bcel Furthermore the maven coordinates have been changes back from org.apache.commons:commons-bcel6:6.0-SNAPSHOT to org.apache.bcel:bcel:6.0-SNAPSHOT git-svn-id: https://svn.apache.org/repos/asf/commons/proper/bcel/trunk@1747273 13f79535-47bb-0310-9956-ffa450edef68 --- .../bcel/AbstractCounterVisitorTestCase.java | 50 ++ .../java/org/apache/bcel/AbstractTestCase.java | 208 +++++++ .../apache/bcel/AnnotationAccessFlagTestCase.java | 41 ++ .../bcel/AnnotationDefaultAttributeTestCase.java | 46 ++ .../org/apache/bcel/AnonymousClassTestCase.java | 58 ++ src/test/java/org/apache/bcel/BCELBenchmark.java | 126 ++++ .../org/apache/bcel/CounterVisitorTestCase.java | 215 +++++++ .../org/apache/bcel/ElementValueGenTestCase.java | 240 ++++++++ .../bcel/EnclosingMethodAttributeTestCase.java | 110 ++++ .../org/apache/bcel/EnumAccessFlagTestCase.java | 41 ++ .../org/apache/bcel/InstructionFinderTestCase.java | 62 ++ src/test/java/org/apache/bcel/NanoTimer.java | 52 ++ src/test/java/org/apache/bcel/PLSETestCase.java | 65 ++ src/test/java/org/apache/bcel/PerformanceTest.java | 152 +++++ .../bcel/classfile/JDKClassDumpTestCase.java | 95 +++ .../org/apache/bcel/classfile/UtilityTestCase.java | 35 ++ .../java/org/apache/bcel/data/AnnotatedFields.java | 25 + .../bcel/data/AnnotatedWithCombinedAnnotation.java | 34 ++ .../apache/bcel/data/AnnotatedWithEnumClass.java | 24 + .../apache/bcel/data/AnnotationEnumElement.java | 28 + .../org/apache/bcel/data/AnonymousClassTest.java | 41 ++ .../apache/bcel/data/AttributeTestClassEM01.java | 34 ++ .../apache/bcel/data/AttributeTestClassEM02.java | 35 ++ .../org/apache/bcel/data/CombinedAnnotation.java | 28 + .../apache/bcel/data/ComplexAnnotatedClass.java | 24 + .../org/apache/bcel/data/ComplexAnnotation.java | 42 ++ src/test/java/org/apache/bcel/data/MarkedType.java | 25 + .../org/apache/bcel/data/MarkerAnnotation.java | 27 + .../bcel/data/MarkerAnnotationInvisible.java | 25 + .../java/org/apache/bcel/data/PLSETestClass.java | 36 ++ .../org/apache/bcel/data/SimpleAnnotatedClass.java | 24 + .../org/apache/bcel/data/SimpleAnnotation.java | 30 + .../java/org/apache/bcel/data/SimpleClass.java | 27 + src/test/java/org/apache/bcel/data/SimpleEnum.java | 21 + .../apache/bcel/generic/AnnotationGenTestCase.java | 166 +++++ .../apache/bcel/generic/BranchHandleTestCase.java | 37 ++ .../bcel/generic/FieldAnnotationsTestCase.java | 167 +++++ .../GeneratingAnnotatedClassesTestCase.java | 680 +++++++++++++++++++++ .../bcel/generic/InstructionHandleTestCase.java | 46 ++ .../bcel/generic/JDKGenericDumpTestCase.java | 116 ++++ .../org/apache/bcel/generic/MethodGenTestCase.java | 91 +++ .../org/apache/bcel/util/BCELifierTestCase.java | 78 +++ .../org/apache/bcel/util/Class2HTMLTestCase.java | 42 ++ .../apache/bcel/util/InstructionFinderTest.java | 46 ++ .../bcel/verifier/AbstractVerifierTestCase.java | 95 +++ .../bcel/verifier/VerifierArrayAccessTestCase.java | 45 ++ .../bcel/verifier/VerifierInvokeTestCase.java | 41 ++ .../bcel/verifier/VerifierReturnTestCase.java | 38 ++ .../org/apache/bcel/verifier/VerifierTestCase.java | 41 ++ .../apache/bcel/verifier/tests/TestArray01.java | 54 ++ .../bcel/verifier/tests/TestArrayAccess01.java | 32 + .../verifier/tests/TestArrayAccess02Creator.java | 99 +++ .../verifier/tests/TestArrayAccess03Creator.java | 95 +++ .../verifier/tests/TestArrayAccess04Creator.java | 96 +++ .../apache/bcel/verifier/tests/TestCreator.java | 64 ++ .../verifier/tests/TestLegalInvokeInterface01.java | 29 + .../verifier/tests/TestLegalInvokeSpecial01.java | 26 + .../verifier/tests/TestLegalInvokeSpecial02.java | 28 + .../verifier/tests/TestLegalInvokeStatic01.java | 26 + .../verifier/tests/TestLegalInvokeVirtual01.java | 26 + .../verifier/tests/TestLegalInvokeVirtual02.java | 28 + .../bcel/verifier/tests/TestReturn01Creator.java | 87 +++ .../apache/bcel/verifier/tests/TestReturn02.java | 65 ++ .../bcel/verifier/tests/TestReturn03Creator.java | 83 +++ .../org/apache/bcel/visitors/CounterVisitor.java | 423 +++++++++++++ .../bcel6/AbstractCounterVisitorTestCase.java | 50 -- .../org/apache/commons/bcel6/AbstractTestCase.java | 208 ------- .../bcel6/AnnotationAccessFlagTestCase.java | 41 -- .../bcel6/AnnotationDefaultAttributeTestCase.java | 46 -- .../commons/bcel6/AnonymousClassTestCase.java | 58 -- .../org/apache/commons/bcel6/BCELBenchmark.java | 126 ---- .../commons/bcel6/CounterVisitorTestCase.java | 215 ------- .../commons/bcel6/ElementValueGenTestCase.java | 240 -------- .../bcel6/EnclosingMethodAttributeTestCase.java | 110 ---- .../commons/bcel6/EnumAccessFlagTestCase.java | 41 -- .../commons/bcel6/InstructionFinderTestCase.java | 62 -- .../java/org/apache/commons/bcel6/NanoTimer.java | 52 -- .../org/apache/commons/bcel6/PLSETestCase.java | 65 -- .../org/apache/commons/bcel6/PerformanceTest.java | 152 ----- .../bcel6/classfile/JDKClassDumpTestCase.java | 95 --- .../commons/bcel6/classfile/UtilityTestCase.java | 35 -- .../apache/commons/bcel6/data/AnnotatedFields.java | 25 - .../data/AnnotatedWithCombinedAnnotation.java | 34 -- .../commons/bcel6/data/AnnotatedWithEnumClass.java | 24 - .../commons/bcel6/data/AnnotationEnumElement.java | 28 - .../commons/bcel6/data/AnonymousClassTest.java | 41 -- .../commons/bcel6/data/AttributeTestClassEM01.java | 34 -- .../commons/bcel6/data/AttributeTestClassEM02.java | 35 -- .../commons/bcel6/data/CombinedAnnotation.java | 28 - .../commons/bcel6/data/ComplexAnnotatedClass.java | 24 - .../commons/bcel6/data/ComplexAnnotation.java | 42 -- .../org/apache/commons/bcel6/data/MarkedType.java | 25 - .../commons/bcel6/data/MarkerAnnotation.java | 27 - .../bcel6/data/MarkerAnnotationInvisible.java | 25 - .../apache/commons/bcel6/data/PLSETestClass.java | 36 -- .../commons/bcel6/data/SimpleAnnotatedClass.java | 24 - .../commons/bcel6/data/SimpleAnnotation.java | 30 - .../org/apache/commons/bcel6/data/SimpleClass.java | 27 - .../org/apache/commons/bcel6/data/SimpleEnum.java | 21 - .../bcel6/generic/AnnotationGenTestCase.java | 166 ----- .../bcel6/generic/BranchHandleTestCase.java | 37 -- .../bcel6/generic/FieldAnnotationsTestCase.java | 167 ----- .../GeneratingAnnotatedClassesTestCase.java | 680 --------------------- .../bcel6/generic/InstructionHandleTestCase.java | 46 -- .../bcel6/generic/JDKGenericDumpTestCase.java | 116 ---- .../commons/bcel6/generic/MethodGenTestCase.java | 91 --- .../commons/bcel6/util/BCELifierTestCase.java | 78 --- .../commons/bcel6/util/Class2HTMLTestCase.java | 42 -- .../commons/bcel6/util/InstructionFinderTest.java | 46 -- .../bcel6/verifier/AbstractVerifierTestCase.java | 95 --- .../verifier/VerifierArrayAccessTestCase.java | 45 -- .../bcel6/verifier/VerifierInvokeTestCase.java | 41 -- .../bcel6/verifier/VerifierReturnTestCase.java | 38 -- .../commons/bcel6/verifier/VerifierTestCase.java | 41 -- .../commons/bcel6/verifier/tests/TestArray01.java | 54 -- .../bcel6/verifier/tests/TestArrayAccess01.java | 32 - .../verifier/tests/TestArrayAccess02Creator.java | 99 --- .../verifier/tests/TestArrayAccess03Creator.java | 95 --- .../verifier/tests/TestArrayAccess04Creator.java | 96 --- .../commons/bcel6/verifier/tests/TestCreator.java | 64 -- .../verifier/tests/TestLegalInvokeInterface01.java | 29 - .../verifier/tests/TestLegalInvokeSpecial01.java | 26 - .../verifier/tests/TestLegalInvokeSpecial02.java | 28 - .../verifier/tests/TestLegalInvokeStatic01.java | 26 - .../verifier/tests/TestLegalInvokeVirtual01.java | 26 - .../verifier/tests/TestLegalInvokeVirtual02.java | 28 - .../bcel6/verifier/tests/TestReturn01Creator.java | 87 --- .../commons/bcel6/verifier/tests/TestReturn02.java | 65 -- .../bcel6/verifier/tests/TestReturn03Creator.java | 83 --- .../commons/bcel6/visitors/CounterVisitor.java | 423 ------------- 130 files changed, 5116 insertions(+), 5116 deletions(-) create mode 100644 src/test/java/org/apache/bcel/AbstractCounterVisitorTestCase.java create mode 100644 src/test/java/org/apache/bcel/AbstractTestCase.java create mode 100644 src/test/java/org/apache/bcel/AnnotationAccessFlagTestCase.java create mode 100644 src/test/java/org/apache/bcel/AnnotationDefaultAttributeTestCase.java create mode 100644 src/test/java/org/apache/bcel/AnonymousClassTestCase.java create mode 100644 src/test/java/org/apache/bcel/BCELBenchmark.java create mode 100644 src/test/java/org/apache/bcel/CounterVisitorTestCase.java create mode 100644 src/test/java/org/apache/bcel/ElementValueGenTestCase.java create mode 100644 src/test/java/org/apache/bcel/EnclosingMethodAttributeTestCase.java create mode 100644 src/test/java/org/apache/bcel/EnumAccessFlagTestCase.java create mode 100644 src/test/java/org/apache/bcel/InstructionFinderTestCase.java create mode 100644 src/test/java/org/apache/bcel/NanoTimer.java create mode 100644 src/test/java/org/apache/bcel/PLSETestCase.java create mode 100644 src/test/java/org/apache/bcel/PerformanceTest.java create mode 100644 src/test/java/org/apache/bcel/classfile/JDKClassDumpTestCase.java create mode 100644 src/test/java/org/apache/bcel/classfile/UtilityTestCase.java create mode 100644 src/test/java/org/apache/bcel/data/AnnotatedFields.java create mode 100644 src/test/java/org/apache/bcel/data/AnnotatedWithCombinedAnnotation.java create mode 100644 src/test/java/org/apache/bcel/data/AnnotatedWithEnumClass.java create mode 100644 src/test/java/org/apache/bcel/data/AnnotationEnumElement.java create mode 100644 src/test/java/org/apache/bcel/data/AnonymousClassTest.java create mode 100644 src/test/java/org/apache/bcel/data/AttributeTestClassEM01.java create mode 100644 src/test/java/org/apache/bcel/data/AttributeTestClassEM02.java create mode 100644 src/test/java/org/apache/bcel/data/CombinedAnnotation.java create mode 100644 src/test/java/org/apache/bcel/data/ComplexAnnotatedClass.java create mode 100644 src/test/java/org/apache/bcel/data/ComplexAnnotation.java create mode 100644 src/test/java/org/apache/bcel/data/MarkedType.java create mode 100644 src/test/java/org/apache/bcel/data/MarkerAnnotation.java create mode 100644 src/test/java/org/apache/bcel/data/MarkerAnnotationInvisible.java create mode 100644 src/test/java/org/apache/bcel/data/PLSETestClass.java create mode 100644 src/test/java/org/apache/bcel/data/SimpleAnnotatedClass.java create mode 100644 src/test/java/org/apache/bcel/data/SimpleAnnotation.java create mode 100644 src/test/java/org/apache/bcel/data/SimpleClass.java create mode 100644 src/test/java/org/apache/bcel/data/SimpleEnum.java create mode 100644 src/test/java/org/apache/bcel/generic/AnnotationGenTestCase.java create mode 100644 src/test/java/org/apache/bcel/generic/BranchHandleTestCase.java create mode 100644 src/test/java/org/apache/bcel/generic/FieldAnnotationsTestCase.java create mode 100644 src/test/java/org/apache/bcel/generic/GeneratingAnnotatedClassesTestCase.java create mode 100644 src/test/java/org/apache/bcel/generic/InstructionHandleTestCase.java create mode 100644 src/test/java/org/apache/bcel/generic/JDKGenericDumpTestCase.java create mode 100644 src/test/java/org/apache/bcel/generic/MethodGenTestCase.java create mode 100644 src/test/java/org/apache/bcel/util/BCELifierTestCase.java create mode 100644 src/test/java/org/apache/bcel/util/Class2HTMLTestCase.java create mode 100644 src/test/java/org/apache/bcel/util/InstructionFinderTest.java create mode 100644 src/test/java/org/apache/bcel/verifier/AbstractVerifierTestCase.java create mode 100644 src/test/java/org/apache/bcel/verifier/VerifierArrayAccessTestCase.java create mode 100644 src/test/java/org/apache/bcel/verifier/VerifierInvokeTestCase.java create mode 100644 src/test/java/org/apache/bcel/verifier/VerifierReturnTestCase.java create mode 100644 src/test/java/org/apache/bcel/verifier/VerifierTestCase.java create mode 100644 src/test/java/org/apache/bcel/verifier/tests/TestArray01.java create mode 100644 src/test/java/org/apache/bcel/verifier/tests/TestArrayAccess01.java create mode 100644 src/test/java/org/apache/bcel/verifier/tests/TestArrayAccess02Creator.java create mode 100644 src/test/java/org/apache/bcel/verifier/tests/TestArrayAccess03Creator.java create mode 100644 src/test/java/org/apache/bcel/verifier/tests/TestArrayAccess04Creator.java create mode 100644 src/test/java/org/apache/bcel/verifier/tests/TestCreator.java create mode 100755 src/test/java/org/apache/bcel/verifier/tests/TestLegalInvokeInterface01.java create mode 100755 src/test/java/org/apache/bcel/verifier/tests/TestLegalInvokeSpecial01.java create mode 100755 src/test/java/org/apache/bcel/verifier/tests/TestLegalInvokeSpecial02.java create mode 100755 src/test/java/org/apache/bcel/verifier/tests/TestLegalInvokeStatic01.java create mode 100755 src/test/java/org/apache/bcel/verifier/tests/TestLegalInvokeVirtual01.java create mode 100755 src/test/java/org/apache/bcel/verifier/tests/TestLegalInvokeVirtual02.java create mode 100644 src/test/java/org/apache/bcel/verifier/tests/TestReturn01Creator.java create mode 100644 src/test/java/org/apache/bcel/verifier/tests/TestReturn02.java create mode 100644 src/test/java/org/apache/bcel/verifier/tests/TestReturn03Creator.java create mode 100644 src/test/java/org/apache/bcel/visitors/CounterVisitor.java delete mode 100644 src/test/java/org/apache/commons/bcel6/AbstractCounterVisitorTestCase.java delete mode 100644 src/test/java/org/apache/commons/bcel6/AbstractTestCase.java delete mode 100644 src/test/java/org/apache/commons/bcel6/AnnotationAccessFlagTestCase.java delete mode 100644 src/test/java/org/apache/commons/bcel6/AnnotationDefaultAttributeTestCase.java delete mode 100644 src/test/java/org/apache/commons/bcel6/AnonymousClassTestCase.java delete mode 100644 src/test/java/org/apache/commons/bcel6/BCELBenchmark.java delete mode 100644 src/test/java/org/apache/commons/bcel6/CounterVisitorTestCase.java delete mode 100644 src/test/java/org/apache/commons/bcel6/ElementValueGenTestCase.java delete mode 100644 src/test/java/org/apache/commons/bcel6/EnclosingMethodAttributeTestCase.java delete mode 100644 src/test/java/org/apache/commons/bcel6/EnumAccessFlagTestCase.java delete mode 100644 src/test/java/org/apache/commons/bcel6/InstructionFinderTestCase.java delete mode 100644 src/test/java/org/apache/commons/bcel6/NanoTimer.java delete mode 100644 src/test/java/org/apache/commons/bcel6/PLSETestCase.java delete mode 100644 src/test/java/org/apache/commons/bcel6/PerformanceTest.java delete mode 100644 src/test/java/org/apache/commons/bcel6/classfile/JDKClassDumpTestCase.java delete mode 100644 src/test/java/org/apache/commons/bcel6/classfile/UtilityTestCase.java delete mode 100644 src/test/java/org/apache/commons/bcel6/data/AnnotatedFields.java delete mode 100644 src/test/java/org/apache/commons/bcel6/data/AnnotatedWithCombinedAnnotation.java delete mode 100644 src/test/java/org/apache/commons/bcel6/data/AnnotatedWithEnumClass.java delete mode 100644 src/test/java/org/apache/commons/bcel6/data/AnnotationEnumElement.java delete mode 100644 src/test/java/org/apache/commons/bcel6/data/AnonymousClassTest.java delete mode 100644 src/test/java/org/apache/commons/bcel6/data/AttributeTestClassEM01.java delete mode 100644 src/test/java/org/apache/commons/bcel6/data/AttributeTestClassEM02.java delete mode 100644 src/test/java/org/apache/commons/bcel6/data/CombinedAnnotation.java delete mode 100644 src/test/java/org/apache/commons/bcel6/data/ComplexAnnotatedClass.java delete mode 100644 src/test/java/org/apache/commons/bcel6/data/ComplexAnnotation.java delete mode 100644 src/test/java/org/apache/commons/bcel6/data/MarkedType.java delete mode 100644 src/test/java/org/apache/commons/bcel6/data/MarkerAnnotation.java delete mode 100644 src/test/java/org/apache/commons/bcel6/data/MarkerAnnotationInvisible.java delete mode 100644 src/test/java/org/apache/commons/bcel6/data/PLSETestClass.java delete mode 100644 src/test/java/org/apache/commons/bcel6/data/SimpleAnnotatedClass.java delete mode 100644 src/test/java/org/apache/commons/bcel6/data/SimpleAnnotation.java delete mode 100644 src/test/java/org/apache/commons/bcel6/data/SimpleClass.java delete mode 100644 src/test/java/org/apache/commons/bcel6/data/SimpleEnum.java delete mode 100644 src/test/java/org/apache/commons/bcel6/generic/AnnotationGenTestCase.java delete mode 100644 src/test/java/org/apache/commons/bcel6/generic/BranchHandleTestCase.java delete mode 100644 src/test/java/org/apache/commons/bcel6/generic/FieldAnnotationsTestCase.java delete mode 100644 src/test/java/org/apache/commons/bcel6/generic/GeneratingAnnotatedClassesTestCase.java delete mode 100644 src/test/java/org/apache/commons/bcel6/generic/InstructionHandleTestCase.java delete mode 100644 src/test/java/org/apache/commons/bcel6/generic/JDKGenericDumpTestCase.java delete mode 100644 src/test/java/org/apache/commons/bcel6/generic/MethodGenTestCase.java delete mode 100644 src/test/java/org/apache/commons/bcel6/util/BCELifierTestCase.java delete mode 100644 src/test/java/org/apache/commons/bcel6/util/Class2HTMLTestCase.java delete mode 100644 src/test/java/org/apache/commons/bcel6/util/InstructionFinderTest.java delete mode 100644 src/test/java/org/apache/commons/bcel6/verifier/AbstractVerifierTestCase.java delete mode 100644 src/test/java/org/apache/commons/bcel6/verifier/VerifierArrayAccessTestCase.java delete mode 100644 src/test/java/org/apache/commons/bcel6/verifier/VerifierInvokeTestCase.java delete mode 100644 src/test/java/org/apache/commons/bcel6/verifier/VerifierReturnTestCase.java delete mode 100644 src/test/java/org/apache/commons/bcel6/verifier/VerifierTestCase.java delete mode 100644 src/test/java/org/apache/commons/bcel6/verifier/tests/TestArray01.java delete mode 100644 src/test/java/org/apache/commons/bcel6/verifier/tests/TestArrayAccess01.java delete mode 100644 src/test/java/org/apache/commons/bcel6/verifier/tests/TestArrayAccess02Creator.java delete mode 100644 src/test/java/org/apache/commons/bcel6/verifier/tests/TestArrayAccess03Creator.java delete mode 100644 src/test/java/org/apache/commons/bcel6/verifier/tests/TestArrayAccess04Creator.java delete mode 100644 src/test/java/org/apache/commons/bcel6/verifier/tests/TestCreator.java delete mode 100755 src/test/java/org/apache/commons/bcel6/verifier/tests/TestLegalInvokeInterface01.java delete mode 100755 src/test/java/org/apache/commons/bcel6/verifier/tests/TestLegalInvokeSpecial01.java delete mode 100755 src/test/java/org/apache/commons/bcel6/verifier/tests/TestLegalInvokeSpecial02.java delete mode 100755 src/test/java/org/apache/commons/bcel6/verifier/tests/TestLegalInvokeStatic01.java delete mode 100755 src/test/java/org/apache/commons/bcel6/verifier/tests/TestLegalInvokeVirtual01.java delete mode 100755 src/test/java/org/apache/commons/bcel6/verifier/tests/TestLegalInvokeVirtual02.java delete mode 100644 src/test/java/org/apache/commons/bcel6/verifier/tests/TestReturn01Creator.java delete mode 100644 src/test/java/org/apache/commons/bcel6/verifier/tests/TestReturn02.java delete mode 100644 src/test/java/org/apache/commons/bcel6/verifier/tests/TestReturn03Creator.java delete mode 100644 src/test/java/org/apache/commons/bcel6/visitors/CounterVisitor.java (limited to 'src/test') diff --git a/src/test/java/org/apache/bcel/AbstractCounterVisitorTestCase.java b/src/test/java/org/apache/bcel/AbstractCounterVisitorTestCase.java new file mode 100644 index 00000000..ea3c99ca --- /dev/null +++ b/src/test/java/org/apache/bcel/AbstractCounterVisitorTestCase.java @@ -0,0 +1,50 @@ +/* + * 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; + +import org.apache.bcel.classfile.DescendingVisitor; +import org.apache.bcel.classfile.JavaClass; +import org.apache.bcel.visitors.CounterVisitor; + +public abstract class AbstractCounterVisitorTestCase extends AbstractTestCase +{ + protected abstract JavaClass getTestClass() throws ClassNotFoundException; + + private CounterVisitor visitor = null; + + @Override + public void setUp() throws ClassNotFoundException + { + visitor = new CounterVisitor(); + new DescendingVisitor(getTestClass(), getVisitor()).visit(); + } + + public CounterVisitor getVisitor() + { + if (visitor == null) { + visitor = new CounterVisitor(); + } + return visitor; + } + + public void setVisitor(final CounterVisitor visitor) + { + this.visitor = visitor; + } +} diff --git a/src/test/java/org/apache/bcel/AbstractTestCase.java b/src/test/java/org/apache/bcel/AbstractTestCase.java new file mode 100644 index 00000000..a7e59cca --- /dev/null +++ b/src/test/java/org/apache/bcel/AbstractTestCase.java @@ -0,0 +1,208 @@ +/* + * 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; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import org.apache.bcel.classfile.AnnotationEntry; +import org.apache.bcel.classfile.Attribute; +import org.apache.bcel.classfile.JavaClass; +import org.apache.bcel.classfile.Method; +import org.apache.bcel.generic.AnnotationEntryGen; +import org.apache.bcel.generic.ConstantPoolGen; +import org.apache.bcel.generic.ElementValueGen; +import org.apache.bcel.generic.ElementValuePairGen; +import org.apache.bcel.generic.ObjectType; +import org.apache.bcel.generic.SimpleElementValueGen; +import org.apache.bcel.util.ClassPath; +import org.apache.bcel.util.SyntheticRepository; + +import junit.framework.TestCase; + +public abstract class AbstractTestCase extends TestCase +{ + private static final boolean verbose = false; + + protected static final String PACKAGE_BASE_NAME = AbstractTestCase.class.getPackage().getName(); + + // Location of test data + protected static final File TESTDATA = new File("target", "testdata"); + + // package base name in signature format, i.e. with '/' separators instead of '.' + protected static final String PACKAGE_BASE_SIG = PACKAGE_BASE_NAME.replace('.', '/'); + + /** + * @param name + * @return Path to file under the TESTDATA directory + */ + protected File createTestdataFile(final String name) + { + return new File(TESTDATA, name); + } + + protected JavaClass getTestClass(final String name) throws ClassNotFoundException + { + return SyntheticRepository.getInstance().loadClass(name); + } + + protected Method getMethod(final JavaClass cl, final String methodname) + { + Method[] methods = cl.getMethods(); + for (Method m : methods) { + if (m.getName().equals(methodname)) + { + return m; + } + } + return null; + } + + /** + * Delete a file under the TESTDATA directory + * @param name + * @return + */ + protected boolean wipe(final String name) + { + return new File(TESTDATA, name).delete(); + } + + /** + * Delete a directory and file under the TESTDATA directory + * @param dir + * @param name + * @return true if the file was deleted + */ + protected boolean wipe(final String dir, final String name) + { + // The parameter is relative to the TESTDATA dir + boolean b = wipe(dir + File.separator + name); + final File testDir = new File(TESTDATA, dir); + String[] files = testDir.list(); + if (files == null || files.length == 0) + { + if (!testDir.delete()){ + System.err.println("Failed to remove: " + testDir); + } + } else { + System.err.println("Non-empty directory: " + testDir); + } + return b; + } + + public SyntheticRepository createRepos(final String cpentry) + { + ClassPath cp = new ClassPath("target" + File.separator + "testdata" + + File.separator + cpentry + File.separator); + return SyntheticRepository.getInstance(cp); + } + + protected Attribute[] findAttribute(final String name, final JavaClass clazz) + { + Attribute[] all = clazz.getAttributes(); + List chosenAttrsList = new ArrayList<>(); + for (Attribute element : all) { + if (verbose) { + System.err.println("Attribute: " + element.getName()); + } + if (element.getName().equals(name)) { + chosenAttrsList.add(element); + } + } + return chosenAttrsList.toArray(new Attribute[] {}); + } + + protected Attribute findAttribute(final String name, final Attribute[] all) + { + List chosenAttrsList = new ArrayList<>(); + for (Attribute element : all) { + if (verbose) { + System.err.println("Attribute: " + element.getName()); + } + if (element.getName().equals(name)) { + chosenAttrsList.add(element); + } + } + assertTrue("Should be one match: " + chosenAttrsList.size(), + chosenAttrsList.size() == 1); + return chosenAttrsList.get(0); + } + + protected String dumpAttributes(final Attribute[] as) + { + StringBuilder result = new StringBuilder(); + result.append("AttributeArray:["); + for (int i = 0; i < as.length; i++) + { + Attribute attr = as[i]; + result.append(attr.toString()); + if (i + 1 < as.length) { + result.append(","); + } + } + result.append("]"); + return result.toString(); + } + + protected String dumpAnnotationEntries(final AnnotationEntry[] as) + { + StringBuilder result = new StringBuilder(); + result.append("["); + for (int i = 0; i < as.length; i++) + { + AnnotationEntry annotation = as[i]; + result.append(annotation.toShortString()); + if (i + 1 < as.length) { + result.append(","); + } + } + result.append("]"); + return result.toString(); + } + + protected String dumpAnnotationEntries(final AnnotationEntryGen[] as) + { + StringBuilder result = new StringBuilder(); + result.append("["); + for (int i = 0; i < as.length; i++) + { + AnnotationEntryGen annotation = as[i]; + result.append(annotation.toShortString()); + if (i + 1 < as.length) { + result.append(","); + } + } + result.append("]"); + return result.toString(); + } + + public AnnotationEntryGen createFruitAnnotationEntry(final ConstantPoolGen cp, + final String aFruit, final boolean visibility) + { + SimpleElementValueGen evg = new SimpleElementValueGen( + ElementValueGen.STRING, cp, aFruit); + ElementValuePairGen nvGen = new ElementValuePairGen("fruit", evg, cp); + ObjectType t = new ObjectType("SimpleStringAnnotation"); + List elements = new ArrayList<>(); + elements.add(nvGen); + return new AnnotationEntryGen(t, elements, visibility, cp); + } +} diff --git a/src/test/java/org/apache/bcel/AnnotationAccessFlagTestCase.java b/src/test/java/org/apache/bcel/AnnotationAccessFlagTestCase.java new file mode 100644 index 00000000..c0409a28 --- /dev/null +++ b/src/test/java/org/apache/bcel/AnnotationAccessFlagTestCase.java @@ -0,0 +1,41 @@ +/* + * 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; + +import org.apache.bcel.classfile.JavaClass; + +public class AnnotationAccessFlagTestCase extends AbstractTestCase +{ + /** + * If you write an annotation and compile it, the class file generated + * should be marked as an annotation type - which is detectable through + * BCEL. + */ + public void testAnnotationClassSaysItIs() throws ClassNotFoundException + { + JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.SimpleAnnotation"); + assertTrue( + "Expected SimpleAnnotation class to say it was an annotation - but it didn't !", + clazz.isAnnotation()); + clazz = getTestClass(PACKAGE_BASE_NAME+".data.SimpleClass"); + assertTrue( + "Expected SimpleClass class to say it was not an annotation - but it didn't !", + !clazz.isAnnotation()); + } +} diff --git a/src/test/java/org/apache/bcel/AnnotationDefaultAttributeTestCase.java b/src/test/java/org/apache/bcel/AnnotationDefaultAttributeTestCase.java new file mode 100644 index 00000000..6afe6794 --- /dev/null +++ b/src/test/java/org/apache/bcel/AnnotationDefaultAttributeTestCase.java @@ -0,0 +1,46 @@ +/* + * 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; + +import org.apache.bcel.classfile.AnnotationDefault; +import org.apache.bcel.classfile.ElementValue; +import org.apache.bcel.classfile.JavaClass; +import org.apache.bcel.classfile.Method; +import org.apache.bcel.classfile.SimpleElementValue; + +public class AnnotationDefaultAttributeTestCase extends AbstractTestCase +{ + /** + * For values in an annotation that have default values, we should be able + * to query the AnnotationDefault attribute against the method to discover + * the default value that was originally declared. + */ + public void testMethodAnnotations() throws ClassNotFoundException + { + JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.SimpleAnnotation"); + Method m = getMethod(clazz, "fruit"); + AnnotationDefault a = (AnnotationDefault) findAttribute( + "AnnotationDefault", m.getAttributes()); + SimpleElementValue val = (SimpleElementValue) a.getDefaultValue(); + assertTrue("Should be STRING but is " + val.getElementValueType(), val + .getElementValueType() == ElementValue.STRING); + assertTrue("Should have default of bananas but default is " + + val.getValueString(), val.getValueString().equals("bananas")); + } +} diff --git a/src/test/java/org/apache/bcel/AnonymousClassTestCase.java b/src/test/java/org/apache/bcel/AnonymousClassTestCase.java new file mode 100644 index 00000000..6506d63d --- /dev/null +++ b/src/test/java/org/apache/bcel/AnonymousClassTestCase.java @@ -0,0 +1,58 @@ +/* + * 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; + +import org.apache.bcel.classfile.JavaClass; + +public class AnonymousClassTestCase extends AbstractTestCase +{ + public void testRegularClassIsNotAnonymous() throws ClassNotFoundException + { + JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.AnonymousClassTest"); + assertFalse("regular outer classes are not anonymous", clazz + .isAnonymous()); + assertFalse("regular outer classes are not nested", clazz.isNested()); + } + + public void testNamedInnerClassIsNotAnonymous() + throws ClassNotFoundException + { + JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.AnonymousClassTest$X"); + assertFalse("regular inner classes are not anonymous", clazz + .isAnonymous()); + assertTrue("regular inner classes are nested", clazz.isNested()); + } + + public void testStaticInnerClassIsNotAnonymous() + throws ClassNotFoundException + { + JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.AnonymousClassTest$Y"); + assertFalse("regular static inner classes are not anonymous", clazz + .isAnonymous()); + assertTrue("regular static inner classes are nested", clazz.isNested()); + } + + public void testAnonymousInnerClassIsAnonymous() + throws ClassNotFoundException + { + JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.AnonymousClassTest$1"); + assertTrue("anonymous inner classes are anonymous", clazz.isAnonymous()); + assertTrue("anonymous inner classes are anonymous", clazz.isNested()); + } +} diff --git a/src/test/java/org/apache/bcel/BCELBenchmark.java b/src/test/java/org/apache/bcel/BCELBenchmark.java new file mode 100644 index 00000000..ad48cee6 --- /dev/null +++ b/src/test/java/org/apache/bcel/BCELBenchmark.java @@ -0,0 +1,126 @@ +/* + * 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; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.util.concurrent.TimeUnit; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; + +import org.apache.bcel.classfile.ClassParser; +import org.apache.bcel.classfile.JavaClass; +import org.apache.bcel.classfile.Method; +import org.apache.bcel.generic.ClassGen; +import org.apache.bcel.generic.InstructionList; +import org.apache.bcel.generic.MethodGen; +import org.apache.commons.collections4.Predicate; +import org.apache.commons.collections4.iterators.EnumerationIterator; +import org.apache.commons.collections4.iterators.FilterIterator; +import org.apache.commons.collections4.iterators.IteratorIterable; +import org.apache.commons.io.IOUtils; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Threads; +import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.infra.Blackhole; + +@BenchmarkMode(Mode.AverageTime) +@Fork(value = 1, jvmArgs = "-server") +@Threads(1) +@Warmup(iterations = 10) +@Measurement(iterations = 20) +@OutputTimeUnit(TimeUnit.MILLISECONDS) +public class BCELBenchmark { + + private JarFile getJarFile() throws IOException { + String javaHome = System.getProperty("java.home"); + return new JarFile(javaHome + "/lib/rt.jar"); + } + + private Iterable getClasses(JarFile jar) { + return new IteratorIterable<>(new FilterIterator<>(new EnumerationIterator<>(jar.entries()), new Predicate() { + @Override + public boolean evaluate(JarEntry entry) { + return entry.getName().endsWith(".class"); + } + })); + } + + /** + * Baseline benchmark. Read the classes but don't parse them. + */ + @Benchmark + public void baseline(Blackhole bh) throws IOException { + JarFile jar = getJarFile(); + + for (JarEntry entry : getClasses(jar)) { + byte[] bytes = IOUtils.toByteArray(jar.getInputStream(entry)); + bh.consume(bytes); + } + + jar.close(); + } + + @Benchmark + public void parser(Blackhole bh) throws IOException { + JarFile jar = getJarFile(); + + for (JarEntry entry : getClasses(jar)) { + byte[] bytes = IOUtils.toByteArray(jar.getInputStream(entry)); + + JavaClass clazz = new ClassParser(new ByteArrayInputStream(bytes), entry.getName()).parse(); + bh.consume(clazz); + } + + jar.close(); + } + + @Benchmark + public void generator(Blackhole bh) throws IOException { + JarFile jar = getJarFile(); + + for (JarEntry entry : getClasses(jar)) { + byte[] bytes = IOUtils.toByteArray(jar.getInputStream(entry)); + + JavaClass clazz = new ClassParser(new ByteArrayInputStream(bytes), entry.getName()).parse(); + + ClassGen cg = new ClassGen(clazz); + + for (Method m : cg.getMethods()) { + MethodGen mg = new MethodGen(m, cg.getClassName(), cg.getConstantPool()); + InstructionList il = mg.getInstructionList(); + + if (il != null) { + mg.getInstructionList().setPositions(); + mg.setMaxLocals(); + mg.setMaxStack(); + } + cg.replaceMethod(m, mg.getMethod()); + } + + bh.consume(cg.getJavaClass().getBytes()); + } + + jar.close(); + } +} diff --git a/src/test/java/org/apache/bcel/CounterVisitorTestCase.java b/src/test/java/org/apache/bcel/CounterVisitorTestCase.java new file mode 100644 index 00000000..83284183 --- /dev/null +++ b/src/test/java/org/apache/bcel/CounterVisitorTestCase.java @@ -0,0 +1,215 @@ +/* + * 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; + +import org.apache.bcel.classfile.JavaClass; + +public class CounterVisitorTestCase extends AbstractCounterVisitorTestCase +{ + @Override + protected JavaClass getTestClass() throws ClassNotFoundException + { + return getTestClass(PACKAGE_BASE_NAME+".data.MarkedType"); + } + + public void testAnnotationsCount() + { + assertEquals("annotationCount", 2, getVisitor().annotationCount); + } + + public void testAnnotationDefaultCount() + { + assertEquals("annotationDefaultCount", 0, getVisitor().annotationDefaultCount); + } + + public void testAnnotationEntryCount() + { + assertEquals("annotationEntryCount", 2, getVisitor().annotationEntryCount); + } + + public void testCodeCount() + { + assertEquals("codeCount", 1, getVisitor().codeCount); + } + + public void testCodeExceptionCount() + { + assertEquals("codeExceptionCount", 0, getVisitor().codeExceptionCount); + } + + public void testConstantClassCount() + { + assertEquals("constantClassCount", 2, getVisitor().constantClassCount); + } + + public void testConstantDoubleCount() + { + assertEquals("constantDoubleCount", 0, getVisitor().constantDoubleCount); + } + + public void testConstantFieldrefCount() + { + assertEquals("constantFieldrefCount", 0, getVisitor().constantFieldrefCount); + } + + public void testConstantFloatCount() + { + assertEquals("constantFloatCount", 0, getVisitor().constantFloatCount); + } + + public void testConstantIntegerCount() + { + assertEquals("constantIntegerCount", 0, getVisitor().constantIntegerCount); + } + + public void testConstantInterfaceMethodrefCount() + { + assertEquals("constantInterfaceMethodrefCount", 0, getVisitor().constantInterfaceMethodrefCount); + } + + public void testConstantLongCount() + { + assertEquals("constantLongCount", 0, getVisitor().constantLongCount); + } + + public void testConstantMethodrefCount() + { + assertEquals("constantMethodrefCount", 1, getVisitor().constantMethodrefCount); + } + + public void testConstantNameAndTypeCount() + { + assertEquals("constantNameAndTypeCount", 1, getVisitor().constantNameAndTypeCount); + } + + public void testConstantPoolCount() + { + assertEquals("constantPoolCount", 1, getVisitor().constantPoolCount); + } + + public void testConstantStringCount() + { + assertEquals("constantStringCount", 0, getVisitor().constantStringCount); + } + + public void testConstantValueCount() + { + assertEquals("constantValueCount", 0, getVisitor().constantValueCount); + } + + public void testDeprecatedCount() + { + assertEquals("deprecatedCount", 0, getVisitor().deprecatedCount); + } + + public void testEnclosingMethodCount() + { + assertEquals("enclosingMethodCount", 0, getVisitor().enclosingMethodCount); + } + + public void testExceptionTableCount() + { + assertEquals("exceptionTableCount", 0, getVisitor().exceptionTableCount); + } + + public void testFieldCount() + { + assertEquals("fieldCount", 0, getVisitor().fieldCount); + } + + public void testInnerClassCount() + { + assertEquals("innerClassCount", 0, getVisitor().innerClassCount); + } + + public void testInnerClassesCount() + { + assertEquals("innerClassesCount", 0, getVisitor().innerClassesCount); + } + + public void testJavaClassCount() + { + assertEquals("javaClassCount", 1, getVisitor().javaClassCount); + } + + public void testLineNumberCount() + { + assertEquals("lineNumberCount", 1, getVisitor().lineNumberCount); + } + + public void testLineNumberTableCount() + { + assertEquals("lineNumberTableCount", 1, getVisitor().lineNumberTableCount); + } + + public void testLocalVariableCount() + { + assertEquals("localVariableCount", 1, getVisitor().localVariableCount); + } + + public void testLocalVariableTableCount() + { + assertEquals("localVariableTableCount", 1, getVisitor().localVariableTableCount); + } + + public void testLocalVariableTypeTableCount() + { + assertEquals("localVariableTypeTableCount", 0, getVisitor().localVariableTypeTableCount); + } + + public void testMethodCount() + { + assertEquals("methodCount", 1, getVisitor().methodCount); + } + + public void testParameterAnnotationCount() + { + assertEquals("parameterAnnotationCount", 0, getVisitor().parameterAnnotationCount); + } + + public void testSignatureCount() + { + assertEquals("signatureAnnotationCount", 0, getVisitor().signatureAnnotationCount); + } + + public void testSourceFileCount() + { + assertEquals("sourceFileCount", 1, getVisitor().sourceFileCount); + } + + public void testStackMapCount() + { + assertEquals("stackMapCount", 0, getVisitor().stackMapCount); + } + + public void testStackMapEntryCount() + { + assertEquals("stackMapEntryCount", 0, getVisitor().stackMapEntryCount); + } + + public void testSyntheticCount() + { + assertEquals("syntheticCount", 0, getVisitor().syntheticCount); + } + + public void testUnknownCount() + { + assertEquals("unknownCount", 0, getVisitor().unknownCount); + } +} diff --git a/src/test/java/org/apache/bcel/ElementValueGenTestCase.java b/src/test/java/org/apache/bcel/ElementValueGenTestCase.java new file mode 100644 index 00000000..ac2d88b6 --- /dev/null +++ b/src/test/java/org/apache/bcel/ElementValueGenTestCase.java @@ -0,0 +1,240 @@ +/* + * 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; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +import org.apache.bcel.generic.ClassElementValueGen; +import org.apache.bcel.generic.ClassGen; +import org.apache.bcel.generic.ConstantPoolGen; +import org.apache.bcel.generic.ElementValueGen; +import org.apache.bcel.generic.EnumElementValueGen; +import org.apache.bcel.generic.ObjectType; +import org.apache.bcel.generic.SimpleElementValueGen; + +public class ElementValueGenTestCase extends AbstractTestCase +{ + private ClassGen createClassGen(final String classname) + { + return new ClassGen(classname, "java.lang.Object", "", + Const.ACC_PUBLIC | Const.ACC_SUPER, null); + } + + /** + * Create primitive element values + */ + public void testCreateIntegerElementValue() + { + ClassGen cg = createClassGen("HelloWorld"); + ConstantPoolGen cp = cg.getConstantPool(); + SimpleElementValueGen evg = new SimpleElementValueGen( + ElementValueGen.PRIMITIVE_INT, cp, 555); + // Creation of an element like that should leave a new entry in the + // cpool + assertTrue("Should have the same index in the constantpool but " + + evg.getIndex() + "!=" + cp.lookupInteger(555), + evg.getIndex() == cp.lookupInteger(555)); + checkSerialize(evg, cp); + } + + public void testCreateFloatElementValue() + { + ClassGen cg = createClassGen("HelloWorld"); + ConstantPoolGen cp = cg.getConstantPool(); + SimpleElementValueGen evg = new SimpleElementValueGen( + ElementValueGen.PRIMITIVE_FLOAT, cp, 111.222f); + // Creation of an element like that should leave a new entry in the + // cpool + assertTrue("Should have the same index in the constantpool but " + + evg.getIndex() + "!=" + cp.lookupFloat(111.222f), evg + .getIndex() == cp.lookupFloat(111.222f)); + checkSerialize(evg, cp); + } + + public void testCreateDoubleElementValue() + { + ClassGen cg = createClassGen("HelloWorld"); + ConstantPoolGen cp = cg.getConstantPool(); + SimpleElementValueGen evg = new SimpleElementValueGen( + ElementValueGen.PRIMITIVE_DOUBLE, cp, 333.44); + // Creation of an element like that should leave a new entry in the + // cpool + int idx = cp.lookupDouble(333.44); + assertTrue("Should have the same index in the constantpool but " + + evg.getIndex() + "!=" + idx, evg.getIndex() == idx); + checkSerialize(evg, cp); + } + + public void testCreateLongElementValue() + { + ClassGen cg = createClassGen("HelloWorld"); + ConstantPoolGen cp = cg.getConstantPool(); + SimpleElementValueGen evg = new SimpleElementValueGen( + ElementValueGen.PRIMITIVE_LONG, cp, 3334455L); + // Creation of an element like that should leave a new entry in the + // cpool + int idx = cp.lookupLong(3334455L); + assertTrue("Should have the same index in the constantpool but " + + evg.getIndex() + "!=" + idx, evg.getIndex() == idx); + checkSerialize(evg, cp); + } + + public void testCreateCharElementValue() + { + ClassGen cg = createClassGen("HelloWorld"); + ConstantPoolGen cp = cg.getConstantPool(); + SimpleElementValueGen evg = new SimpleElementValueGen( + ElementValueGen.PRIMITIVE_CHAR, cp, 't'); + // Creation of an element like that should leave a new entry in the + // cpool + int idx = cp.lookupInteger('t'); + assertTrue("Should have the same index in the constantpool but " + + evg.getIndex() + "!=" + idx, evg.getIndex() == idx); + checkSerialize(evg, cp); + } + + public void testCreateByteElementValue() + { + ClassGen cg = createClassGen("HelloWorld"); + ConstantPoolGen cp = cg.getConstantPool(); + SimpleElementValueGen evg = new SimpleElementValueGen( + ElementValueGen.PRIMITIVE_CHAR, cp, (byte) 'z'); + // Creation of an element like that should leave a new entry in the + // cpool + int idx = cp.lookupInteger((byte) 'z'); + assertTrue("Should have the same index in the constantpool but " + + evg.getIndex() + "!=" + idx, evg.getIndex() == idx); + checkSerialize(evg, cp); + } + + public void testCreateBooleanElementValue() + { + ClassGen cg = createClassGen("HelloWorld"); + ConstantPoolGen cp = cg.getConstantPool(); + SimpleElementValueGen evg = new SimpleElementValueGen( + ElementValueGen.PRIMITIVE_BOOLEAN, cp, true); + // Creation of an element like that should leave a new entry in the + // cpool + int idx = cp.lookupInteger(1); // 1 == true + assertTrue("Should have the same index in the constantpool but " + + evg.getIndex() + "!=" + idx, evg.getIndex() == idx); + checkSerialize(evg, cp); + } + + public void testCreateShortElementValue() + { + ClassGen cg = createClassGen("HelloWorld"); + ConstantPoolGen cp = cg.getConstantPool(); + SimpleElementValueGen evg = new SimpleElementValueGen( + ElementValueGen.PRIMITIVE_SHORT, cp, (short) 42); + // Creation of an element like that should leave a new entry in the + // cpool + int idx = cp.lookupInteger(42); + assertTrue("Should have the same index in the constantpool but " + + evg.getIndex() + "!=" + idx, evg.getIndex() == idx); + checkSerialize(evg, cp); + } + + // // + // Create string element values + public void testCreateStringElementValue() + { + // Create HelloWorld + ClassGen cg = createClassGen("HelloWorld"); + ConstantPoolGen cp = cg.getConstantPool(); + SimpleElementValueGen evg = new SimpleElementValueGen( + ElementValueGen.STRING, cp, "hello"); + // Creation of an element like that should leave a new entry in the + // cpool + assertTrue("Should have the same index in the constantpool but " + + evg.getIndex() + "!=" + cp.lookupUtf8("hello"), evg + .getIndex() == cp.lookupUtf8("hello")); + checkSerialize(evg, cp); + } + + // // + // Create enum element value + public void testCreateEnumElementValue() + { + ClassGen cg = createClassGen("HelloWorld"); + ConstantPoolGen cp = cg.getConstantPool(); + ObjectType enumType = new ObjectType("SimpleEnum"); // Supports rainbow + // :) + EnumElementValueGen evg = new EnumElementValueGen(enumType, "Red", cp); + // Creation of an element like that should leave a new entry in the + // cpool + assertTrue( + "The new ElementValue value index should match the contents of the constantpool but " + + evg.getValueIndex() + "!=" + cp.lookupUtf8("Red"), + evg.getValueIndex() == cp.lookupUtf8("Red")); + // BCELBUG: Should the class signature or class name be in the constant + // pool? (see note in ConstantPool) + // assertTrue("The new ElementValue type index should match the contents + // of the constantpool but "+ + // evg.getTypeIndex()+"!="+cp.lookupClass(enumType.getSignature()), + // evg.getTypeIndex()==cp.lookupClass(enumType.getSignature())); + checkSerialize(evg, cp); + } + + // // + // Create class element value + public void testCreateClassElementValue() + { + ClassGen cg = createClassGen("HelloWorld"); + ConstantPoolGen cp = cg.getConstantPool(); + ObjectType classType = new ObjectType("java.lang.Integer"); + ClassElementValueGen evg = new ClassElementValueGen(classType, cp); + assertTrue("Unexpected value for contained class: '" + + evg.getClassString() + "'", evg.getClassString().contains("Integer")); + checkSerialize(evg, cp); + } + + private void checkSerialize(final ElementValueGen evgBefore, final ConstantPoolGen cpg) + { + try + { + String beforeValue = evgBefore.stringifyValue(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(baos); + evgBefore.dump(dos); + dos.flush(); + dos.close(); + byte[] bs = baos.toByteArray(); + ByteArrayInputStream bais = new ByteArrayInputStream(bs); + DataInputStream dis = new DataInputStream(bais); + ElementValueGen evgAfter = ElementValueGen.readElementValue(dis, + cpg); + dis.close(); + String afterValue = evgAfter.stringifyValue(); + if (!beforeValue.equals(afterValue)) + { + fail("Deserialization failed: before='" + beforeValue + + "' after='" + afterValue + "'"); + } + } + catch (IOException ioe) + { + fail("Unexpected exception whilst checking serialization: " + ioe); + } + } +} diff --git a/src/test/java/org/apache/bcel/EnclosingMethodAttributeTestCase.java b/src/test/java/org/apache/bcel/EnclosingMethodAttributeTestCase.java new file mode 100644 index 00000000..80f0c36b --- /dev/null +++ b/src/test/java/org/apache/bcel/EnclosingMethodAttributeTestCase.java @@ -0,0 +1,110 @@ +/* + * 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; + +import java.io.File; +import java.io.IOException; + +import org.apache.bcel.classfile.Attribute; +import org.apache.bcel.classfile.ConstantPool; +import org.apache.bcel.classfile.EnclosingMethod; +import org.apache.bcel.classfile.JavaClass; +import org.apache.bcel.util.SyntheticRepository; +import org.junit.Assert; + +public class EnclosingMethodAttributeTestCase extends AbstractTestCase +{ + /** + * Verify for an inner class declared inside the 'main' method that the + * enclosing method attribute is set correctly. + */ + public void testCheckMethodLevelNamedInnerClass() + throws ClassNotFoundException + { + JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.AttributeTestClassEM01$1S"); + ConstantPool pool = clazz.getConstantPool(); + Attribute[] encMethodAttrs = findAttribute("EnclosingMethod", clazz); + assertTrue("Expected 1 EnclosingMethod attribute but found " + + encMethodAttrs.length, encMethodAttrs.length == 1); + EnclosingMethod em = (EnclosingMethod) encMethodAttrs[0]; + String enclosingClassName = em.getEnclosingClass().getBytes(pool); + String enclosingMethodName = em.getEnclosingMethod().getName(pool); + assertTrue( + "Expected class name to be '"+PACKAGE_BASE_SIG+"/data/AttributeTestClassEM01' but was " + + enclosingClassName, enclosingClassName + .equals(PACKAGE_BASE_SIG+"/data/AttributeTestClassEM01")); + assertTrue("Expected method name to be 'main' but was " + + enclosingMethodName, enclosingMethodName.equals("main")); + } + + /** + * Verify for an inner class declared at the type level that the + * EnclosingMethod attribute is set correctly (i.e. to a null value) + */ + public void testCheckClassLevelNamedInnerClass() + throws ClassNotFoundException + { + JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.AttributeTestClassEM02$1"); + ConstantPool pool = clazz.getConstantPool(); + Attribute[] encMethodAttrs = findAttribute("EnclosingMethod", clazz); + assertTrue("Expected 1 EnclosingMethod attribute but found " + + encMethodAttrs.length, encMethodAttrs.length == 1); + EnclosingMethod em = (EnclosingMethod) encMethodAttrs[0]; + String enclosingClassName = em.getEnclosingClass().getBytes(pool); + assertTrue( + "The class is not within a method, so method_index should be null, but it is " + + em.getEnclosingMethodIndex(), em + .getEnclosingMethodIndex() == 0); + assertTrue( + "Expected class name to be '"+PACKAGE_BASE_SIG+"/data/AttributeTestClassEM02' but was " + + enclosingClassName, enclosingClassName + .equals(PACKAGE_BASE_SIG+"/data/AttributeTestClassEM02")); + } + + /** + * Check that we can save and load the attribute correctly. + */ + public void testAttributeSerializtion() throws ClassNotFoundException, + IOException + { + JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.AttributeTestClassEM02$1"); + ConstantPool pool = clazz.getConstantPool(); + Attribute[] encMethodAttrs = findAttribute("EnclosingMethod", clazz); + assertTrue("Expected 1 EnclosingMethod attribute but found " + + encMethodAttrs.length, encMethodAttrs.length == 1); + // Write it out + File tfile = createTestdataFile("AttributeTestClassEM02$1.class"); + clazz.dump(tfile); + // Read in the new version and check it is OK + SyntheticRepository repos2 = createRepos("."); + JavaClass clazz2 = repos2.loadClass("AttributeTestClassEM02$1"); + Assert.assertNotNull(clazz2); // Use the variable to avoid a warning + EnclosingMethod em = (EnclosingMethod) encMethodAttrs[0]; + String enclosingClassName = em.getEnclosingClass().getBytes(pool); + assertTrue( + "The class is not within a method, so method_index should be null, but it is " + + em.getEnclosingMethodIndex(), em + .getEnclosingMethodIndex() == 0); + assertTrue( + "Expected class name to be '"+PACKAGE_BASE_SIG+"/data/AttributeTestClassEM02' but was " + + enclosingClassName, enclosingClassName + .equals(PACKAGE_BASE_SIG+"/data/AttributeTestClassEM02")); + tfile.deleteOnExit(); + } +} diff --git a/src/test/java/org/apache/bcel/EnumAccessFlagTestCase.java b/src/test/java/org/apache/bcel/EnumAccessFlagTestCase.java new file mode 100644 index 00000000..fc624bb7 --- /dev/null +++ b/src/test/java/org/apache/bcel/EnumAccessFlagTestCase.java @@ -0,0 +1,41 @@ +/* + * 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; + +import org.apache.bcel.classfile.JavaClass; + +public class EnumAccessFlagTestCase extends AbstractTestCase +{ + /** + * An enumerated type, once compiled, should result in a class file that is + * marked such that we can determine from the access flags (through BCEL) + * that it was originally an enum type declaration. + */ + public void testEnumClassSaysItIs() throws ClassNotFoundException + { + JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.SimpleEnum"); + assertTrue( + "Expected SimpleEnum class to say it was an enum - but it didn't !", + clazz.isEnum()); + clazz = getTestClass(PACKAGE_BASE_NAME+".data.SimpleClass"); + assertTrue( + "Expected SimpleClass class to say it was not an enum - but it didn't !", + !clazz.isEnum()); + } +} diff --git a/src/test/java/org/apache/bcel/InstructionFinderTestCase.java b/src/test/java/org/apache/bcel/InstructionFinderTestCase.java new file mode 100644 index 00000000..6feb1911 --- /dev/null +++ b/src/test/java/org/apache/bcel/InstructionFinderTestCase.java @@ -0,0 +1,62 @@ +/* + * 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; + +import java.util.Iterator; + +import org.apache.bcel.classfile.JavaClass; +import org.apache.bcel.classfile.Method; +import org.apache.bcel.generic.InstructionHandle; +import org.apache.bcel.generic.InstructionList; +import org.apache.bcel.util.InstructionFinder; + +public class InstructionFinderTestCase extends AbstractTestCase +{ + public void testSearchAll() throws Exception + { + JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".util.InstructionFinder"); + Method[] methods = clazz.getMethods(); + Method searchM = null; + for (Method m : methods) + { + if (m.getName().equals("search") && (m.getArgumentTypes().length == 3)) + { + searchM = m; + break; + } + } + + if (searchM == null) { + throw new Exception("search method not found"); + } + + byte[] bytes = searchM.getCode().getCode(); + InstructionList il = new InstructionList(bytes); + InstructionFinder finder = new InstructionFinder(il); + Iterator it = finder.search(".*", il.getStart(), null); + + InstructionHandle[] ihs = (InstructionHandle[])it.next(); + int size = 0; + for (InstructionHandle ih : ihs) + { + size += ih.getInstruction().getLength(); + } + assertEquals(bytes.length, size); + + } +} diff --git a/src/test/java/org/apache/bcel/NanoTimer.java b/src/test/java/org/apache/bcel/NanoTimer.java new file mode 100644 index 00000000..c6e57a02 --- /dev/null +++ b/src/test/java/org/apache/bcel/NanoTimer.java @@ -0,0 +1,52 @@ +/* + * 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; + +public class NanoTimer { + + private long time = 0; + + public NanoTimer start() { + time -= System.nanoTime(); + return this; + } + + public void stop() { + time += System.nanoTime(); + } + + public void subtract(final NanoTimer o) { + time -= o.time; + } + + public void reset() { + time = 0; + } + + /** + * May ony be called after stop has been called as many times as start. + */ + @Override + public String toString() { + return ((double) time / 1000000000) + " s"; + } + + + +} diff --git a/src/test/java/org/apache/bcel/PLSETestCase.java b/src/test/java/org/apache/bcel/PLSETestCase.java new file mode 100644 index 00000000..f30a5feb --- /dev/null +++ b/src/test/java/org/apache/bcel/PLSETestCase.java @@ -0,0 +1,65 @@ +/* + * 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; + +import org.apache.bcel.classfile.JavaClass; +import org.apache.bcel.classfile.LocalVariableTable; +import org.apache.bcel.classfile.Method; +import org.apache.bcel.generic.ClassGen; +import org.apache.bcel.generic.ConstantPoolGen; +import org.apache.bcel.generic.MethodGen; +import org.apache.bcel.generic.Type; + +public class PLSETestCase extends AbstractTestCase +{ + /** + * BCEL-208: A couple of methods in MethodGen.java need to test for + * an empty instruction list. + */ + public void testB208() throws ClassNotFoundException + { + JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.PLSETestClass"); + ClassGen gen = new ClassGen(clazz); + ConstantPoolGen pool = gen.getConstantPool(); + Method m = gen.getMethodAt(1); + MethodGen mg = new MethodGen(m, gen.getClassName(), pool); + mg.setInstructionList(null); + mg.addLocalVariable("local2", Type.INT, null, null); + // currently, this will cause null pointer exception + mg.getLocalVariableTable(pool); + } + + /** + * BCEL-79: + */ + public void testB79() throws ClassNotFoundException + { + JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.PLSETestClass"); + ClassGen gen = new ClassGen(clazz); + ConstantPoolGen pool = gen.getConstantPool(); + Method m = gen.getMethodAt(2); + LocalVariableTable lvt = m.getLocalVariableTable(); + //System.out.println(lvt); + //System.out.println(lvt.getTableLength()); + MethodGen mg = new MethodGen(m, gen.getClassName(), pool); + LocalVariableTable new_lvt = mg.getLocalVariableTable(mg.getConstantPool()); + //System.out.println(new_lvt); + assertEquals("number of locals", lvt.getTableLength(), new_lvt.getTableLength()); + } +} diff --git a/src/test/java/org/apache/bcel/PerformanceTest.java b/src/test/java/org/apache/bcel/PerformanceTest.java new file mode 100644 index 00000000..abd4f8ae --- /dev/null +++ b/src/test/java/org/apache/bcel/PerformanceTest.java @@ -0,0 +1,152 @@ +/* + * 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; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileFilter; +import java.io.IOException; +import java.io.InputStream; +import java.util.Enumeration; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; + +import org.apache.bcel.classfile.ClassParser; +import org.apache.bcel.classfile.JavaClass; +import org.apache.bcel.classfile.Method; +import org.apache.bcel.generic.ClassGen; +import org.apache.bcel.generic.InstructionList; +import org.apache.bcel.generic.MethodGen; +import org.junit.Assert; + +import junit.framework.TestCase; + +public final class PerformanceTest extends TestCase { + + private static final boolean REPORT = Boolean.parseBoolean(System.getProperty("PerformanceTest.report", "true"));; + + private static byte[] read(final InputStream is) throws IOException { + if (is == null) { + throw new IOException("Class not found"); + } + byte[] b = new byte[is.available()]; + int len = 0; + while (true) { + int n = is.read(b, len, b.length - len); + if (n == -1) { + if (len < b.length) { + byte[] c = new byte[len]; + System.arraycopy(b, 0, c, 0, len); + b = c; + } + return b; + } + len += n; + if (len == b.length) { + byte[] c = new byte[b.length + 1000]; + System.arraycopy(b, 0, c, 0, len); + b = c; + } + } + } + + private static void test(final File lib) throws IOException { + NanoTimer total = new NanoTimer(); + NanoTimer parseTime = new NanoTimer(); + NanoTimer cgenTime = new NanoTimer(); + NanoTimer mgenTime = new NanoTimer(); + NanoTimer mserTime = new NanoTimer(); + NanoTimer serTime = new NanoTimer(); + + System.out.println("parsing " + lib); + + total.start(); + JarFile jar = new JarFile(lib); + Enumeration en = jar.entries(); + + while (en.hasMoreElements()) { + JarEntry e = (JarEntry) en.nextElement(); + if (e.getName().endsWith(".class")) { + InputStream in = jar.getInputStream(e); + byte[] bytes = read(in); + + parseTime.start(); + JavaClass clazz = new ClassParser(new ByteArrayInputStream(bytes), e.getName()) + .parse(); + parseTime.stop(); + + cgenTime.start(); + ClassGen cg = new ClassGen(clazz); + cgenTime.stop(); + + Method[] methods = cg.getMethods(); + for (Method m : methods) { + mgenTime.start(); + MethodGen mg = new MethodGen(m, cg.getClassName(), cg.getConstantPool()); + InstructionList il = mg.getInstructionList(); + mgenTime.stop(); + + mserTime.start(); + if (il != null) { + mg.getInstructionList().setPositions(); + mg.setMaxLocals(); + mg.setMaxStack(); + } + cg.replaceMethod(m, mg.getMethod()); + mserTime.stop(); + } + + serTime.start(); + cg.getJavaClass().getBytes(); + serTime.stop(); + } + } + + jar.close(); + total.stop(); + if (REPORT) { + System.out.println("ClassParser.parse: " + parseTime); + System.out.println("ClassGen.init: " + cgenTime); + System.out.println("MethodGen.init: " + mgenTime); + System.out.println("MethodGen.getMethod: " + mserTime); + System.out.println("ClassGen.getJavaClass.getBytes: " + serTime); + System.out.println("Total: " + total); + System.out.println(); + } + } + + public void testPerformance() throws IOException { + File javaLib = new File(System.getProperty("java.home") + "/lib"); + javaLib.listFiles(new FileFilter() { + + @Override + public boolean accept(final File file) { + if(file.getName().endsWith(".jar")) { + try { + test(file); + } catch (IOException e) { + Assert.fail(e.getMessage()); + } + } + return false; + } + }); + } + +} diff --git a/src/test/java/org/apache/bcel/classfile/JDKClassDumpTestCase.java b/src/test/java/org/apache/bcel/classfile/JDKClassDumpTestCase.java new file mode 100644 index 00000000..277b4bd9 --- /dev/null +++ b/src/test/java/org/apache/bcel/classfile/JDKClassDumpTestCase.java @@ -0,0 +1,95 @@ +/* + * 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.commons.bcel6.classfile; + +import static org.junit.Assert.assertEquals; + +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.File; +import java.io.FileFilter; +import java.io.InputStream; +import java.util.Enumeration; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Test that dump() methods work on the JDK classes + */ +public class JDKClassDumpTestCase { + + @Test + public void testPerformance() throws Exception { + File javaLib = new File(System.getProperty("java.home") + "/lib"); + javaLib.listFiles(new FileFilter() { + + @Override + public boolean accept(final File file) { + if(file.getName().endsWith(".jar")) { + try { + testJar(file); + } catch (Exception e) { + Assert.fail(e.getMessage()); + } + } + return false; + } + }); + } + + + private void testJar(final File file) throws Exception { + System.out.println("parsing " + file); + JarFile jar = new JarFile(file); + Enumeration en = jar.entries(); + + while (en.hasMoreElements()) { + JarEntry e = en.nextElement(); + final String name = e.getName(); + if (name.endsWith(".class")) { +// System.out.println("parsing " + name); + InputStream in = jar.getInputStream(e); + ClassParser parser = new ClassParser(in, name); + JavaClass jc = parser.parse(); + compare(jc, jar.getInputStream(e), name); + } + } + jar.close(); + } + + private void compare(final JavaClass jc, final InputStream inputStream, final String name) throws Exception { + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(baos); + jc.dump(dos); + dos.close(); + DataInputStream src = new DataInputStream(inputStream); + int i=0; + for(int out : baos.toByteArray()) { + int in = src.read(); + assertEquals(name + ": Mismatch at "+i, in, out&0xFF); + i++; + } + src.close(); + } + + +} diff --git a/src/test/java/org/apache/bcel/classfile/UtilityTestCase.java b/src/test/java/org/apache/bcel/classfile/UtilityTestCase.java new file mode 100644 index 00000000..6a0394ac --- /dev/null +++ b/src/test/java/org/apache/bcel/classfile/UtilityTestCase.java @@ -0,0 +1,35 @@ +/* + * 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.commons.bcel6.classfile; + +import junit.framework.TestCase; + +public class UtilityTestCase extends TestCase { + + public void testSignatureToStringWithGenerics() throws Exception { + assertEquals("generic signature", + "java.util.Map>", + Utility.signatureToString("Ljava/util/Map;>;")); + assertEquals("generic signature", + "java.util.Set" + , Utility.signatureToString("Ljava/util/Set<+Ljava/nio/file/OpenOption;>;")); + assertEquals("generic signature", + "java.nio.file.attribute.FileAttribute...[]", + Utility.signatureToString("[Ljava/nio/file/attribute/FileAttribute<*>;")); + } +} diff --git a/src/test/java/org/apache/bcel/data/AnnotatedFields.java b/src/test/java/org/apache/bcel/data/AnnotatedFields.java new file mode 100644 index 00000000..d8391a0d --- /dev/null +++ b/src/test/java/org/apache/bcel/data/AnnotatedFields.java @@ -0,0 +1,25 @@ +/* + * 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.commons.bcel6.data; + +public class AnnotatedFields { + @SimpleAnnotation(id=1) int i; + + @SimpleAnnotation(id=2) String s; +} diff --git a/src/test/java/org/apache/bcel/data/AnnotatedWithCombinedAnnotation.java b/src/test/java/org/apache/bcel/data/AnnotatedWithCombinedAnnotation.java new file mode 100644 index 00000000..e514ad0b --- /dev/null +++ b/src/test/java/org/apache/bcel/data/AnnotatedWithCombinedAnnotation.java @@ -0,0 +1,34 @@ +/* + * 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.commons.bcel6.data; + +@CombinedAnnotation( { @SimpleAnnotation(id = 4) }) +public class AnnotatedWithCombinedAnnotation +{ + public AnnotatedWithCombinedAnnotation(final int param1, @SimpleAnnotation(id=42) final int param2) { + } + + @CombinedAnnotation( {}) + public void methodWithArrayOfZeroAnnotations() { + } + + @CombinedAnnotation( { @SimpleAnnotation(id=1, fruit="apples"), @SimpleAnnotation(id= 2, fruit="oranges")}) + public void methodWithArrayOfTwoAnnotations() { + } +} diff --git a/src/test/java/org/apache/bcel/data/AnnotatedWithEnumClass.java b/src/test/java/org/apache/bcel/data/AnnotatedWithEnumClass.java new file mode 100644 index 00000000..8aad494a --- /dev/null +++ b/src/test/java/org/apache/bcel/data/AnnotatedWithEnumClass.java @@ -0,0 +1,24 @@ +/* + * 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.commons.bcel6.data; + +@AnnotationEnumElement(enumval = SimpleEnum.Red) +public class AnnotatedWithEnumClass +{ +} diff --git a/src/test/java/org/apache/bcel/data/AnnotationEnumElement.java b/src/test/java/org/apache/bcel/data/AnnotationEnumElement.java new file mode 100644 index 00000000..63b333d5 --- /dev/null +++ b/src/test/java/org/apache/bcel/data/AnnotationEnumElement.java @@ -0,0 +1,28 @@ +/* + * 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.commons.bcel6.data; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Retention(RetentionPolicy.RUNTIME) +public @interface AnnotationEnumElement +{ + SimpleEnum enumval(); +} diff --git a/src/test/java/org/apache/bcel/data/AnonymousClassTest.java b/src/test/java/org/apache/bcel/data/AnonymousClassTest.java new file mode 100644 index 00000000..070451f7 --- /dev/null +++ b/src/test/java/org/apache/bcel/data/AnonymousClassTest.java @@ -0,0 +1,41 @@ +/* + * 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.commons.bcel6.data; + +public class AnonymousClassTest +{ + public void foo() + { + new Runnable() + { + @Override + public void run() + { + } + }.run(); + } + + class X + { + } + + static class Y + { + } +} diff --git a/src/test/java/org/apache/bcel/data/AttributeTestClassEM01.java b/src/test/java/org/apache/bcel/data/AttributeTestClassEM01.java new file mode 100644 index 00000000..c4ea20b2 --- /dev/null +++ b/src/test/java/org/apache/bcel/data/AttributeTestClassEM01.java @@ -0,0 +1,34 @@ +/* + * 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.commons.bcel6.data; + +public class AttributeTestClassEM01 +{ + public static void main(final String[] argv) + { + @SuppressWarnings("unused") + class S + { + public void sayhello() + { + System.err.println("hello"); + } + } + } +} diff --git a/src/test/java/org/apache/bcel/data/AttributeTestClassEM02.java b/src/test/java/org/apache/bcel/data/AttributeTestClassEM02.java new file mode 100644 index 00000000..16c85eaa --- /dev/null +++ b/src/test/java/org/apache/bcel/data/AttributeTestClassEM02.java @@ -0,0 +1,35 @@ +/* + * 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.commons.bcel6.data; + +public class AttributeTestClassEM02 +{ + Runnable r = new Runnable() + { + @Override + public void run() + { + System.err.println("hello"); + } + }; + + public static void main(final String[] argv) + { + } +} diff --git a/src/test/java/org/apache/bcel/data/CombinedAnnotation.java b/src/test/java/org/apache/bcel/data/CombinedAnnotation.java new file mode 100644 index 00000000..efcbeb71 --- /dev/null +++ b/src/test/java/org/apache/bcel/data/CombinedAnnotation.java @@ -0,0 +1,28 @@ +/* + * 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.commons.bcel6.data; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Retention(RetentionPolicy.RUNTIME) +public @interface CombinedAnnotation +{ + public SimpleAnnotation[] value(); +} diff --git a/src/test/java/org/apache/bcel/data/ComplexAnnotatedClass.java b/src/test/java/org/apache/bcel/data/ComplexAnnotatedClass.java new file mode 100644 index 00000000..51748c51 --- /dev/null +++ b/src/test/java/org/apache/bcel/data/ComplexAnnotatedClass.java @@ -0,0 +1,24 @@ +/* + * 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.commons.bcel6.data; + +@ComplexAnnotation(ival = 4, bval = 2, cval = '5', fval = 3.0f, dval = 33.4, zval = false, jval = 56, sval = 99) +public class ComplexAnnotatedClass +{ +} diff --git a/src/test/java/org/apache/bcel/data/ComplexAnnotation.java b/src/test/java/org/apache/bcel/data/ComplexAnnotation.java new file mode 100644 index 00000000..39f0be45 --- /dev/null +++ b/src/test/java/org/apache/bcel/data/ComplexAnnotation.java @@ -0,0 +1,42 @@ +/* + * 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.commons.bcel6.data; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Retention(RetentionPolicy.RUNTIME) +public @interface ComplexAnnotation +{ + int ival(); + + byte bval(); + + char cval(); + + long jval(); + + double dval(); + + boolean zval(); + + short sval(); + + float fval(); +} diff --git a/src/test/java/org/apache/bcel/data/MarkedType.java b/src/test/java/org/apache/bcel/data/MarkedType.java new file mode 100644 index 00000000..56f97df2 --- /dev/null +++ b/src/test/java/org/apache/bcel/data/MarkedType.java @@ -0,0 +1,25 @@ +/* + * 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.commons.bcel6.data; + +@MarkerAnnotationInvisible +@MarkerAnnotation +public class MarkedType +{ +} diff --git a/src/test/java/org/apache/bcel/data/MarkerAnnotation.java b/src/test/java/org/apache/bcel/data/MarkerAnnotation.java new file mode 100644 index 00000000..714a0a98 --- /dev/null +++ b/src/test/java/org/apache/bcel/data/MarkerAnnotation.java @@ -0,0 +1,27 @@ +/* + * 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.commons.bcel6.data; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Retention(RetentionPolicy.RUNTIME) +public @interface MarkerAnnotation +{ +} diff --git a/src/test/java/org/apache/bcel/data/MarkerAnnotationInvisible.java b/src/test/java/org/apache/bcel/data/MarkerAnnotationInvisible.java new file mode 100644 index 00000000..20563b6b --- /dev/null +++ b/src/test/java/org/apache/bcel/data/MarkerAnnotationInvisible.java @@ -0,0 +1,25 @@ +/* + * 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.commons.bcel6.data; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Retention(RetentionPolicy.CLASS) +public @interface MarkerAnnotationInvisible { } diff --git a/src/test/java/org/apache/bcel/data/PLSETestClass.java b/src/test/java/org/apache/bcel/data/PLSETestClass.java new file mode 100644 index 00000000..6a1cf31d --- /dev/null +++ b/src/test/java/org/apache/bcel/data/PLSETestClass.java @@ -0,0 +1,36 @@ +/* + * 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.commons.bcel6.data; + +import java.util.ArrayList; + +public class PLSETestClass +{ + public void meth1(final int arg1) + { + @SuppressWarnings("unused") + int local1 = arg1; + } + + public void meth2(final int arg1, final ArrayList arg2, final int arg3) + { + @SuppressWarnings("unused") + int local1 = arg1; + } +} diff --git a/src/test/java/org/apache/bcel/data/SimpleAnnotatedClass.java b/src/test/java/org/apache/bcel/data/SimpleAnnotatedClass.java new file mode 100644 index 00000000..37831da4 --- /dev/null +++ b/src/test/java/org/apache/bcel/data/SimpleAnnotatedClass.java @@ -0,0 +1,24 @@ +/* + * 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.commons.bcel6.data; + +@SimpleAnnotation(id = 4) +public class SimpleAnnotatedClass +{ +} diff --git a/src/test/java/org/apache/bcel/data/SimpleAnnotation.java b/src/test/java/org/apache/bcel/data/SimpleAnnotation.java new file mode 100644 index 00000000..4b4140fc --- /dev/null +++ b/src/test/java/org/apache/bcel/data/SimpleAnnotation.java @@ -0,0 +1,30 @@ +/* + * 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.commons.bcel6.data; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Retention(RetentionPolicy.RUNTIME) +public @interface SimpleAnnotation +{ + int id(); + + String fruit() default "bananas"; +} diff --git a/src/test/java/org/apache/bcel/data/SimpleClass.java b/src/test/java/org/apache/bcel/data/SimpleClass.java new file mode 100644 index 00000000..9fa9ac01 --- /dev/null +++ b/src/test/java/org/apache/bcel/data/SimpleClass.java @@ -0,0 +1,27 @@ +/* + * 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.commons.bcel6.data; + +public class SimpleClass +{ + public static void main(final String[] argv) + { + // Nothing unusual in this class + } +} diff --git a/src/test/java/org/apache/bcel/data/SimpleEnum.java b/src/test/java/org/apache/bcel/data/SimpleEnum.java new file mode 100644 index 00000000..743de36a --- /dev/null +++ b/src/test/java/org/apache/bcel/data/SimpleEnum.java @@ -0,0 +1,21 @@ +/* + * 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.commons.bcel6.data; + +public enum SimpleEnum { Red,Orange,Yellow,Green,Blue,Indigo,Violet } diff --git a/src/test/java/org/apache/bcel/generic/AnnotationGenTestCase.java b/src/test/java/org/apache/bcel/generic/AnnotationGenTestCase.java new file mode 100644 index 00000000..0bebb801 --- /dev/null +++ b/src/test/java/org/apache/bcel/generic/AnnotationGenTestCase.java @@ -0,0 +1,166 @@ +/* + * 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.commons.bcel6.generic; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.bcel6.AbstractTestCase; +import org.apache.commons.bcel6.Const; +import org.apache.commons.bcel6.classfile.Annotations; +import org.apache.commons.bcel6.classfile.Attribute; +import org.apache.commons.bcel6.classfile.RuntimeInvisibleAnnotations; +import org.apache.commons.bcel6.classfile.RuntimeVisibleAnnotations; + +public class AnnotationGenTestCase extends AbstractTestCase +{ + private ClassGen createClassGen(final String classname) + { + return new ClassGen(classname, "java.lang.Object", "", + Const.ACC_PUBLIC | Const.ACC_SUPER, null); + } + + /** + * Programmatically construct an mutable annotation (AnnotationGen) object. + */ + public void testConstructMutableAnnotation() + { + // Create the containing class + ClassGen cg = createClassGen("HelloWorld"); + ConstantPoolGen cp = cg.getConstantPool(); + // Create the simple primitive value '4' of type 'int' + SimpleElementValueGen evg = new SimpleElementValueGen( + ElementValueGen.PRIMITIVE_INT, cp, 4); + // Give it a name, call it 'id' + ElementValuePairGen nvGen = new ElementValuePairGen("id", evg, + cp); + // Check it looks right + assertTrue( + "Should include string 'id=4' but says: " + nvGen.toString(), + nvGen.toString().contains("id=4")); + ObjectType t = new ObjectType("SimpleAnnotation"); + List elements = new ArrayList<>(); + elements.add(nvGen); + // Build an annotation of type 'SimpleAnnotation' with 'id=4' as the + // only value :) + AnnotationEntryGen a = new AnnotationEntryGen(t, elements, true, cp); + // Check we can save and load it ok + checkSerialize(a, cp); + } + + public void testVisibleInvisibleAnnotationGen() + { + // Create the containing class + ClassGen cg = createClassGen("HelloWorld"); + ConstantPoolGen cp = cg.getConstantPool(); + // Create the simple primitive value '4' of type 'int' + SimpleElementValueGen evg = new SimpleElementValueGen( + ElementValueGen.PRIMITIVE_INT, cp, 4); + // Give it a name, call it 'id' + ElementValuePairGen nvGen = new ElementValuePairGen("id", evg, + cp); + // Check it looks right + assertTrue( + "Should include string 'id=4' but says: " + nvGen.toString(), + nvGen.toString().contains("id=4")); + ObjectType t = new ObjectType("SimpleAnnotation"); + List elements = new ArrayList<>(); + elements.add(nvGen); + // Build a RV annotation of type 'SimpleAnnotation' with 'id=4' as the + // only value :) + AnnotationEntryGen a = new AnnotationEntryGen(t, elements, true, cp); + List v = new ArrayList<>(); + v.add(a); + Attribute[] attributes = AnnotationEntryGen.getAnnotationAttributes(cp, v.toArray(new AnnotationEntryGen[0])); + boolean foundRV = false; + for (Attribute attribute : attributes) { + if (attribute instanceof RuntimeVisibleAnnotations) + { + assertTrue(((Annotations) attribute).isRuntimeVisible()); + foundRV = true; + } + } + assertTrue("Should have seen a RuntimeVisibleAnnotation", foundRV); + // Build a RIV annotation of type 'SimpleAnnotation' with 'id=4' as the + // only value :) + AnnotationEntryGen a2 = new AnnotationEntryGen(t, elements, false, cp); + List v2 = new ArrayList<>(); + v2.add(a2); + Attribute[] attributes2 = AnnotationEntryGen.getAnnotationAttributes(cp, v2.toArray(new AnnotationEntryGen[0])); + boolean foundRIV = false; + for (Attribute attribute : attributes2) { + if (attribute instanceof RuntimeInvisibleAnnotations) + { + assertFalse(((Annotations) attribute).isRuntimeVisible()); + foundRIV = true; + } + } + assertTrue("Should have seen a RuntimeInvisibleAnnotation", foundRIV); + } + + private void checkSerialize(final AnnotationEntryGen a, final ConstantPoolGen cpg) + { + try + { + String beforeName = a.getTypeName(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(baos); + a.dump(dos); + dos.flush(); + dos.close(); + byte[] bs = baos.toByteArray(); + ByteArrayInputStream bais = new ByteArrayInputStream(bs); + DataInputStream dis = new DataInputStream(bais); + AnnotationEntryGen annAfter = AnnotationEntryGen.read(dis, cpg, a + .isRuntimeVisible()); + dis.close(); + String afterName = annAfter.getTypeName(); + if (!beforeName.equals(afterName)) + { + fail("Deserialization failed: before type='" + beforeName + + "' after type='" + afterName + "'"); + } + if (a.getValues().size() != annAfter.getValues().size()) + { + fail("Different numbers of element name value pairs?? " + + a.getValues().size() + "!=" + + annAfter.getValues().size()); + } + for (int i = 0; i < a.getValues().size(); i++) + { + ElementValuePairGen beforeElement = a.getValues().get(i); + ElementValuePairGen afterElement = annAfter.getValues().get(i); + if (!beforeElement.getNameString().equals( + afterElement.getNameString())) + { + fail("Different names?? " + beforeElement.getNameString() + + "!=" + afterElement.getNameString()); + } + } + } + catch (IOException ioe) + { + fail("Unexpected exception whilst checking serialization: " + ioe); + } + } +} diff --git a/src/test/java/org/apache/bcel/generic/BranchHandleTestCase.java b/src/test/java/org/apache/bcel/generic/BranchHandleTestCase.java new file mode 100644 index 00000000..7c4f8f72 --- /dev/null +++ b/src/test/java/org/apache/bcel/generic/BranchHandleTestCase.java @@ -0,0 +1,37 @@ +package org.apache.commons.bcel6.generic; + +import org.junit.Assert; +import org.junit.Test; + +public class BranchHandleTestCase { + + // Test that setInstruction only allows BranchInstructions + @Test(expected=ClassGenException.class) + public void testsetInstructionNull() { + BranchHandle bh = BranchHandle.getBranchHandle(new GOTO(null));// have to start with a valid BI + Assert.assertNotNull(bh); + bh.setInstruction(null); + Assert.assertNotNull(bh); + } + + @Test + public void testsetInstructionBI() { + BranchHandle bh = BranchHandle.getBranchHandle(new GOTO(null));// have to start with a valid BI + Assert.assertNotNull(bh); + bh.setInstruction(new GOTO(null)); + Assert.assertNotNull(bh); + } + + @Test(expected=ClassGenException.class) + public void testsetInstructionnotBI() { + BranchHandle bh = BranchHandle.getBranchHandle(new GOTO(null));// have to start with a valid BI + Assert.assertNotNull(bh); + bh.setInstruction(new NOP()); + Assert.assertNotNull(bh); + } + + @Test(expected=ClassGenException.class) + public void testGetBHnull() { + BranchHandle.getBranchHandle(null); + } +} diff --git a/src/test/java/org/apache/bcel/generic/FieldAnnotationsTestCase.java b/src/test/java/org/apache/bcel/generic/FieldAnnotationsTestCase.java new file mode 100644 index 00000000..306ecb2b --- /dev/null +++ b/src/test/java/org/apache/bcel/generic/FieldAnnotationsTestCase.java @@ -0,0 +1,167 @@ +/* + * 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.commons.bcel6.generic; + +import java.io.File; +import java.io.IOException; + +import org.apache.commons.bcel6.AbstractTestCase; +import org.apache.commons.bcel6.classfile.AnnotationEntry; +import org.apache.commons.bcel6.classfile.ElementValuePair; +import org.apache.commons.bcel6.classfile.Field; +import org.apache.commons.bcel6.classfile.JavaClass; +import org.apache.commons.bcel6.util.SyntheticRepository; + +public class FieldAnnotationsTestCase extends AbstractTestCase +{ + /** + * Check field AnnotationEntrys are retrievable. + */ + public void testFieldAnnotationEntrys() throws ClassNotFoundException + { + JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.AnnotatedFields"); + // TODO L...;? + checkAnnotatedField(clazz, "i", "L"+PACKAGE_BASE_SIG+"/data/SimpleAnnotation;", "id", "1"); + checkAnnotatedField(clazz, "s", "L"+PACKAGE_BASE_SIG+"/data/SimpleAnnotation;", "id", "2"); + } + + /** + * Check field AnnotationEntrys (de)serialize ok. + */ + public void testFieldAnnotationEntrysReadWrite() throws ClassNotFoundException, + IOException + { + JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.AnnotatedFields"); + checkAnnotatedField(clazz, "i", "L"+PACKAGE_BASE_SIG+"/data/SimpleAnnotation;", "id", "1"); + checkAnnotatedField(clazz, "s", "L"+PACKAGE_BASE_SIG+"/data/SimpleAnnotation;", "id", "2"); + // Write it out + File tfile = createTestdataFile("AnnotatedFields.class"); + clazz.dump(tfile); + SyntheticRepository repos2 = createRepos("."); + repos2.loadClass("AnnotatedFields"); + checkAnnotatedField(clazz, "i", "L"+PACKAGE_BASE_SIG+"/data/SimpleAnnotation;", "id", "1"); + checkAnnotatedField(clazz, "s", "L"+PACKAGE_BASE_SIG+"/data/SimpleAnnotation;", "id", "2"); + assertTrue(tfile.delete()); + } + + /** + * Check we can load in a class, modify its field AnnotationEntrys, save it, + * reload it and everything is correct. + */ + public void testFieldAnnotationModification() + throws ClassNotFoundException + { + boolean dbg = false; + JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.AnnotatedFields"); + ClassGen clg = new ClassGen(clazz); + Field f = clg.getFields()[0]; + if (dbg) { + System.err.println("Field in freshly constructed class is: " + f); + } + if (dbg) { + System.err.println("AnnotationEntrys on field are: " + + dumpAnnotationEntries(f.getAnnotationEntries())); + } + AnnotationEntryGen fruitBasedAnnotationEntry = createFruitAnnotationEntry(clg + .getConstantPool(), "Tomato", false); + FieldGen fg = new FieldGen(f, clg.getConstantPool()); + if (dbg) { + System.err.println("Adding AnnotationEntry to the field"); + } + fg.addAnnotationEntry(fruitBasedAnnotationEntry); + if (dbg) { + System.err.println("FieldGen (mutable field) is " + fg); + } + if (dbg) { + System.err.println("with AnnotationEntrys: " + + dumpAnnotationEntries(fg.getAnnotationEntries())); + } + if (dbg) { + System.err + .println("Replacing original field with new field that has extra AnnotationEntry"); + } + clg.removeField(f); + clg.addField(fg.getField()); + f = clg.getFields()[1]; // there are two fields in the class, removing + // and readding has changed the order + // so this time index [1] is the 'int i' field + if (dbg) { + System.err.println("Field now looks like this: " + f); + } + if (dbg) { + System.err.println("With AnnotationEntrys: " + + dumpAnnotationEntries(f.getAnnotationEntries())); + } + assertTrue("Should be 2 AnnotationEntrys on this field, but there are " + + f.getAnnotationEntries().length, f.getAnnotationEntries().length == 2); + } + + // helper methods + public void checkAnnotatedField(final JavaClass clazz, final String fieldname, + final String AnnotationEntryName, final String AnnotationEntryElementName, + final String AnnotationEntryElementValue) + { + Field[] fields = clazz.getFields(); + for (Field f : fields) { + AnnotationEntry[] fieldAnnotationEntrys = f.getAnnotationEntries(); + if (f.getName().equals(fieldname)) + { + checkAnnotationEntry(fieldAnnotationEntrys[0], AnnotationEntryName, + AnnotationEntryElementName, AnnotationEntryElementValue); + } + } + } + + private void checkAnnotationEntry(final AnnotationEntry a, final String name, final String elementname, + final String elementvalue) + { + assertTrue("Expected AnnotationEntry to have name " + name + + " but it had name " + a.getAnnotationType(), a.getAnnotationType() + .equals(name)); + assertTrue("Expected AnnotationEntry to have one element but it had " + + a.getElementValuePairs().length, a.getElementValuePairs().length == 1); + ElementValuePair envp = a.getElementValuePairs()[0]; + assertTrue("Expected element name " + elementname + " but was " + + envp.getNameString(), elementname + .equals(envp.getNameString())); + assertTrue("Expected element value " + elementvalue + " but was " + + envp.getValue().stringifyValue(), elementvalue.equals(envp + .getValue().stringifyValue())); + } + + // helper methods + public void checkValue(final AnnotationEntry a, final String name, final String tostring) + { + for (int i = 0; i < a.getElementValuePairs().length; i++) + { + ElementValuePair element = a.getElementValuePairs()[i]; + if (element.getNameString().equals(name)) + { + if (!element.getValue().stringifyValue().equals(tostring)) + { + fail("Expected element " + name + " to have value " + + tostring + " but it had value " + + element.getValue().stringifyValue()); + } + return; + } + } + fail("Didnt find named element " + name); + } +} diff --git a/src/test/java/org/apache/bcel/generic/GeneratingAnnotatedClassesTestCase.java b/src/test/java/org/apache/bcel/generic/GeneratingAnnotatedClassesTestCase.java new file mode 100644 index 00000000..b63700d2 --- /dev/null +++ b/src/test/java/org/apache/bcel/generic/GeneratingAnnotatedClassesTestCase.java @@ -0,0 +1,680 @@ +/* + * 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.commons.bcel6.generic; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.bcel6.AbstractTestCase; +import org.apache.commons.bcel6.Const; +import org.apache.commons.bcel6.classfile.AnnotationEntry; +import org.apache.commons.bcel6.classfile.ArrayElementValue; +import org.apache.commons.bcel6.classfile.ElementValue; +import org.apache.commons.bcel6.classfile.ElementValuePair; +import org.apache.commons.bcel6.classfile.JavaClass; +import org.apache.commons.bcel6.classfile.Method; +import org.apache.commons.bcel6.classfile.ParameterAnnotationEntry; +import org.apache.commons.bcel6.classfile.SimpleElementValue; +import org.apache.commons.bcel6.util.SyntheticRepository; + +/** + * The program that some of the tests generate looks like this: + * + *
+ * public class HelloWorld
+ * {
+ *  public static void main(String[] argv)
+ *  {
+ *      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
+ *      String name = null;
+ *
+ *      try
+ *      {
+ *          name = "Andy";
+ *      }
+ *      catch (IOException e)
+ *      {
+ *          return;
+ *      }
+ *      System.out.println("Hello, " + name);
+ *  }
+ * }
+ * 
+ */ +public class GeneratingAnnotatedClassesTestCase extends AbstractTestCase +{ + /** + * Steps in the test: + *
    + *
  1. Programmatically construct the HelloWorld program
  2. + *
  3. Add two simple annotations at the class level
  4. + *
  5. Save the class to disk
  6. + *
  7. Reload the class using the 'static' variant of the BCEL classes
  8. + *
  9. Check the attributes are OK
  10. + *
+ */ + public void testGenerateClassLevelAnnotations() + throws ClassNotFoundException + { + // Create HelloWorld + ClassGen cg = createClassGen("HelloWorld"); + cg.setMajor(49); + cg.setMinor(0); + ConstantPoolGen cp = cg.getConstantPool(); + InstructionList il = new InstructionList(); + cg.addAnnotationEntry(createSimpleVisibleAnnotation(cp)); + cg.addAnnotationEntry(createSimpleInvisibleAnnotation(cp)); + buildClassContents(cg, cp, il); + //System.out.println(cg.getJavaClass().toString()); + dumpClass(cg, "HelloWorld.class"); + JavaClass jc = getClassFrom(".", "HelloWorld"); + AnnotationEntry[] as = jc.getAnnotationEntries(); + assertTrue("Should be two AnnotationEntries but found " + as.length, + as.length == 2); + // TODO L??; + assertTrue( + "Name of annotation 1 should be LSimpleAnnotation; but it is " + + as[0].getAnnotationType(), as[0].getAnnotationType() + .equals("LSimpleAnnotation;")); + assertTrue( + "Name of annotation 2 should be LSimpleAnnotation; but it is " + + as[1].getAnnotationType(), as[1].getAnnotationType() + .equals("LSimpleAnnotation;")); + ElementValuePair[] vals = as[0].getElementValuePairs(); + ElementValuePair nvp = vals[0]; + assertTrue( + "Name of element in SimpleAnnotation should be 'id' but it is " + + nvp.getNameString(), nvp.getNameString().equals("id")); + ElementValue ev = nvp.getValue(); + assertTrue("Type of element value should be int but it is " + + ev.getElementValueType(), + ev.getElementValueType() == ElementValue.PRIMITIVE_INT); + assertTrue("Value of element should be 4 but it is " + + ev.stringifyValue(), ev.stringifyValue().equals("4")); + assertTrue(createTestdataFile("HelloWorld.class").delete()); + } + + /** + * Just check that we can dump a class that has a method annotation on it + * and it is still there when we read it back in + */ + public void testGenerateMethodLevelAnnotations1() + throws ClassNotFoundException + { + // Create HelloWorld + ClassGen cg = createClassGen("HelloWorld"); + ConstantPoolGen cp = cg.getConstantPool(); + InstructionList il = new InstructionList(); + buildClassContentsWithAnnotatedMethods(cg, cp, il); + // Check annotation is OK + int i = cg.getMethods()[0].getAnnotationEntries().length; + assertTrue( + "Prior to dumping, main method should have 1 annotation but has " + + i, i == 1); + dumpClass(cg, "temp1" + File.separator + "HelloWorld.class"); + JavaClass jc2 = getClassFrom("temp1", "HelloWorld"); + // Check annotation is OK + i = jc2.getMethods()[0].getAnnotationEntries().length; + assertTrue("JavaClass should say 1 annotation on main method but says " + + i, i == 1); + ClassGen cg2 = new ClassGen(jc2); + // Check it now it is a ClassGen + Method[] m = cg2.getMethods(); + i = m[0].getAnnotationEntries().length; + assertTrue("The main 'Method' should have one annotation but has " + i, + i == 1); + MethodGen mg = new MethodGen(m[0], cg2.getClassName(), cg2 + .getConstantPool()); + // Check it finally when the Method is changed to a MethodGen + i = mg.getAnnotationEntries().length; + assertTrue("The main 'MethodGen' should have one annotation but has " + + i, i == 1); + + assertTrue(wipe("temp1", "HelloWorld.class")); + } + + /** + * Going further than the last test - when we reload the method back in, + * let's change it (adding a new annotation) and then store that, read it + * back in and verify both annotations are there ! + */ + public void testGenerateMethodLevelAnnotations2() + throws ClassNotFoundException + { + // Create HelloWorld + ClassGen cg = createClassGen("HelloWorld"); + ConstantPoolGen cp = cg.getConstantPool(); + InstructionList il = new InstructionList(); + buildClassContentsWithAnnotatedMethods(cg, cp, il); + dumpClass(cg, "temp2", "HelloWorld.class"); + JavaClass jc2 = getClassFrom("temp2", "HelloWorld"); + ClassGen cg2 = new ClassGen(jc2); + // Main method after reading the class back in + Method mainMethod1 = jc2.getMethods()[0]; + assertTrue("The 'Method' should have one annotations but has " + + mainMethod1.getAnnotationEntries().length, mainMethod1 + .getAnnotationEntries().length == 1); + MethodGen mainMethod2 = new MethodGen(mainMethod1, cg2.getClassName(), + cg2.getConstantPool()); + assertTrue("The 'MethodGen' should have one annotations but has " + + mainMethod2.getAnnotationEntries().length, mainMethod2 + .getAnnotationEntries().length == 1); + mainMethod2.addAnnotationEntry(createFruitAnnotation(cg2 + .getConstantPool(), "Pear")); + cg2.removeMethod(mainMethod1); + cg2.addMethod(mainMethod2.getMethod()); + dumpClass(cg2, "temp3", "HelloWorld.class"); + JavaClass jc3 = getClassFrom("temp3", "HelloWorld"); + ClassGen cg3 = new ClassGen(jc3); + Method mainMethod3 = cg3.getMethods()[1]; + int i = mainMethod3.getAnnotationEntries().length; + assertTrue("The 'Method' should now have two annotations but has " + i, + i == 2); + assertTrue(wipe("temp2", "HelloWorld.class")); + assertTrue(wipe("temp3", "HelloWorld.class")); + } + + // J5TODO: Need to add deleteFile calls to many of these tests + /** + * Transform simple class from an immutable to a mutable object. + */ + public void testTransformClassToClassGen_SimpleTypes() + throws ClassNotFoundException + { + JavaClass jc = getTestClass(PACKAGE_BASE_NAME+".data.SimpleAnnotatedClass"); + ClassGen cgen = new ClassGen(jc); + // Check annotations are correctly preserved + AnnotationEntryGen[] annotations = cgen.getAnnotationEntries(); + assertTrue("Expected one annotation but found " + annotations.length, + annotations.length == 1); + } + + /** + * Transform simple class from an immutable to a mutable object. The class + * is annotated with an annotation that uses an enum. + */ + public void testTransformClassToClassGen_EnumType() + throws ClassNotFoundException + { + JavaClass jc = getTestClass(PACKAGE_BASE_NAME+".data.AnnotatedWithEnumClass"); + ClassGen cgen = new ClassGen(jc); + // Check annotations are correctly preserved + AnnotationEntryGen[] annotations = cgen.getAnnotationEntries(); + assertTrue("Expected one annotation but found " + annotations.length, + annotations.length == 1); + } + + /** + * Transform simple class from an immutable to a mutable object. The class + * is annotated with an annotation that uses an array of SimpleAnnotations. + */ + public void testTransformClassToClassGen_ArrayAndAnnotationTypes() + throws ClassNotFoundException + { + JavaClass jc = getTestClass(PACKAGE_BASE_NAME+".data.AnnotatedWithCombinedAnnotation"); + ClassGen cgen = new ClassGen(jc); + // Check annotations are correctly preserved + AnnotationEntryGen[] annotations = cgen.getAnnotationEntries(); + assertTrue("Expected one annotation but found " + annotations.length, + annotations.length == 1); + AnnotationEntryGen a = annotations[0]; + assertTrue("That annotation should only have one value but has " + + a.getValues().size(), a.getValues().size() == 1); + ElementValuePairGen nvp = a.getValues().get(0); + ElementValueGen value = nvp.getValue(); + assertTrue("Value should be ArrayElementValueGen but is " + value, + value instanceof ArrayElementValueGen); + ArrayElementValueGen arrayValue = (ArrayElementValueGen) value; + assertTrue("Array value should be size one but is " + + arrayValue.getElementValuesSize(), arrayValue + .getElementValuesSize() == 1); + ElementValueGen innerValue = arrayValue.getElementValues().get(0); + assertTrue( + "Value in the array should be AnnotationElementValueGen but is " + + innerValue, + innerValue instanceof AnnotationElementValueGen); + AnnotationElementValueGen innerAnnotationValue = (AnnotationElementValueGen) innerValue; + assertTrue("Should be called L"+PACKAGE_BASE_SIG+"/data/SimpleAnnotation; but is called: " + + innerAnnotationValue.getAnnotation().getTypeName(), + innerAnnotationValue.getAnnotation().getTypeSignature().equals( + "L"+PACKAGE_BASE_SIG+"/data/SimpleAnnotation;")); + + // check the three methods + Method[] methods = cgen.getMethods(); + assertEquals(3, methods.length); + for(Method method : methods) + { + String methodName= method.getName(); + if(methodName.equals("")) + { + assertMethodAnnotations(method, 0, 1); + assertParameterAnnotations(method, 0, 1); + } + else if(methodName.equals("methodWithArrayOfZeroAnnotations")) + { + assertMethodAnnotations(method, 1, 0); + } + else if(methodName.equals("methodWithArrayOfTwoAnnotations")) + { + assertMethodAnnotations(method, 1, 2); + } + else + { + fail("unexpected method "+method.getName()); + } + } + } + + private void assertMethodAnnotations(final Method method, final int expectedNumberAnnotations, final int nExpectedArrayValues) + { + String methodName= method.getName(); + AnnotationEntry[] annos= method.getAnnotationEntries(); + assertEquals("For "+methodName, expectedNumberAnnotations, annos.length); + if(expectedNumberAnnotations!=0) + { + assertArrayElementValue(nExpectedArrayValues, annos[0]); + } + } + + private void assertArrayElementValue(final int nExpectedArrayValues, final AnnotationEntry anno) + { + ElementValuePair elementValuePair = anno.getElementValuePairs()[0]; + assertEquals("value", elementValuePair.getNameString()); + ArrayElementValue ev = (ArrayElementValue) elementValuePair.getValue(); + ElementValue[] eva = ev.getElementValuesArray(); + assertEquals(nExpectedArrayValues, eva.length); + } + + private void assertParameterAnnotations(final Method method, final int... expectedNumberOfParmeterAnnotations) + { + String methodName= "For "+method.getName(); + ParameterAnnotationEntry[] parameterAnnotations= method.getParameterAnnotationEntries(); + assertEquals(methodName, expectedNumberOfParmeterAnnotations.length, parameterAnnotations.length); + + int i= 0; + for(ParameterAnnotationEntry parameterAnnotation : parameterAnnotations) + { + AnnotationEntry[] annos= parameterAnnotation.getAnnotationEntries(); + int expectedLength = expectedNumberOfParmeterAnnotations[i++]; + assertEquals(methodName+" parameter "+i, expectedLength, annos.length); + if(expectedLength!=0) + { + assertSimpleElementValue(annos[0]); + } + } + } + + private void assertSimpleElementValue(final AnnotationEntry anno) + { + ElementValuePair elementValuePair = anno.getElementValuePairs()[0]; + assertEquals("id", elementValuePair.getNameString()); + SimpleElementValue ev = (SimpleElementValue)elementValuePair.getValue(); + assertEquals(42, ev.getValueInt()); + } + + /** + * Transform complex class from an immutable to a mutable object. + */ + public void testTransformComplexClassToClassGen() + throws ClassNotFoundException + { + JavaClass jc = getTestClass(PACKAGE_BASE_NAME+".data.ComplexAnnotatedClass"); + ClassGen cgen = new ClassGen(jc); + // Check annotations are correctly preserved + AnnotationEntryGen[] annotations = cgen.getAnnotationEntries(); + assertTrue("Expected one annotation but found " + annotations.length, + annotations.length == 1); + List l = annotations[0].getValues(); + boolean found = false; + for (Object name : l) { + ElementValuePairGen element = (ElementValuePairGen) name; + if (element.getNameString().equals("dval")) + { + if (((SimpleElementValueGen) element.getValue()) + .stringifyValue().equals("33.4")) { + found = true; + } + } + } + assertTrue("Did not find double annotation value with value 33.4", + found); + } + + /** + * Load a class in and modify it with a new attribute - A SimpleAnnotation + * annotation + */ + public void testModifyingClasses1() throws ClassNotFoundException + { + JavaClass jc = getTestClass(PACKAGE_BASE_NAME+".data.SimpleAnnotatedClass"); + ClassGen cgen = new ClassGen(jc); + ConstantPoolGen cp = cgen.getConstantPool(); + cgen.addAnnotationEntry(createFruitAnnotation(cp, "Pineapple")); + assertTrue("Should now have two annotations but has " + + cgen.getAnnotationEntries().length, cgen + .getAnnotationEntries().length == 2); + dumpClass(cgen, "SimpleAnnotatedClass.class"); + assertTrue(wipe("SimpleAnnotatedClass.class")); + } + + /** + * Load a class in and modify it with a new attribute - A ComplexAnnotation + * annotation + */ + public void testModifyingClasses2() throws ClassNotFoundException + { + JavaClass jc = getTestClass(PACKAGE_BASE_NAME+".data.SimpleAnnotatedClass"); + ClassGen cgen = new ClassGen(jc); + ConstantPoolGen cp = cgen.getConstantPool(); + cgen.addAnnotationEntry(createCombinedAnnotation(cp)); + assertTrue("Should now have two annotations but has " + + cgen.getAnnotationEntries().length, cgen + .getAnnotationEntries().length == 2); + dumpClass(cgen, "SimpleAnnotatedClass.class"); + JavaClass jc2 = getClassFrom(".", "SimpleAnnotatedClass"); + jc2.getAnnotationEntries(); + assertTrue(wipe("SimpleAnnotatedClass.class")); + // System.err.println(jc2.toString()); + } + + private void dumpClass(final ClassGen cg, final String fname) + { + try + { + File f = createTestdataFile(fname); + cg.getJavaClass().dump(f); + } + catch (java.io.IOException e) + { + System.err.println(e); + } + } + + private void dumpClass(final ClassGen cg, final String dir, final String fname) + { + dumpClass(cg, dir + File.separator + fname); + } + + private void buildClassContentsWithAnnotatedMethods(final ClassGen cg, + final ConstantPoolGen cp, final InstructionList il) + { + // Create method 'public static void main(String[]argv)' + MethodGen mg = createMethodGen("main", il, cp); + InstructionFactory factory = new InstructionFactory(cg); + mg.addAnnotationEntry(createSimpleVisibleAnnotation(mg + .getConstantPool())); + // We now define some often used types: + ObjectType i_stream = new ObjectType("java.io.InputStream"); + ObjectType p_stream = new ObjectType("java.io.PrintStream"); + // Create variables in and name : We call the constructors, i.e., + // execute BufferedReader(InputStreamReader(System.in)) . The reference + // to the BufferedReader object stays on top of the stack and is stored + // in the newly allocated in variable. + il.append(factory.createNew("java.io.BufferedReader")); + il.append(InstructionConst.DUP); // Use predefined constant + il.append(factory.createNew("java.io.InputStreamReader")); + il.append(InstructionConst.DUP); + il.append(factory.createFieldAccess("java.lang.System", "in", i_stream, + Const.GETSTATIC)); + il.append(factory.createInvoke("java.io.InputStreamReader", "", + Type.VOID, new Type[] { i_stream }, Const.INVOKESPECIAL)); + il.append(factory.createInvoke("java.io.BufferedReader", "", + Type.VOID, new Type[] { new ObjectType("java.io.Reader") }, + Const.INVOKESPECIAL)); + LocalVariableGen lg = mg.addLocalVariable("in", new ObjectType( + "java.io.BufferedReader"), null, null); + int in = lg.getIndex(); + lg.setStart(il.append(new ASTORE(in))); // "in" valid from here + // Create local variable name and initialize it to null + lg = mg.addLocalVariable("name", Type.STRING, null, null); + int name = lg.getIndex(); + il.append(InstructionConst.ACONST_NULL); + lg.setStart(il.append(new ASTORE(name))); // "name" valid from here + // Create try-catch block: We remember the start of the block, read a + // line from the standard input and store it into the variable name . + // InstructionHandle try_start = il.append(factory.createFieldAccess( + // "java.lang.System", "out", p_stream, Constants.GETSTATIC)); + // il.append(new PUSH(cp, "Please enter your name> ")); + // il.append(factory.createInvoke("java.io.PrintStream", "print", + // Type.VOID, new Type[] { Type.STRING }, + // Constants.INVOKEVIRTUAL)); + // il.append(new ALOAD(in)); + // il.append(factory.createInvoke("java.io.BufferedReader", "readLine", + // Type.STRING, Type.NO_ARGS, Constants.INVOKEVIRTUAL)); + InstructionHandle try_start = il.append(new PUSH(cp, "Andy")); + il.append(new ASTORE(name)); + // Upon normal execution we jump behind exception handler, the target + // address is not known yet. + GOTO g = new GOTO(null); + InstructionHandle try_end = il.append(g); + // We add the exception handler which simply returns from the method. + LocalVariableGen var_ex = mg.addLocalVariable("ex", Type + .getType("Ljava.io.IOException;"), null, null); + int var_ex_slot = var_ex.getIndex(); + InstructionHandle handler = il.append(new ASTORE(var_ex_slot)); + var_ex.setStart(handler); + var_ex.setEnd(il.append(InstructionConst.RETURN)); + mg.addExceptionHandler(try_start, try_end, handler, new ObjectType( + "java.io.IOException")); + // "Normal" code continues, now we can set the branch target of the GOTO + // . + InstructionHandle ih = il.append(factory.createFieldAccess( + "java.lang.System", "out", p_stream, Const.GETSTATIC)); + g.setTarget(ih); + // Printing "Hello": String concatenation compiles to StringBuffer + // operations. + il.append(factory.createNew(Type.STRINGBUFFER)); + il.append(InstructionConst.DUP); + il.append(new PUSH(cp, "Hello, ")); + il + .append(factory.createInvoke("java.lang.StringBuffer", + "", Type.VOID, new Type[] { Type.STRING }, + Const.INVOKESPECIAL)); + il.append(new ALOAD(name)); + il.append(factory.createInvoke("java.lang.StringBuffer", "append", + Type.STRINGBUFFER, new Type[] { Type.STRING }, + Const.INVOKEVIRTUAL)); + il.append(factory.createInvoke("java.lang.StringBuffer", "toString", + Type.STRING, Type.NO_ARGS, Const.INVOKEVIRTUAL)); + il + .append(factory.createInvoke("java.io.PrintStream", "println", + Type.VOID, new Type[] { Type.STRING }, + Const.INVOKEVIRTUAL)); + il.append(InstructionConst.RETURN); + // Finalization: Finally, we have to set the stack size, which normally + // would have to be computed on the fly and add a default constructor + // method to the class, which is empty in this case. + mg.setMaxStack(); + mg.setMaxLocals(); + cg.addMethod(mg.getMethod()); + il.dispose(); // Allow instruction handles to be reused + cg.addEmptyConstructor(Const.ACC_PUBLIC); + } + + private void buildClassContents(final ClassGen cg, final ConstantPoolGen cp, + final InstructionList il) + { + // Create method 'public static void main(String[]argv)' + MethodGen mg = createMethodGen("main", il, cp); + InstructionFactory factory = new InstructionFactory(cg); + // We now define some often used types: + ObjectType i_stream = new ObjectType("java.io.InputStream"); + ObjectType p_stream = new ObjectType("java.io.PrintStream"); + // Create variables in and name : We call the constructors, i.e., + // execute BufferedReader(InputStreamReader(System.in)) . The reference + // to the BufferedReader object stays on top of the stack and is stored + // in the newly allocated in variable. + il.append(factory.createNew("java.io.BufferedReader")); + il.append(InstructionConst.DUP); // Use predefined constant + il.append(factory.createNew("java.io.InputStreamReader")); + il.append(InstructionConst.DUP); + il.append(factory.createFieldAccess("java.lang.System", "in", i_stream, + Const.GETSTATIC)); + il.append(factory.createInvoke("java.io.InputStreamReader", "", + Type.VOID, new Type[] { i_stream }, Const.INVOKESPECIAL)); + il.append(factory.createInvoke("java.io.BufferedReader", "", + Type.VOID, new Type[] { new ObjectType("java.io.Reader") }, + Const.INVOKESPECIAL)); + LocalVariableGen lg = mg.addLocalVariable("in", new ObjectType( + "java.io.BufferedReader"), null, null); + int in = lg.getIndex(); + lg.setStart(il.append(new ASTORE(in))); // "in" valid from here + // Create local variable name and initialize it to null + lg = mg.addLocalVariable("name", Type.STRING, null, null); + int name = lg.getIndex(); + il.append(InstructionConst.ACONST_NULL); + lg.setStart(il.append(new ASTORE(name))); // "name" valid from here + // Create try-catch block: We remember the start of the block, read a + // line from the standard input and store it into the variable name . + // InstructionHandle try_start = il.append(factory.createFieldAccess( + // "java.lang.System", "out", p_stream, Constants.GETSTATIC)); + // il.append(new PUSH(cp, "Please enter your name> ")); + // il.append(factory.createInvoke("java.io.PrintStream", "print", + // Type.VOID, new Type[] { Type.STRING }, + // Constants.INVOKEVIRTUAL)); + // il.append(new ALOAD(in)); + // il.append(factory.createInvoke("java.io.BufferedReader", "readLine", + // Type.STRING, Type.NO_ARGS, Constants.INVOKEVIRTUAL)); + InstructionHandle try_start = il.append(new PUSH(cp, "Andy")); + il.append(new ASTORE(name)); + // Upon normal execution we jump behind exception handler, the target + // address is not known yet. + GOTO g = new GOTO(null); + InstructionHandle try_end = il.append(g); + // We add the exception handler which simply returns from the method. + LocalVariableGen var_ex = mg.addLocalVariable("ex", Type + .getType("Ljava.io.IOException;"), null, null); + int var_ex_slot = var_ex.getIndex(); + InstructionHandle handler = il.append(new ASTORE(var_ex_slot)); + var_ex.setStart(handler); + var_ex.setEnd(il.append(InstructionConst.RETURN)); + mg.addExceptionHandler(try_start, try_end, handler, new ObjectType( + "java.io.IOException")); + // "Normal" code continues, now we can set the branch target of the GOTO + // . + InstructionHandle ih = il.append(factory.createFieldAccess( + "java.lang.System", "out", p_stream, Const.GETSTATIC)); + g.setTarget(ih); + // Printing "Hello": String concatenation compiles to StringBuffer + // operations. + il.append(factory.createNew(Type.STRINGBUFFER)); + il.append(InstructionConst.DUP); + il.append(new PUSH(cp, "Hello, ")); + il + .append(factory.createInvoke("java.lang.StringBuffer", + "", Type.VOID, new Type[] { Type.STRING }, + Const.INVOKESPECIAL)); + il.append(new ALOAD(name)); + il.append(factory.createInvoke("java.lang.StringBuffer", "append", + Type.STRINGBUFFER, new Type[] { Type.STRING }, + Const.INVOKEVIRTUAL)); + il.append(factory.createInvoke("java.lang.StringBuffer", "toString", + Type.STRING, Type.NO_ARGS, Const.INVOKEVIRTUAL)); + il + .append(factory.createInvoke("java.io.PrintStream", "println", + Type.VOID, new Type[] { Type.STRING }, + Const.INVOKEVIRTUAL)); + il.append(InstructionConst.RETURN); + // Finalization: Finally, we have to set the stack size, which normally + // would have to be computed on the fly and add a default constructor + // method to the class, which is empty in this case. + mg.setMaxStack(); + mg.setMaxLocals(); + cg.addMethod(mg.getMethod()); + il.dispose(); // Allow instruction handles to be reused + cg.addEmptyConstructor(Const.ACC_PUBLIC); + } + + private JavaClass getClassFrom(final String where, final String clazzname) + throws ClassNotFoundException + { + // System.out.println(where); + SyntheticRepository repos = createRepos(where); + return repos.loadClass(clazzname); + } + + // helper methods + private ClassGen createClassGen(final String classname) + { + return new ClassGen(classname, "java.lang.Object", "", + Const.ACC_PUBLIC | Const.ACC_SUPER, null); + } + + private MethodGen createMethodGen(final String methodname, final InstructionList il, + final ConstantPoolGen cp) + { + return new MethodGen(Const.ACC_STATIC | Const.ACC_PUBLIC, // access + // flags + Type.VOID, // return type + new Type[] { new ArrayType(Type.STRING, 1) }, // argument + // types + new String[] { "argv" }, // arg names + methodname, "HelloWorld", // method, class + il, cp); + } + + public AnnotationEntryGen createSimpleVisibleAnnotation(final ConstantPoolGen cp) + { + SimpleElementValueGen evg = new SimpleElementValueGen( + ElementValueGen.PRIMITIVE_INT, cp, 4); + ElementValuePairGen nvGen = new ElementValuePairGen("id", evg, cp); + ObjectType t = new ObjectType("SimpleAnnotation"); + List elements = new ArrayList<>(); + elements.add(nvGen); + AnnotationEntryGen a = new AnnotationEntryGen(t, elements, true, cp); + return a; + } + + public AnnotationEntryGen createFruitAnnotation(final ConstantPoolGen cp, + final String aFruit) + { + SimpleElementValueGen evg = new SimpleElementValueGen( + ElementValueGen.STRING, cp, aFruit); + ElementValuePairGen nvGen = new ElementValuePairGen("fruit", evg, cp); + ObjectType t = new ObjectType("SimpleStringAnnotation"); + List elements = new ArrayList<>(); + elements.add(nvGen); + return new AnnotationEntryGen(t, elements, true, cp); + } + + public AnnotationEntryGen createCombinedAnnotation(final ConstantPoolGen cp) + { + // Create an annotation instance + AnnotationEntryGen a = createSimpleVisibleAnnotation(cp); + ArrayElementValueGen array = new ArrayElementValueGen(cp); + array.addElement(new AnnotationElementValueGen(a, cp)); + ElementValuePairGen nvp = new ElementValuePairGen("value", array, cp); + List elements = new ArrayList<>(); + elements.add(nvp); + return new AnnotationEntryGen(new ObjectType("CombinedAnnotation"), + elements, true, cp); + } + + public AnnotationEntryGen createSimpleInvisibleAnnotation(final ConstantPoolGen cp) + { + SimpleElementValueGen evg = new SimpleElementValueGen( + ElementValueGen.PRIMITIVE_INT, cp, 4); + ElementValuePairGen nvGen = new ElementValuePairGen("id", evg, cp); + ObjectType t = new ObjectType("SimpleAnnotation"); + List elements = new ArrayList<>(); + elements.add(nvGen); + AnnotationEntryGen a = new AnnotationEntryGen(t, elements, false, cp); + return a; + } +} diff --git a/src/test/java/org/apache/bcel/generic/InstructionHandleTestCase.java b/src/test/java/org/apache/bcel/generic/InstructionHandleTestCase.java new file mode 100644 index 00000000..ab9d786c --- /dev/null +++ b/src/test/java/org/apache/bcel/generic/InstructionHandleTestCase.java @@ -0,0 +1,46 @@ +package org.apache.commons.bcel6.generic; + +import org.junit.Assert; +import org.junit.Test; + +public class InstructionHandleTestCase { + + // Test that setInstruction only allows Instructions that are not BranchInstructions + + @Test(expected=ClassGenException.class) + public void testsetInstructionNull() { + InstructionHandle ih = InstructionHandle.getInstructionHandle(new NOP());// have to start with a valid non BI + Assert.assertNotNull(ih); + ih.setInstruction(null); + Assert.assertNotNull(ih); + } + + @Test + public void testsetInstructionI() { + InstructionHandle ih = InstructionHandle.getInstructionHandle(new NOP());// have to start with a valid non BI + Assert.assertNotNull(ih); + ih.setInstruction(new NOP()); + Assert.assertNotNull(ih); + } + + @Test(expected=ClassGenException.class) + public void testsetInstructionnotI() { + InstructionHandle ih = InstructionHandle.getInstructionHandle(new NOP());// have to start with a valid non BI + Assert.assertNotNull(ih); + ih.setInstruction(new GOTO(null)); + Assert.assertNotNull(ih); + } + + @Test(expected=ClassGenException.class) + public void testGetIHnull() { + InstructionHandle.getInstructionHandle(null); + } + + @Test + public void testBCEL195() { + InstructionList il = new InstructionList(); + InstructionHandle ih = il.append(InstructionConst.NOP); + new TABLESWITCH(new int[0], new InstructionHandle[0], ih); + new TABLESWITCH(new int[0], new InstructionHandle[0], ih); + } +} diff --git a/src/test/java/org/apache/bcel/generic/JDKGenericDumpTestCase.java b/src/test/java/org/apache/bcel/generic/JDKGenericDumpTestCase.java new file mode 100644 index 00000000..34e1caf2 --- /dev/null +++ b/src/test/java/org/apache/bcel/generic/JDKGenericDumpTestCase.java @@ -0,0 +1,116 @@ +/* + * 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.commons.bcel6.generic; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.fail; + +import java.io.File; +import java.io.FileFilter; +import java.io.InputStream; +import java.util.Enumeration; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; + +import org.apache.commons.bcel6.classfile.ClassParser; +import org.apache.commons.bcel6.classfile.Code; +import org.apache.commons.bcel6.classfile.JavaClass; +import org.apache.commons.bcel6.classfile.Method; +import org.junit.Test; + +/** + * Test that the generic dump() methods work on the JDK classes + * Reads each class into an instruction list and then dumps + * the instructions. The output bytes should be the same as the input. + */ +public class JDKGenericDumpTestCase { + + @Test + public void testJDKjars() throws Exception { + File[] jars = listJDKjars(); + for(File file : jars) { + testJar(file); + } + } + + private void testJar(final File file) throws Exception { + System.out.println(file); + JarFile jar = new JarFile(file); + Enumeration en = jar.entries(); + + while (en.hasMoreElements()) { + JarEntry e = en.nextElement(); + final String name = e.getName(); + if (name.endsWith(".class")) { +// System.out.println("- " + name); + InputStream in = jar.getInputStream(e); + ClassParser parser = new ClassParser(in, name); + JavaClass jc = parser.parse(); + for(Method m : jc.getMethods()) { + compare(name, m); + } + } + } + jar.close(); + } + + private void compare(final String name, final Method m) { +// System.out.println("Method: " + m); + Code c = m.getCode(); + if (c==null) { + return; // e.g. abstract method + } + byte[] src = c.getCode(); + InstructionList il = new InstructionList(src); + byte[] out = il.getByteCode(); + if (src.length == out.length) { + assertArrayEquals(name + ": "+m.toString(), src, out); + } else { + System.out.println(name + ": "+m.toString() +" "+ src.length+" "+out.length); + System.out.println(bytesToHex(src)); + System.out.println(bytesToHex(out)); + for(InstructionHandle ih : il) { + System.out.println(ih.toString(false)); + } + fail("Array comparison failure"); + } + } + + private File[] listJDKjars() throws Exception { + File javaLib = new File(System.getProperty("java.home") + "/lib"); + return javaLib.listFiles(new FileFilter() { + @Override + public boolean accept(final File file) { + return file.getName().endsWith(".jar"); + } + }); + } + + private static final char[] hexArray = "0123456789ABCDEF".toCharArray(); + private static String bytesToHex(final byte[] bytes) { + char[] hexChars = new char[bytes.length * 3]; + int i=0; + for (byte b : bytes) { + int v = b & 0xFF; + hexChars[i++] = hexArray[v >>> 4]; + hexChars[i++] = hexArray[v & 0x0F]; + hexChars[i++] = ' '; + } + return new String(hexChars); + } +} diff --git a/src/test/java/org/apache/bcel/generic/MethodGenTestCase.java b/src/test/java/org/apache/bcel/generic/MethodGenTestCase.java new file mode 100644 index 00000000..d432d127 --- /dev/null +++ b/src/test/java/org/apache/bcel/generic/MethodGenTestCase.java @@ -0,0 +1,91 @@ +/* + * 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.commons.bcel6.generic; + +import java.util.Arrays; + +import org.apache.commons.bcel6.Repository; +import org.apache.commons.bcel6.classfile.JavaClass; +import org.apache.commons.bcel6.classfile.Method; + +import junit.framework.TestCase; + +public class MethodGenTestCase extends TestCase { + + public static class Foo { + public void bar() { + @SuppressWarnings("unused") + int a = 1; + } + } + + private MethodGen getMethod(final Class cls, final String name) throws ClassNotFoundException { + JavaClass jc = Repository.lookupClass(cls); + ConstantPoolGen cp = new ConstantPoolGen(jc.getConstantPool()); + for (Method method : jc.getMethods()) { + if (method.getName().equals(name)) { + return new MethodGen(method, jc.getClassName(), cp); + } + } + + fail("Method " + name + " not found in class " + cls); + return null; + } + + public void testRemoveLocalVariable() throws Exception { + MethodGen mg = getMethod(Foo.class, "bar"); + + LocalVariableGen lv = mg.getLocalVariables()[1]; + assertEquals("variable name", "a", lv.getName()); + InstructionHandle start = lv.getStart(); + InstructionHandle end = lv.getEnd(); + assertNotNull("scope start", start); + assertNotNull("scope end", end); + assertTrue("scope start not targeted by the local variable", Arrays.asList(start.getTargeters()).contains(lv)); + assertTrue("scope end not targeted by the local variable", Arrays.asList(end.getTargeters()).contains(lv)); + + // now let's remove the local variable + mg.removeLocalVariable(lv); + + assertFalse("scope start still targeted by the removed variable", Arrays.asList(start.getTargeters()).contains(lv)); + assertFalse("scope end still targeted by the removed variable", Arrays.asList(end.getTargeters()).contains(lv)); + assertNull("scope start", lv.getStart()); + assertNull("scope end", lv.getEnd()); + } + + public void testRemoveLocalVariables() throws Exception { + MethodGen mg = getMethod(Foo.class, "bar"); + + LocalVariableGen lv = mg.getLocalVariables()[1]; + assertEquals("variable name", "a", lv.getName()); + InstructionHandle start = lv.getStart(); + InstructionHandle end = lv.getEnd(); + assertNotNull("scope start", start); + assertNotNull("scope end", end); + assertTrue("scope start not targeted by the local variable", Arrays.asList(start.getTargeters()).contains(lv)); + assertTrue("scope end not targeted by the local variable", Arrays.asList(end.getTargeters()).contains(lv)); + + // now let's remove the local variables + mg.removeLocalVariables(); + + assertFalse("scope start still targeted by the removed variable", Arrays.asList(start.getTargeters()).contains(lv)); + assertFalse("scope end still targeted by the removed variable", Arrays.asList(end.getTargeters()).contains(lv)); + assertNull("scope start", lv.getStart()); + assertNull("scope end", lv.getEnd()); + } +} diff --git a/src/test/java/org/apache/bcel/util/BCELifierTestCase.java b/src/test/java/org/apache/bcel/util/BCELifierTestCase.java new file mode 100644 index 00000000..b42b94e9 --- /dev/null +++ b/src/test/java/org/apache/bcel/util/BCELifierTestCase.java @@ -0,0 +1,78 @@ +package org.apache.commons.bcel6.util; + +import static org.junit.Assert.*; + +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.OutputStream; +import org.apache.commons.bcel6.classfile.JavaClass; +import org.junit.Test; + + +public class BCELifierTestCase { + + @Test + public void test() throws Exception { + OutputStream os = new ByteArrayOutputStream(); + JavaClass java_class = BCELifier.getJavaClass("Java8Example"); + assertNotNull(java_class); + BCELifier bcelifier = new BCELifier(java_class, os); + bcelifier.start(); + } + + /* + * Dump a class using "javap" and compare with the same class recreated + * using BCELifier, "javac", "java" and dumped with "javap" + * TODO: detect if JDK present and skip test if not + */ + @Test + @org.junit.Ignore // does not work properly on some systems. Also the output is rather different + public void testJavapCompare() throws Exception { + testClassOnPath("target/test-classes/Java8Example.class"); + } + + private void testClassOnPath(final String javaClass) throws Exception { + // Get javap of the input class + final String initial = exec(null, "javap", "-p", "-c", javaClass); + + final File workDir = new File("target"); + File infile = new File(javaClass); + JavaClass java_class = BCELifier.getJavaClass(infile.getName().replace(".class", "")); + assertNotNull(java_class); + File outfile = new File(workDir,infile.getName().replace(".class", "Creator.java")); + FileOutputStream fos = new FileOutputStream(outfile); + BCELifier bcelifier = new BCELifier(java_class, fos); + bcelifier.start(); + fos.close(); + exec(workDir, "javac", "-cp", "classes", outfile.getName()); + exec(workDir, "java", "-cp", ".:classes", outfile.getName().replace(".java", "")); + final String output = exec(workDir, "javap", "-p", "-c", infile.getName()); + assertEquals(initial, output); + } + + private String exec(final File workDir, final String ... args) throws Exception { +// System.err.println(java.util.Arrays.toString(args)); + ProcessBuilder pb = new ProcessBuilder( args ); + pb.directory(workDir); + Process proc = pb.start(); + BufferedInputStream is = new BufferedInputStream(proc.getInputStream()); + InputStream es = proc.getErrorStream(); + proc.waitFor(); + byte []buff=new byte[2048]; + int len; + while((len=es.read(buff)) != -1) { + System.err.print(new String(buff,0,len)); + } + + StringBuilder sb = new StringBuilder(); + while((len=is.read(buff)) != -1) { + sb.append(new String(buff,0,len)); + } + is.close(); + return sb.toString(); + } + +} diff --git a/src/test/java/org/apache/bcel/util/Class2HTMLTestCase.java b/src/test/java/org/apache/bcel/util/Class2HTMLTestCase.java new file mode 100644 index 00000000..be468a32 --- /dev/null +++ b/src/test/java/org/apache/bcel/util/Class2HTMLTestCase.java @@ -0,0 +1,42 @@ +/* + * 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.commons.bcel6.util; + +import java.io.File; +import java.io.FileInputStream; + +import org.apache.commons.bcel6.classfile.ClassParser; +import org.junit.Assert; + +import junit.framework.TestCase; + +public class Class2HTMLTestCase extends TestCase { + + public void testConvertJavaUtil() throws Exception { + File outputDir = new File("target/test-output/html"); + if (!outputDir.mkdirs()) { // either was not created or already existed + Assert.assertTrue(outputDir.isDirectory()); // fail if missing + } + + FileInputStream file = new FileInputStream("target/test-classes/Java8Example.class"); + + ClassParser parser = new ClassParser(file, "Java8Example.class"); + + new Class2HTML(parser.parse(), outputDir.getAbsolutePath() + "/"); + } +} diff --git a/src/test/java/org/apache/bcel/util/InstructionFinderTest.java b/src/test/java/org/apache/bcel/util/InstructionFinderTest.java new file mode 100644 index 00000000..7701e841 --- /dev/null +++ b/src/test/java/org/apache/bcel/util/InstructionFinderTest.java @@ -0,0 +1,46 @@ +/* + * 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.commons.bcel6.util; + +import java.util.Iterator; + +import org.apache.commons.bcel6.AbstractTestCase; +import org.apache.commons.bcel6.generic.IADD; +import org.apache.commons.bcel6.generic.ILOAD; +import org.apache.commons.bcel6.generic.ISTORE; +import org.apache.commons.bcel6.generic.InstructionHandle; +import org.apache.commons.bcel6.generic.InstructionList; + +public class InstructionFinderTest extends AbstractTestCase { + + public void testSearch() { + InstructionList il = new InstructionList(); + il.append(new ILOAD(1)); + il.append(new ILOAD(2)); + il.append(new IADD()); + il.append(new ISTORE(3)); + InstructionFinder finder = new InstructionFinder(il); + + Iterator it = finder.search("ILOAD IADD", il.getInstructionHandles()[0], null ); + InstructionHandle[] ihs = (InstructionHandle[])it.next(); + assertEquals(2, ihs.length); + assertEquals(ihs[0].getInstruction(), new ILOAD(2)); + assertEquals(ihs[1].getInstruction(), new IADD()); + } +} diff --git a/src/test/java/org/apache/bcel/verifier/AbstractVerifierTestCase.java b/src/test/java/org/apache/bcel/verifier/AbstractVerifierTestCase.java new file mode 100644 index 00000000..b773e8ba --- /dev/null +++ b/src/test/java/org/apache/bcel/verifier/AbstractVerifierTestCase.java @@ -0,0 +1,95 @@ +/* + * 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.commons.bcel6.verifier; + +import org.apache.commons.bcel6.Repository; +import org.apache.commons.bcel6.classfile.JavaClass; + +import junit.framework.TestCase; + +public abstract class AbstractVerifierTestCase extends TestCase { + + public static final String TEST_PACKAGE = AbstractVerifierTestCase.class.getPackage().getName() + ".tests."; + + /** + * Asserts that the verification of the given class is OK. If it isn't it throws an AssertionFailedError with the given message. + * + * @param classname simple classname of the class to verify + * @param message message displayed if assertion fails + */ + public void assertVerifyOK(final String classname, final String message) { + final String testClassname = TEST_PACKAGE + classname; + assertTrue(message, doAllPasses(testClassname)); + } + + /** + * Asserts that the verification of the given class is rejected. + * If it isn't it throws an AssertionFailedError with the given message. + * + * @param classname simple classname of the class to verify + * @param message message displayed if assertion fails + */ + public void assertVerifyRejected(final String classname, final String message) { + final String testClassname = TEST_PACKAGE + classname; + assertFalse(message, doAllPasses(testClassname)); + } + + /** + * Executes all the verification on the given class. + * + * @param classname name of the class to verify + * @return false if the verification fails, true otherwise + */ + public boolean doAllPasses(final String classname) { + int nbMethods = 0; + + try { + JavaClass jc = Repository.lookupClass(classname); + nbMethods = jc.getMethods().length; + } catch (ClassNotFoundException e) { + fail(e.getMessage()); + return false; + } + + Verifier verifier = VerifierFactory.getVerifier(classname); + VerificationResult result = verifier.doPass1(); + if (result.getStatus() != VerificationResult.VERIFIED_OK) { + return false; + } + + result = verifier.doPass2(); + if (result.getStatus() != VerificationResult.VERIFIED_OK) { + return false; + } + + for (int i = nbMethods; --i >= 0;) { + result = verifier.doPass3a(i); + if (result.getStatus() != VerificationResult.VERIFIED_OK) { + return false; + } + result = verifier.doPass3b(i); + if (result.getStatus() != VerificationResult.VERIFIED_OK) { + return false; + } + } + + return true; + } + +} diff --git a/src/test/java/org/apache/bcel/verifier/VerifierArrayAccessTestCase.java b/src/test/java/org/apache/bcel/verifier/VerifierArrayAccessTestCase.java new file mode 100644 index 00000000..7408e87f --- /dev/null +++ b/src/test/java/org/apache/bcel/verifier/VerifierArrayAccessTestCase.java @@ -0,0 +1,45 @@ +/* + * 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.commons.bcel6.verifier; + +import java.io.IOException; + +import org.apache.commons.bcel6.verifier.tests.TestArrayAccess02Creator; +import org.apache.commons.bcel6.verifier.tests.TestArrayAccess03Creator; +import org.apache.commons.bcel6.verifier.tests.TestArrayAccess04Creator; + +public class VerifierArrayAccessTestCase extends AbstractVerifierTestCase { + + public void testInvalidArrayAccess() throws IOException { + new TestArrayAccess03Creator().create(); + assertVerifyRejected("TestArrayAccess03", "Verification of an arraystore instruction on an object must fail."); + new TestArrayAccess04Creator().create(); + assertVerifyRejected("TestArrayAccess04", + "Verification of an arraystore instruction of an int on an array of references must fail."); + } + + public void testValidArrayAccess() throws IOException { + assertVerifyOK("TestArrayAccess01", + "Verification of an arraystore instruction on an array that is not compatible with the stored element must pass."); + new TestArrayAccess02Creator().create(); + assertVerifyOK("TestArrayAccess02", + "Verification of an arraystore instruction on an array that is not compatible with the stored element must pass."); + } + +} diff --git a/src/test/java/org/apache/bcel/verifier/VerifierInvokeTestCase.java b/src/test/java/org/apache/bcel/verifier/VerifierInvokeTestCase.java new file mode 100644 index 00000000..6a01a902 --- /dev/null +++ b/src/test/java/org/apache/bcel/verifier/VerifierInvokeTestCase.java @@ -0,0 +1,41 @@ +/* + * 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.commons.bcel6.verifier; + + +public class VerifierInvokeTestCase extends AbstractVerifierTestCase { + + public void testLegalInvokeVirtual() { + assertVerifyOK("TestLegalInvokeVirtual01", "Verification of invokevirtual on method defined in superclass must pass."); + assertVerifyOK("TestLegalInvokeVirtual02", "Verification of invokevirtual on method defined in superinterface must pass."); + } + + public void testLegalInvokeStatic() { + assertVerifyOK("TestLegalInvokeStatic01", "Verification of invokestatic on method defined in superclass must pass."); + } + + public void testLegalInvokeInterface() { + assertVerifyOK("TestLegalInvokeInterface01", "Verification of invokeinterface on method defined in superinterface must pass."); + } + + public void testLegalInvokeSpecial() { + assertVerifyOK("TestLegalInvokeSpecial01", "Verification of invokespecial on method defined in superclass must pass."); + assertVerifyOK("TestLegalInvokeSpecial02", "Verification of invokespecial on method defined in superclass must pass."); + } +} diff --git a/src/test/java/org/apache/bcel/verifier/VerifierReturnTestCase.java b/src/test/java/org/apache/bcel/verifier/VerifierReturnTestCase.java new file mode 100644 index 00000000..dd6554d4 --- /dev/null +++ b/src/test/java/org/apache/bcel/verifier/VerifierReturnTestCase.java @@ -0,0 +1,38 @@ +/* + * 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.commons.bcel6.verifier; + +import java.io.IOException; + +import org.apache.commons.bcel6.verifier.tests.TestReturn01Creator; +import org.apache.commons.bcel6.verifier.tests.TestReturn03Creator; + +public class VerifierReturnTestCase extends AbstractVerifierTestCase { + + public void testInvalidReturn() throws IOException { + new TestReturn01Creator().create(); + assertVerifyRejected("TestReturn01", "Verification of a void method that returns an object must fail."); + new TestReturn03Creator().create(); + assertVerifyRejected("TestReturn03", "Verification of an int method that returns null must fail."); + } + + public void testValidReturn() { + assertVerifyOK("TestReturn02", "Verification of a method that returns a newly created object must pass."); + assertVerifyOK("TestArray01", "Verification of a method that returns an array must pass."); + } +} diff --git a/src/test/java/org/apache/bcel/verifier/VerifierTestCase.java b/src/test/java/org/apache/bcel/verifier/VerifierTestCase.java new file mode 100644 index 00000000..38ea8df5 --- /dev/null +++ b/src/test/java/org/apache/bcel/verifier/VerifierTestCase.java @@ -0,0 +1,41 @@ +/* + * 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.commons.bcel6.verifier; + +import java.util.Collection; + +import junit.framework.TestCase; + +public class VerifierTestCase extends TestCase { + + public void testDefaultMethodValidation() { + String classname = Collection.class.getName(); + + Verifier verifier = VerifierFactory.getVerifier(classname); + VerificationResult result = verifier.doPass1(); + + assertEquals("Pass 1 verification of " + classname + " failed: " + result.getMessage(), VerificationResult.VERIFIED_OK, + result.getStatus()); + + result = verifier.doPass2(); + + assertEquals("Pass 2 verification of " + classname + " failed: " + result.getMessage(), VerificationResult.VERIFIED_OK, + result.getStatus()); + } +} diff --git a/src/test/java/org/apache/bcel/verifier/tests/TestArray01.java b/src/test/java/org/apache/bcel/verifier/tests/TestArray01.java new file mode 100644 index 00000000..e97aa1c8 --- /dev/null +++ b/src/test/java/org/apache/bcel/verifier/tests/TestArray01.java @@ -0,0 +1,54 @@ +/* + * 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.commons.bcel6.verifier.tests; + +import java.io.Serializable; + +public class TestArray01{ + + public static Object test1(){ + String[] a = new String[4]; + a[0] = ""; + a.equals(null); + test2(a); + test3(a); + test4(a); + return a; + } + + public static void test2(final Object o){ + } + + public static void test3(final Serializable o){ + } + + public static void test4(final Cloneable o){ + } + + public static Serializable test5(){ + return new Object[1]; + } + + public static Cloneable test6(){ + return new Object[1]; + } + + public static Object foo(final String s){ + return s; + } +} diff --git a/src/test/java/org/apache/bcel/verifier/tests/TestArrayAccess01.java b/src/test/java/org/apache/bcel/verifier/tests/TestArrayAccess01.java new file mode 100644 index 00000000..7815c194 --- /dev/null +++ b/src/test/java/org/apache/bcel/verifier/tests/TestArrayAccess01.java @@ -0,0 +1,32 @@ +/* + * 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.commons.bcel6.verifier.tests; + + +public class TestArrayAccess01 extends XTestArray01{ + + public static void test(){ + XTestArray01[] array = new TestArrayAccess01[1]; + array[0] = new XTestArray01(); + } + +} + +class XTestArray01 { + +} diff --git a/src/test/java/org/apache/bcel/verifier/tests/TestArrayAccess02Creator.java b/src/test/java/org/apache/bcel/verifier/tests/TestArrayAccess02Creator.java new file mode 100644 index 00000000..5442121b --- /dev/null +++ b/src/test/java/org/apache/bcel/verifier/tests/TestArrayAccess02Creator.java @@ -0,0 +1,99 @@ +/* + * 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.commons.bcel6.verifier.tests; + +import java.io.IOException; +import java.io.OutputStream; + +import org.apache.commons.bcel6.Const; +import org.apache.commons.bcel6.generic.ClassGen; +import org.apache.commons.bcel6.generic.ConstantPoolGen; +import org.apache.commons.bcel6.generic.InstructionConst; +import org.apache.commons.bcel6.generic.InstructionFactory; +import org.apache.commons.bcel6.generic.InstructionHandle; +import org.apache.commons.bcel6.generic.InstructionList; +import org.apache.commons.bcel6.generic.MethodGen; +import org.apache.commons.bcel6.generic.ObjectType; +import org.apache.commons.bcel6.generic.PUSH; +import org.apache.commons.bcel6.generic.Type; +import org.junit.Assert; + +public class TestArrayAccess02Creator extends TestCreator { + private final InstructionFactory _factory; + private final ConstantPoolGen _cp; + private final ClassGen _cg; + + public TestArrayAccess02Creator() { + _cg = new ClassGen(TEST_PACKAGE+".TestArrayAccess02", "java.lang.Object", "TestArrayAccess02.java", + Const.ACC_PUBLIC | Const.ACC_SUPER, new String[] { }); + + _cp = _cg.getConstantPool(); + _factory = new InstructionFactory(_cg, _cp); + } + + @Override +public void create(final OutputStream out) throws IOException { + createMethod_0(); + createMethod_1(); + _cg.getJavaClass().dump(out); + } + + private void createMethod_0() { + InstructionList il = new InstructionList(); + MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] { }, "", + TEST_PACKAGE+".TestArrayAccess02", il, _cp); + + InstructionHandle ih_0 = il.append(InstructionFactory.createLoad(Type.OBJECT, 0)); + Assert.assertNotNull(ih_0); // TODO why is this not used + il.append(_factory.createInvoke("java.lang.Object", "", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL)); + InstructionHandle ih_4 = il.append(InstructionFactory.createReturn(Type.VOID)); + Assert.assertNotNull(ih_4); // TODO why is this not used + method.setMaxStack(); + method.setMaxLocals(); + _cg.addMethod(method.getMethod()); + il.dispose(); + } + + private void createMethod_1() { + InstructionList il = new InstructionList(); + MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.VOID, Type.NO_ARGS, new String[] { }, + "test", TEST_PACKAGE+".TestArrayAccess02", il, _cp); + + InstructionHandle ih_0 = il.append(new PUSH(_cp, 1)); + Assert.assertNotNull(ih_0); // TODO why is this not used + il.append(_factory.createNewArray(new ObjectType(TEST_PACKAGE+".TestArrayAccess02"), (short) 1)); + il.append(InstructionFactory.createStore(Type.OBJECT, 0)); + InstructionHandle ih_5 = il.append(new PUSH(_cp, 1)); + Assert.assertNotNull(ih_5); // TODO why is this not used + il.append(_factory.createNewArray(Type.STRING, (short) 1)); + il.append(InstructionFactory.createStore(Type.OBJECT, 1)); + InstructionHandle ih_10 = il.append(InstructionFactory.createLoad(Type.OBJECT, 1)); + Assert.assertNotNull(ih_10); // TODO why is this not used + il.append(new PUSH(_cp, 0)); + il.append(_factory.createNew(TEST_PACKAGE+".TestArrayAccess02")); + il.append(InstructionConst.DUP); + il.append(_factory.createInvoke(TEST_PACKAGE+".TestArrayAccess02", "", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL)); + il.append(InstructionConst.AASTORE); + InstructionHandle ih_20 = il.append(InstructionFactory.createReturn(Type.VOID)); + Assert.assertNotNull(ih_20); // TODO why is this not used + method.setMaxStack(); + method.setMaxLocals(); + _cg.addMethod(method.getMethod()); + il.dispose(); + } +} diff --git a/src/test/java/org/apache/bcel/verifier/tests/TestArrayAccess03Creator.java b/src/test/java/org/apache/bcel/verifier/tests/TestArrayAccess03Creator.java new file mode 100644 index 00000000..d9cae0b8 --- /dev/null +++ b/src/test/java/org/apache/bcel/verifier/tests/TestArrayAccess03Creator.java @@ -0,0 +1,95 @@ +/* + * 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.commons.bcel6.verifier.tests; + +import java.io.IOException; +import java.io.OutputStream; + +import org.apache.commons.bcel6.Const; +import org.apache.commons.bcel6.generic.ClassGen; +import org.apache.commons.bcel6.generic.ConstantPoolGen; +import org.apache.commons.bcel6.generic.InstructionConst; +import org.apache.commons.bcel6.generic.InstructionFactory; +import org.apache.commons.bcel6.generic.InstructionHandle; +import org.apache.commons.bcel6.generic.InstructionList; +import org.apache.commons.bcel6.generic.MethodGen; +import org.apache.commons.bcel6.generic.ObjectType; +import org.apache.commons.bcel6.generic.PUSH; +import org.apache.commons.bcel6.generic.Type; +import org.junit.Assert; + +public class TestArrayAccess03Creator extends TestCreator { + private final InstructionFactory _factory; + private final ConstantPoolGen _cp; + private final ClassGen _cg; + + public TestArrayAccess03Creator() { + _cg = new ClassGen(TEST_PACKAGE+".TestArrayAccess03", "java.lang.Object", "TestArrayAccess03.java", + Const.ACC_PUBLIC | Const.ACC_SUPER, new String[] { }); + + _cp = _cg.getConstantPool(); + _factory = new InstructionFactory(_cg, _cp); + } + + @Override +public void create(final OutputStream out) throws IOException { + createMethod_0(); + createMethod_1(); + _cg.getJavaClass().dump(out); + } + + private void createMethod_0() { + InstructionList il = new InstructionList(); + MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] { }, "", + TEST_PACKAGE+".TestArrayAccess03", il, _cp); + + InstructionHandle ih_0 = il.append(InstructionFactory.createLoad(Type.OBJECT, 0)); + Assert.assertNotNull(ih_0); // TODO why is this not used + il.append(_factory.createInvoke("java.lang.Object", "", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL)); + InstructionHandle ih_4 = il.append(InstructionFactory.createReturn(Type.VOID)); + Assert.assertNotNull(ih_4); // TODO why is this not used + method.setMaxStack(); + method.setMaxLocals(); + _cg.addMethod(method.getMethod()); + il.dispose(); + } + + private void createMethod_1() { + InstructionList il = new InstructionList(); + MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.VOID, new Type[] { Type.OBJECT }, + new String[] { "arg0" }, "test", TEST_PACKAGE+".TestArrayAccess03", il, _cp); + + InstructionHandle ih_0 = il.append(new PUSH(_cp, 1)); + Assert.assertNotNull(ih_0); // TODO why is this not used + il.append(_factory.createNewArray(new ObjectType(TEST_PACKAGE+".TestArrayAccess03"), (short) 1)); + il.append(InstructionFactory.createStore(Type.OBJECT, 1)); + InstructionHandle ih_5 = il.append(InstructionFactory.createLoad(Type.OBJECT, 0)); + Assert.assertNotNull(ih_5); // TODO why is this not used + il.append(new PUSH(_cp, 0)); + il.append(_factory.createNew(TEST_PACKAGE+".TestArrayAccess03")); + il.append(InstructionConst.DUP); + il.append(_factory.createInvoke(TEST_PACKAGE+".TestArrayAccess03", "", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL)); + il.append(InstructionConst.AASTORE); + InstructionHandle ih_15 = il.append(InstructionFactory.createReturn(Type.VOID)); + Assert.assertNotNull(ih_15); // TODO why is this not used + method.setMaxStack(); + method.setMaxLocals(); + _cg.addMethod(method.getMethod()); + il.dispose(); + } +} diff --git a/src/test/java/org/apache/bcel/verifier/tests/TestArrayAccess04Creator.java b/src/test/java/org/apache/bcel/verifier/tests/TestArrayAccess04Creator.java new file mode 100644 index 00000000..d4c2bf8b --- /dev/null +++ b/src/test/java/org/apache/bcel/verifier/tests/TestArrayAccess04Creator.java @@ -0,0 +1,96 @@ +/* + * 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.commons.bcel6.verifier.tests; + +import java.io.IOException; +import java.io.OutputStream; + +import org.apache.commons.bcel6.Const; +import org.apache.commons.bcel6.generic.ClassGen; +import org.apache.commons.bcel6.generic.ConstantPoolGen; +import org.apache.commons.bcel6.generic.InstructionConst; +import org.apache.commons.bcel6.generic.InstructionFactory; +import org.apache.commons.bcel6.generic.InstructionHandle; +import org.apache.commons.bcel6.generic.InstructionList; +import org.apache.commons.bcel6.generic.MethodGen; +import org.apache.commons.bcel6.generic.PUSH; +import org.apache.commons.bcel6.generic.Type; +import org.junit.Assert; + +public class TestArrayAccess04Creator extends TestCreator { + private final InstructionFactory _factory; + private final ConstantPoolGen _cp; + private final ClassGen _cg; + + public TestArrayAccess04Creator() { + _cg = new ClassGen(TEST_PACKAGE+".TestArrayAccess04", "java.lang.Object", "TestArrayAccess04.java", + Const.ACC_PUBLIC | Const.ACC_SUPER, new String[] { }); + + _cp = _cg.getConstantPool(); + _factory = new InstructionFactory(_cg, _cp); + } + + @Override +public void create(final OutputStream out) throws IOException { + createMethod_0(); + createMethod_1(); + _cg.getJavaClass().dump(out); + } + + private void createMethod_0() { + InstructionList il = new InstructionList(); + MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] { }, "", + TEST_PACKAGE+".TestArrayAccess04", il, _cp); + + InstructionHandle ih_0 = il.append(InstructionFactory.createLoad(Type.OBJECT, 0)); + Assert.assertNotNull(ih_0); // TODO why is this not used + il.append(_factory.createInvoke("java.lang.Object", "", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL)); + InstructionHandle ih_4 = il.append(InstructionFactory.createReturn(Type.VOID)); + Assert.assertNotNull(ih_4); // TODO why is this not used + method.setMaxStack(); + method.setMaxLocals(); + _cg.addMethod(method.getMethod()); + il.dispose(); + } + + private void createMethod_1() { + InstructionList il = new InstructionList(); + MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.VOID, new Type[] { Type.OBJECT }, + new String[] { "arg0" }, "test", TEST_PACKAGE+".TestArrayAccess04", il, _cp); + + InstructionHandle ih_0 = il.append(new PUSH(_cp, 1)); + Assert.assertNotNull(ih_0); // TODO why is this not used + il.append(_factory.createNewArray(Type.OBJECT, (short) 1)); + il.append(InstructionFactory.createStore(Type.OBJECT, 1)); + InstructionHandle ih_5 = il.append(new PUSH(_cp, 1)); + Assert.assertNotNull(ih_5); // TODO why is this not used + il.append(InstructionFactory.createStore(Type.INT, 2)); + InstructionHandle ih_7 = il.append(InstructionFactory.createLoad(Type.OBJECT, 1)); + Assert.assertNotNull(ih_7); // TODO why is this not used + il.append(new PUSH(_cp, 0)); + il.append(InstructionFactory.createLoad(Type.INT, 2)); + il.append(InstructionConst.AASTORE); + InstructionHandle ih_11 = il.append(InstructionFactory.createReturn(Type.VOID)); + Assert.assertNotNull(ih_11); // TODO why is this not used + method.setMaxStack(); + method.setMaxLocals(); + _cg.addMethod(method.getMethod()); + il.dispose(); + } +} diff --git a/src/test/java/org/apache/bcel/verifier/tests/TestCreator.java b/src/test/java/org/apache/bcel/verifier/tests/TestCreator.java new file mode 100644 index 00000000..1ae14722 --- /dev/null +++ b/src/test/java/org/apache/bcel/verifier/tests/TestCreator.java @@ -0,0 +1,64 @@ +package org.apache.commons.bcel6.verifier.tests; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.net.URISyntaxException; + +/* + * 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. + * + */ +public abstract class TestCreator { + + // Common package base name for generated test classes + protected static final String TEST_PACKAGE = TestCreator.class.getPackage().getName(); + + public void create() throws IOException { + File classFile = new File(getPackageFolder(), getClassName()); + FileOutputStream out = new FileOutputStream(classFile); + try { + create(out); + } + finally { + out.close(); + } + } + + private String getClassName() { + String name = getClass().getName(); + return name.substring(name.lastIndexOf('.')+1).replace("Creator", ".class"); + } + + private File getPackageFolder() throws IOException { + return new File(getClassesFolder(), getPackageName()); + } + + protected String getPackageName() { + return getClass().getPackage().getName().replace('.', '/'); + } + + private File getClassesFolder() throws IOException { + try { + return new File(getClass().getProtectionDomain().getCodeSource().getLocation().toURI()); + } catch (URISyntaxException e) { + throw new IOException(e); + } + } + + public abstract void create(OutputStream out) throws IOException; +} diff --git a/src/test/java/org/apache/bcel/verifier/tests/TestLegalInvokeInterface01.java b/src/test/java/org/apache/bcel/verifier/tests/TestLegalInvokeInterface01.java new file mode 100755 index 00000000..784ab213 --- /dev/null +++ b/src/test/java/org/apache/bcel/verifier/tests/TestLegalInvokeInterface01.java @@ -0,0 +1,29 @@ +/* + * 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.commons.bcel6.verifier.tests; + +public class TestLegalInvokeInterface01{ + + public static void test1(final Interface01 t){ + t.run(); + } +} + +interface Interface01 extends Runnable { + +} diff --git a/src/test/java/org/apache/bcel/verifier/tests/TestLegalInvokeSpecial01.java b/src/test/java/org/apache/bcel/verifier/tests/TestLegalInvokeSpecial01.java new file mode 100755 index 00000000..df428a75 --- /dev/null +++ b/src/test/java/org/apache/bcel/verifier/tests/TestLegalInvokeSpecial01.java @@ -0,0 +1,26 @@ +/* + * 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.commons.bcel6.verifier.tests; + +public class TestLegalInvokeSpecial01{ + + public static void test1(){ + new TestLegalInvokeSpecial01().getClass(); + } + +} diff --git a/src/test/java/org/apache/bcel/verifier/tests/TestLegalInvokeSpecial02.java b/src/test/java/org/apache/bcel/verifier/tests/TestLegalInvokeSpecial02.java new file mode 100755 index 00000000..346f762a --- /dev/null +++ b/src/test/java/org/apache/bcel/verifier/tests/TestLegalInvokeSpecial02.java @@ -0,0 +1,28 @@ +/* + * 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.commons.bcel6.verifier.tests; + +public abstract class TestLegalInvokeSpecial02 implements Runnable{ + + public static void test1(final TestLegalInvokeSpecial02 t, final int i){ + if(i > 0){ + t.run(); + } + } + +} diff --git a/src/test/java/org/apache/bcel/verifier/tests/TestLegalInvokeStatic01.java b/src/test/java/org/apache/bcel/verifier/tests/TestLegalInvokeStatic01.java new file mode 100755 index 00000000..927d8867 --- /dev/null +++ b/src/test/java/org/apache/bcel/verifier/tests/TestLegalInvokeStatic01.java @@ -0,0 +1,26 @@ +/* + * 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.commons.bcel6.verifier.tests; + +public class TestLegalInvokeStatic01 extends Thread{ + + public static void test1() throws InterruptedException{ + Thread.sleep(0); + } + +} diff --git a/src/test/java/org/apache/bcel/verifier/tests/TestLegalInvokeVirtual01.java b/src/test/java/org/apache/bcel/verifier/tests/TestLegalInvokeVirtual01.java new file mode 100755 index 00000000..a94066e2 --- /dev/null +++ b/src/test/java/org/apache/bcel/verifier/tests/TestLegalInvokeVirtual01.java @@ -0,0 +1,26 @@ +/* + * 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.commons.bcel6.verifier.tests; + +public class TestLegalInvokeVirtual01 { + + public static void test1(){ + new TestLegalInvokeVirtual01().toString(); + } + +} diff --git a/src/test/java/org/apache/bcel/verifier/tests/TestLegalInvokeVirtual02.java b/src/test/java/org/apache/bcel/verifier/tests/TestLegalInvokeVirtual02.java new file mode 100755 index 00000000..b5a579ba --- /dev/null +++ b/src/test/java/org/apache/bcel/verifier/tests/TestLegalInvokeVirtual02.java @@ -0,0 +1,28 @@ +/* + * 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.commons.bcel6.verifier.tests; + +public abstract class TestLegalInvokeVirtual02 implements Runnable{ + + public static void test1(final TestLegalInvokeVirtual02 t, final int i){ + if(i > 0){ + t.run(); + } + } + +} diff --git a/src/test/java/org/apache/bcel/verifier/tests/TestReturn01Creator.java b/src/test/java/org/apache/bcel/verifier/tests/TestReturn01Creator.java new file mode 100644 index 00000000..a2764d6e --- /dev/null +++ b/src/test/java/org/apache/bcel/verifier/tests/TestReturn01Creator.java @@ -0,0 +1,87 @@ +/* + * 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.commons.bcel6.verifier.tests; + +import java.io.IOException; +import java.io.OutputStream; + +import org.apache.commons.bcel6.Const; +import org.apache.commons.bcel6.generic.ClassGen; +import org.apache.commons.bcel6.generic.ConstantPoolGen; +import org.apache.commons.bcel6.generic.InstructionConst; +import org.apache.commons.bcel6.generic.InstructionFactory; +import org.apache.commons.bcel6.generic.InstructionHandle; +import org.apache.commons.bcel6.generic.InstructionList; +import org.apache.commons.bcel6.generic.MethodGen; +import org.apache.commons.bcel6.generic.Type; +import org.junit.Assert; + +public class TestReturn01Creator extends TestCreator { + private final InstructionFactory _factory; + private final ConstantPoolGen _cp; + private final ClassGen _cg; + + public TestReturn01Creator() { + _cg = new ClassGen(TEST_PACKAGE+".TestReturn01", "java.lang.Object", "TestReturn01.java", + Const.ACC_PUBLIC | Const.ACC_SUPER, new String[] { }); + + _cp = _cg.getConstantPool(); + _factory = new InstructionFactory(_cg, _cp); + } + + @Override +public void create(final OutputStream out) throws IOException { + createMethod_0(); + createMethod_1(); + _cg.getJavaClass().dump(out); + } + + private void createMethod_0() { + InstructionList il = new InstructionList(); + MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] { }, + "", TEST_PACKAGE+".TestReturn01", il, _cp); + + InstructionHandle ih_0 = il.append(InstructionFactory.createLoad(Type.OBJECT, 0)); + Assert.assertNotNull(ih_0); // TODO why is this not used + il.append(_factory.createInvoke("java.lang.Object", "", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL)); + InstructionHandle ih_4 = il.append(InstructionFactory.createReturn(Type.VOID)); + Assert.assertNotNull(ih_4); // TODO why is this not used + method.setMaxStack(); + method.setMaxLocals(); + _cg.addMethod(method.getMethod()); + il.dispose(); + } + + private void createMethod_1() { + InstructionList il = new InstructionList(); + MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.VOID, Type.NO_ARGS, + new String[] { }, "foo", TEST_PACKAGE+".TestReturn01", il, _cp); + + InstructionHandle ih_0 = il.append(_factory.createNew("java.lang.Object")); + Assert.assertNotNull(ih_0); // TODO why is this not used + il.append(InstructionConst.DUP); + il.append(_factory.createInvoke("java.lang.Object", "", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL)); + il.append(InstructionConst.NOP); + InstructionHandle ih_8 = il.append(InstructionFactory.createReturn(Type.OBJECT)); + Assert.assertNotNull(ih_8); // TODO why is this not used + method.setMaxStack(); + method.setMaxLocals(); + _cg.addMethod(method.getMethod()); + il.dispose(); + } +} diff --git a/src/test/java/org/apache/bcel/verifier/tests/TestReturn02.java b/src/test/java/org/apache/bcel/verifier/tests/TestReturn02.java new file mode 100644 index 00000000..0e44e3bd --- /dev/null +++ b/src/test/java/org/apache/bcel/verifier/tests/TestReturn02.java @@ -0,0 +1,65 @@ +/* + * 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.commons.bcel6.verifier.tests; + +public class TestReturn02 { + + public static String test1(final char[] data, final int offset, final int count) { + return new String(data, offset, count); + } + + public static Object test2(){ + return new Object(); + } + + public static boolean test3(){ + return true; + } + + public static byte test4(){ + return 1; + } + + public static short test5(){ + return 1; + } + + public static char test6(){ + return 'a'; + } + + public static int test7(){ + return 1; + } + + public static long test8(){ + return 1L; + } + + public static float test9(){ + return 1.0f; + } + + public static double test10(){ + return 1.0; + } + + public static Object test11(){ + return null; + } +} diff --git a/src/test/java/org/apache/bcel/verifier/tests/TestReturn03Creator.java b/src/test/java/org/apache/bcel/verifier/tests/TestReturn03Creator.java new file mode 100644 index 00000000..7e500d19 --- /dev/null +++ b/src/test/java/org/apache/bcel/verifier/tests/TestReturn03Creator.java @@ -0,0 +1,83 @@ +/* + * 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.commons.bcel6.verifier.tests; + +import java.io.IOException; +import java.io.OutputStream; + +import org.apache.commons.bcel6.Const; +import org.apache.commons.bcel6.generic.ClassGen; +import org.apache.commons.bcel6.generic.ConstantPoolGen; +import org.apache.commons.bcel6.generic.InstructionConst; +import org.apache.commons.bcel6.generic.InstructionFactory; +import org.apache.commons.bcel6.generic.InstructionHandle; +import org.apache.commons.bcel6.generic.InstructionList; +import org.apache.commons.bcel6.generic.MethodGen; +import org.apache.commons.bcel6.generic.Type; +import org.junit.Assert; + +public class TestReturn03Creator extends TestCreator { + private final InstructionFactory _factory; + private final ConstantPoolGen _cp; + private final ClassGen _cg; + + public TestReturn03Creator() { + _cg = new ClassGen(TEST_PACKAGE+".TestReturn03", "java.lang.Object", "TestReturn03.java", + Const.ACC_PUBLIC | Const.ACC_SUPER, new String[] { }); + + _cp = _cg.getConstantPool(); + _factory = new InstructionFactory(_cg, _cp); + } + + @Override +public void create(final OutputStream out) throws IOException { + createMethod_0(); + createMethod_1(); + _cg.getJavaClass().dump(out); + } + + private void createMethod_0() { + InstructionList il = new InstructionList(); + MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] { }, + "", TEST_PACKAGE+".TestReturn03", il, _cp); + + InstructionHandle ih_0 = il.append(InstructionFactory.createLoad(Type.OBJECT, 0)); + Assert.assertNotNull(ih_0); // TODO why is this not used + il.append(_factory.createInvoke("java.lang.Object", "", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL)); + InstructionHandle ih_4 = il.append(InstructionFactory.createReturn(Type.VOID)); + Assert.assertNotNull(ih_4); // TODO why is this not used + method.setMaxStack(); + method.setMaxLocals(); + _cg.addMethod(method.getMethod()); + il.dispose(); + } + + private void createMethod_1() { + InstructionList il = new InstructionList(); + MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.INT, Type.NO_ARGS, + new String[] { }, "test3", TEST_PACKAGE+".TestReturn03", il, _cp); + + InstructionHandle ih_0 = il.append(InstructionConst.ACONST_NULL); + Assert.assertNotNull(ih_0); // TODO why is this not used + il.append(InstructionFactory.createReturn(Type.OBJECT)); + method.setMaxStack(); + method.setMaxLocals(); + _cg.addMethod(method.getMethod()); + il.dispose(); + } +} diff --git a/src/test/java/org/apache/bcel/visitors/CounterVisitor.java b/src/test/java/org/apache/bcel/visitors/CounterVisitor.java new file mode 100644 index 00000000..cd578538 --- /dev/null +++ b/src/test/java/org/apache/bcel/visitors/CounterVisitor.java @@ -0,0 +1,423 @@ +/* + * 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.commons.bcel6.visitors; + +import org.apache.commons.bcel6.classfile.AnnotationDefault; +import org.apache.commons.bcel6.classfile.AnnotationEntry; +import org.apache.commons.bcel6.classfile.Annotations; +import org.apache.commons.bcel6.classfile.BootstrapMethods; +import org.apache.commons.bcel6.classfile.Code; +import org.apache.commons.bcel6.classfile.CodeException; +import org.apache.commons.bcel6.classfile.ConstantClass; +import org.apache.commons.bcel6.classfile.ConstantDouble; +import org.apache.commons.bcel6.classfile.ConstantFieldref; +import org.apache.commons.bcel6.classfile.ConstantFloat; +import org.apache.commons.bcel6.classfile.ConstantInteger; +import org.apache.commons.bcel6.classfile.ConstantInterfaceMethodref; +import org.apache.commons.bcel6.classfile.ConstantInvokeDynamic; +import org.apache.commons.bcel6.classfile.ConstantLong; +import org.apache.commons.bcel6.classfile.ConstantMethodHandle; +import org.apache.commons.bcel6.classfile.ConstantMethodType; +import org.apache.commons.bcel6.classfile.ConstantMethodref; +import org.apache.commons.bcel6.classfile.ConstantNameAndType; +import org.apache.commons.bcel6.classfile.ConstantPool; +import org.apache.commons.bcel6.classfile.ConstantString; +import org.apache.commons.bcel6.classfile.ConstantUtf8; +import org.apache.commons.bcel6.classfile.ConstantValue; +import org.apache.commons.bcel6.classfile.Deprecated; +import org.apache.commons.bcel6.classfile.EnclosingMethod; +import org.apache.commons.bcel6.classfile.ExceptionTable; +import org.apache.commons.bcel6.classfile.Field; +import org.apache.commons.bcel6.classfile.InnerClass; +import org.apache.commons.bcel6.classfile.InnerClasses; +import org.apache.commons.bcel6.classfile.JavaClass; +import org.apache.commons.bcel6.classfile.LineNumber; +import org.apache.commons.bcel6.classfile.LineNumberTable; +import org.apache.commons.bcel6.classfile.LocalVariable; +import org.apache.commons.bcel6.classfile.LocalVariableTable; +import org.apache.commons.bcel6.classfile.LocalVariableTypeTable; +import org.apache.commons.bcel6.classfile.Method; +import org.apache.commons.bcel6.classfile.MethodParameters; +import org.apache.commons.bcel6.classfile.ParameterAnnotationEntry; +import org.apache.commons.bcel6.classfile.ParameterAnnotations; +import org.apache.commons.bcel6.classfile.Signature; +import org.apache.commons.bcel6.classfile.SourceFile; +import org.apache.commons.bcel6.classfile.StackMap; +import org.apache.commons.bcel6.classfile.StackMapEntry; +import org.apache.commons.bcel6.classfile.Synthetic; +import org.apache.commons.bcel6.classfile.Unknown; +import org.apache.commons.bcel6.classfile.Visitor; + +public class CounterVisitor implements Visitor +{ + // CHECKSTYLE:OFF (public mutable fields in test code) + public int unknownCount = 0; + + public int syntheticCount = 0; + + public int stackMapEntryCount = 0; + + public int stackMapCount = 0; + + public int sourceFileCount = 0; + + public int signatureAnnotationCount = 0; + + public int parameterAnnotationCount = 0; + + public int methodCount = 0; + + public int localVariableTypeTableCount = 0; + + public int localVariableTableCount = 0; + + public int localVariableCount = 0; + + public int lineNumberTableCount = 0; + + public int lineNumberCount = 0; + + public int javaClassCount = 0; + + public int innerClassesCount = 0; + + public int innerClassCount = 0; + + public int fieldCount = 0; + + public int exceptionTableCount = 0; + + public int enclosingMethodCount = 0; + + public int deprecatedCount = 0; + + public int constantValueCount = 0; + + public int constantUtf8Count = 0; + + public int constantStringCount = 0; + + public int constantNameAndTypeCount = 0; + + public int constantPoolCount = 0; + + public int constantMethodrefCount = 0; + + public int constantLongCount = 0; + + public int constantIntegerCount = 0; + + public int constantInterfaceMethodrefCount = 0; + + public int constantFloatCount = 0; + + public int constantFieldrefCount = 0; + + public int constantClassCount = 0; + + public int constantDoubleCount = 0; + + public int codeExceptionCount = 0; + + public int codeCount = 0; + + public int annotationEntryCount = 0; + + public int annotationDefaultCount = 0; + + public int annotationCount = 0; + + /** @since 6.0 */ + public int bootstrapMethodsCount = 0; + + /** @since 6.0 */ + public int methodParametersCount = 0; + + /** @since 6.0 */ + public int constantInvokeDynamic = 0; + // CHECKSTYLE:ON + + + @Override + public void visitAnnotation(final Annotations obj) + { + annotationCount++; + } + + @Override + public void visitAnnotationDefault(final AnnotationDefault obj) + { + annotationDefaultCount++; + } + + @Override + public void visitAnnotationEntry(final AnnotationEntry obj) + { + annotationEntryCount++; + } + + @Override + public void visitCode(final Code obj) + { + codeCount++; + } + + @Override + public void visitCodeException(final CodeException obj) + { + codeExceptionCount++; + } + + @Override + public void visitConstantClass(final ConstantClass obj) + { + constantClassCount++; + } + + @Override + public void visitConstantDouble(final ConstantDouble obj) + { + constantDoubleCount++; + } + + @Override + public void visitConstantFieldref(final ConstantFieldref obj) + { + constantFieldrefCount++; + } + + @Override + public void visitConstantFloat(final ConstantFloat obj) + { + constantFloatCount++; + } + + @Override + public void visitConstantInteger(final ConstantInteger obj) + { + constantIntegerCount++; + } + + @Override + public void visitConstantInterfaceMethodref(final ConstantInterfaceMethodref obj) + { + constantInterfaceMethodrefCount++; + } + + @Override + public void visitConstantLong(final ConstantLong obj) + { + constantLongCount++; + } + + @Override + public void visitConstantMethodref(final ConstantMethodref obj) + { + constantMethodrefCount++; + } + + @Override + public void visitConstantNameAndType(final ConstantNameAndType obj) + { + constantNameAndTypeCount++; + } + + @Override + public void visitConstantPool(final ConstantPool obj) + { + constantPoolCount++; + } + + @Override + public void visitConstantString(final ConstantString obj) + { + constantStringCount++; + } + + @Override + public void visitConstantUtf8(final ConstantUtf8 obj) + { + constantUtf8Count++; + } + + @Override + public void visitConstantValue(final ConstantValue obj) + { + constantValueCount++; + } + + @Override + public void visitDeprecated(final Deprecated obj) + { + deprecatedCount++; + } + + @Override + public void visitEnclosingMethod(final EnclosingMethod obj) + { + enclosingMethodCount++; + } + + @Override + public void visitExceptionTable(final ExceptionTable obj) + { + exceptionTableCount++; + } + + @Override + public void visitField(final Field obj) + { + fieldCount++; + } + + @Override + public void visitInnerClass(final InnerClass obj) + { + innerClassCount++; + } + + @Override + public void visitInnerClasses(final InnerClasses obj) + { + innerClassesCount++; + } + + @Override + public void visitJavaClass(final JavaClass obj) + { + javaClassCount++; + } + + @Override + public void visitLineNumber(final LineNumber obj) + { + lineNumberCount++; + } + + @Override + public void visitLineNumberTable(final LineNumberTable obj) + { + lineNumberTableCount++; + } + + @Override + public void visitLocalVariable(final LocalVariable obj) + { + localVariableCount++; + } + + @Override + public void visitLocalVariableTable(final LocalVariableTable obj) + { + localVariableTableCount++; + } + + @Override + public void visitLocalVariableTypeTable(final LocalVariableTypeTable obj) + { + localVariableTypeTableCount++; + } + + @Override + public void visitMethod(final Method obj) + { + methodCount++; + } + + @Override + public void visitParameterAnnotation(final ParameterAnnotations obj) + { + parameterAnnotationCount++; + } + + @Override + public void visitSignature(final Signature obj) + { + signatureAnnotationCount++; + } + + @Override + public void visitSourceFile(final SourceFile obj) + { + sourceFileCount++; + } + + @Override + public void visitStackMap(final StackMap obj) + { + stackMapCount++; + } + + @Override + public void visitStackMapEntry(final StackMapEntry obj) + { + stackMapEntryCount++; + } + + @Override + public void visitSynthetic(final Synthetic obj) + { + syntheticCount++; + } + + @Override + public void visitUnknown(final Unknown obj) + { + unknownCount++; + } + + /** @since 6.0 */ + @Override + public void visitBootstrapMethods(final BootstrapMethods obj) + { + bootstrapMethodsCount++; + } + + /** @since 6.0 */ + @Override + public void visitMethodParameters(final MethodParameters obj) + { + methodParametersCount++; + } + + /** @since 6.0 */ + @Override + public void visitConstantInvokeDynamic(final ConstantInvokeDynamic obj) + { + constantInvokeDynamic++; + } + + /** @since 6.0 */ + @Override + public void visitConstantMethodType(final ConstantMethodType obj) { + // TODO Auto-generated method stub + } + + /** @since 6.0 */ + @Override + public void visitConstantMethodHandle(final ConstantMethodHandle constantMethodHandle) { + // TODO Auto-generated method stub + } + + /** @since 6.0 */ + @Override + public void visitParameterAnnotationEntry(final ParameterAnnotationEntry parameterAnnotationEntry) { + // TODO Auto-generated method stub + } +} diff --git a/src/test/java/org/apache/commons/bcel6/AbstractCounterVisitorTestCase.java b/src/test/java/org/apache/commons/bcel6/AbstractCounterVisitorTestCase.java deleted file mode 100644 index a8f8625b..00000000 --- a/src/test/java/org/apache/commons/bcel6/AbstractCounterVisitorTestCase.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * 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.commons.bcel6; - -import org.apache.commons.bcel6.classfile.DescendingVisitor; -import org.apache.commons.bcel6.classfile.JavaClass; -import org.apache.commons.bcel6.visitors.CounterVisitor; - -public abstract class AbstractCounterVisitorTestCase extends AbstractTestCase -{ - protected abstract JavaClass getTestClass() throws ClassNotFoundException; - - private CounterVisitor visitor = null; - - @Override - public void setUp() throws ClassNotFoundException - { - visitor = new CounterVisitor(); - new DescendingVisitor(getTestClass(), getVisitor()).visit(); - } - - public CounterVisitor getVisitor() - { - if (visitor == null) { - visitor = new CounterVisitor(); - } - return visitor; - } - - public void setVisitor(final CounterVisitor visitor) - { - this.visitor = visitor; - } -} diff --git a/src/test/java/org/apache/commons/bcel6/AbstractTestCase.java b/src/test/java/org/apache/commons/bcel6/AbstractTestCase.java deleted file mode 100644 index dbb80256..00000000 --- a/src/test/java/org/apache/commons/bcel6/AbstractTestCase.java +++ /dev/null @@ -1,208 +0,0 @@ -/* - * 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.commons.bcel6; - -import java.io.File; -import java.util.ArrayList; -import java.util.List; - -import org.apache.commons.bcel6.classfile.AnnotationEntry; -import org.apache.commons.bcel6.classfile.Attribute; -import org.apache.commons.bcel6.classfile.JavaClass; -import org.apache.commons.bcel6.classfile.Method; -import org.apache.commons.bcel6.generic.AnnotationEntryGen; -import org.apache.commons.bcel6.generic.ConstantPoolGen; -import org.apache.commons.bcel6.generic.ElementValueGen; -import org.apache.commons.bcel6.generic.ElementValuePairGen; -import org.apache.commons.bcel6.generic.ObjectType; -import org.apache.commons.bcel6.generic.SimpleElementValueGen; -import org.apache.commons.bcel6.util.ClassPath; -import org.apache.commons.bcel6.util.SyntheticRepository; - -import junit.framework.TestCase; - -public abstract class AbstractTestCase extends TestCase -{ - private static final boolean verbose = false; - - protected static final String PACKAGE_BASE_NAME = AbstractTestCase.class.getPackage().getName(); - - // Location of test data - protected static final File TESTDATA = new File("target", "testdata"); - - // package base name in signature format, i.e. with '/' separators instead of '.' - protected static final String PACKAGE_BASE_SIG = PACKAGE_BASE_NAME.replace('.', '/'); - - /** - * @param name - * @return Path to file under the TESTDATA directory - */ - protected File createTestdataFile(final String name) - { - return new File(TESTDATA, name); - } - - protected JavaClass getTestClass(final String name) throws ClassNotFoundException - { - return SyntheticRepository.getInstance().loadClass(name); - } - - protected Method getMethod(final JavaClass cl, final String methodname) - { - Method[] methods = cl.getMethods(); - for (Method m : methods) { - if (m.getName().equals(methodname)) - { - return m; - } - } - return null; - } - - /** - * Delete a file under the TESTDATA directory - * @param name - * @return - */ - protected boolean wipe(final String name) - { - return new File(TESTDATA, name).delete(); - } - - /** - * Delete a directory and file under the TESTDATA directory - * @param dir - * @param name - * @return true if the file was deleted - */ - protected boolean wipe(final String dir, final String name) - { - // The parameter is relative to the TESTDATA dir - boolean b = wipe(dir + File.separator + name); - final File testDir = new File(TESTDATA, dir); - String[] files = testDir.list(); - if (files == null || files.length == 0) - { - if (!testDir.delete()){ - System.err.println("Failed to remove: " + testDir); - } - } else { - System.err.println("Non-empty directory: " + testDir); - } - return b; - } - - public SyntheticRepository createRepos(final String cpentry) - { - ClassPath cp = new ClassPath("target" + File.separator + "testdata" - + File.separator + cpentry + File.separator); - return SyntheticRepository.getInstance(cp); - } - - protected Attribute[] findAttribute(final String name, final JavaClass clazz) - { - Attribute[] all = clazz.getAttributes(); - List chosenAttrsList = new ArrayList<>(); - for (Attribute element : all) { - if (verbose) { - System.err.println("Attribute: " + element.getName()); - } - if (element.getName().equals(name)) { - chosenAttrsList.add(element); - } - } - return chosenAttrsList.toArray(new Attribute[] {}); - } - - protected Attribute findAttribute(final String name, final Attribute[] all) - { - List chosenAttrsList = new ArrayList<>(); - for (Attribute element : all) { - if (verbose) { - System.err.println("Attribute: " + element.getName()); - } - if (element.getName().equals(name)) { - chosenAttrsList.add(element); - } - } - assertTrue("Should be one match: " + chosenAttrsList.size(), - chosenAttrsList.size() == 1); - return chosenAttrsList.get(0); - } - - protected String dumpAttributes(final Attribute[] as) - { - StringBuilder result = new StringBuilder(); - result.append("AttributeArray:["); - for (int i = 0; i < as.length; i++) - { - Attribute attr = as[i]; - result.append(attr.toString()); - if (i + 1 < as.length) { - result.append(","); - } - } - result.append("]"); - return result.toString(); - } - - protected String dumpAnnotationEntries(final AnnotationEntry[] as) - { - StringBuilder result = new StringBuilder(); - result.append("["); - for (int i = 0; i < as.length; i++) - { - AnnotationEntry annotation = as[i]; - result.append(annotation.toShortString()); - if (i + 1 < as.length) { - result.append(","); - } - } - result.append("]"); - return result.toString(); - } - - protected String dumpAnnotationEntries(final AnnotationEntryGen[] as) - { - StringBuilder result = new StringBuilder(); - result.append("["); - for (int i = 0; i < as.length; i++) - { - AnnotationEntryGen annotation = as[i]; - result.append(annotation.toShortString()); - if (i + 1 < as.length) { - result.append(","); - } - } - result.append("]"); - return result.toString(); - } - - public AnnotationEntryGen createFruitAnnotationEntry(final ConstantPoolGen cp, - final String aFruit, final boolean visibility) - { - SimpleElementValueGen evg = new SimpleElementValueGen( - ElementValueGen.STRING, cp, aFruit); - ElementValuePairGen nvGen = new ElementValuePairGen("fruit", evg, cp); - ObjectType t = new ObjectType("SimpleStringAnnotation"); - List elements = new ArrayList<>(); - elements.add(nvGen); - return new AnnotationEntryGen(t, elements, visibility, cp); - } -} diff --git a/src/test/java/org/apache/commons/bcel6/AnnotationAccessFlagTestCase.java b/src/test/java/org/apache/commons/bcel6/AnnotationAccessFlagTestCase.java deleted file mode 100644 index eac7f95c..00000000 --- a/src/test/java/org/apache/commons/bcel6/AnnotationAccessFlagTestCase.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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.commons.bcel6; - -import org.apache.commons.bcel6.classfile.JavaClass; - -public class AnnotationAccessFlagTestCase extends AbstractTestCase -{ - /** - * If you write an annotation and compile it, the class file generated - * should be marked as an annotation type - which is detectable through - * BCEL. - */ - public void testAnnotationClassSaysItIs() throws ClassNotFoundException - { - JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.SimpleAnnotation"); - assertTrue( - "Expected SimpleAnnotation class to say it was an annotation - but it didn't !", - clazz.isAnnotation()); - clazz = getTestClass(PACKAGE_BASE_NAME+".data.SimpleClass"); - assertTrue( - "Expected SimpleClass class to say it was not an annotation - but it didn't !", - !clazz.isAnnotation()); - } -} diff --git a/src/test/java/org/apache/commons/bcel6/AnnotationDefaultAttributeTestCase.java b/src/test/java/org/apache/commons/bcel6/AnnotationDefaultAttributeTestCase.java deleted file mode 100644 index 592e0f21..00000000 --- a/src/test/java/org/apache/commons/bcel6/AnnotationDefaultAttributeTestCase.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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.commons.bcel6; - -import org.apache.commons.bcel6.classfile.AnnotationDefault; -import org.apache.commons.bcel6.classfile.ElementValue; -import org.apache.commons.bcel6.classfile.JavaClass; -import org.apache.commons.bcel6.classfile.Method; -import org.apache.commons.bcel6.classfile.SimpleElementValue; - -public class AnnotationDefaultAttributeTestCase extends AbstractTestCase -{ - /** - * For values in an annotation that have default values, we should be able - * to query the AnnotationDefault attribute against the method to discover - * the default value that was originally declared. - */ - public void testMethodAnnotations() throws ClassNotFoundException - { - JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.SimpleAnnotation"); - Method m = getMethod(clazz, "fruit"); - AnnotationDefault a = (AnnotationDefault) findAttribute( - "AnnotationDefault", m.getAttributes()); - SimpleElementValue val = (SimpleElementValue) a.getDefaultValue(); - assertTrue("Should be STRING but is " + val.getElementValueType(), val - .getElementValueType() == ElementValue.STRING); - assertTrue("Should have default of bananas but default is " - + val.getValueString(), val.getValueString().equals("bananas")); - } -} diff --git a/src/test/java/org/apache/commons/bcel6/AnonymousClassTestCase.java b/src/test/java/org/apache/commons/bcel6/AnonymousClassTestCase.java deleted file mode 100644 index fcbd66b3..00000000 --- a/src/test/java/org/apache/commons/bcel6/AnonymousClassTestCase.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * 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.commons.bcel6; - -import org.apache.commons.bcel6.classfile.JavaClass; - -public class AnonymousClassTestCase extends AbstractTestCase -{ - public void testRegularClassIsNotAnonymous() throws ClassNotFoundException - { - JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.AnonymousClassTest"); - assertFalse("regular outer classes are not anonymous", clazz - .isAnonymous()); - assertFalse("regular outer classes are not nested", clazz.isNested()); - } - - public void testNamedInnerClassIsNotAnonymous() - throws ClassNotFoundException - { - JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.AnonymousClassTest$X"); - assertFalse("regular inner classes are not anonymous", clazz - .isAnonymous()); - assertTrue("regular inner classes are nested", clazz.isNested()); - } - - public void testStaticInnerClassIsNotAnonymous() - throws ClassNotFoundException - { - JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.AnonymousClassTest$Y"); - assertFalse("regular static inner classes are not anonymous", clazz - .isAnonymous()); - assertTrue("regular static inner classes are nested", clazz.isNested()); - } - - public void testAnonymousInnerClassIsAnonymous() - throws ClassNotFoundException - { - JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.AnonymousClassTest$1"); - assertTrue("anonymous inner classes are anonymous", clazz.isAnonymous()); - assertTrue("anonymous inner classes are anonymous", clazz.isNested()); - } -} diff --git a/src/test/java/org/apache/commons/bcel6/BCELBenchmark.java b/src/test/java/org/apache/commons/bcel6/BCELBenchmark.java deleted file mode 100644 index 34589fbe..00000000 --- a/src/test/java/org/apache/commons/bcel6/BCELBenchmark.java +++ /dev/null @@ -1,126 +0,0 @@ -/* - * 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.commons.bcel6; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.util.concurrent.TimeUnit; -import java.util.jar.JarEntry; -import java.util.jar.JarFile; - -import org.apache.commons.bcel6.classfile.ClassParser; -import org.apache.commons.bcel6.classfile.JavaClass; -import org.apache.commons.bcel6.classfile.Method; -import org.apache.commons.bcel6.generic.ClassGen; -import org.apache.commons.bcel6.generic.InstructionList; -import org.apache.commons.bcel6.generic.MethodGen; -import org.apache.commons.collections4.Predicate; -import org.apache.commons.collections4.iterators.EnumerationIterator; -import org.apache.commons.collections4.iterators.FilterIterator; -import org.apache.commons.collections4.iterators.IteratorIterable; -import org.apache.commons.io.IOUtils; -import org.openjdk.jmh.annotations.Benchmark; -import org.openjdk.jmh.annotations.BenchmarkMode; -import org.openjdk.jmh.annotations.Fork; -import org.openjdk.jmh.annotations.Measurement; -import org.openjdk.jmh.annotations.Mode; -import org.openjdk.jmh.annotations.OutputTimeUnit; -import org.openjdk.jmh.annotations.Threads; -import org.openjdk.jmh.annotations.Warmup; -import org.openjdk.jmh.infra.Blackhole; - -@BenchmarkMode(Mode.AverageTime) -@Fork(value = 1, jvmArgs = "-server") -@Threads(1) -@Warmup(iterations = 10) -@Measurement(iterations = 20) -@OutputTimeUnit(TimeUnit.MILLISECONDS) -public class BCELBenchmark { - - private JarFile getJarFile() throws IOException { - String javaHome = System.getProperty("java.home"); - return new JarFile(javaHome + "/lib/rt.jar"); - } - - private Iterable getClasses(JarFile jar) { - return new IteratorIterable<>(new FilterIterator<>(new EnumerationIterator<>(jar.entries()), new Predicate() { - @Override - public boolean evaluate(JarEntry entry) { - return entry.getName().endsWith(".class"); - } - })); - } - - /** - * Baseline benchmark. Read the classes but don't parse them. - */ - @Benchmark - public void baseline(Blackhole bh) throws IOException { - JarFile jar = getJarFile(); - - for (JarEntry entry : getClasses(jar)) { - byte[] bytes = IOUtils.toByteArray(jar.getInputStream(entry)); - bh.consume(bytes); - } - - jar.close(); - } - - @Benchmark - public void parser(Blackhole bh) throws IOException { - JarFile jar = getJarFile(); - - for (JarEntry entry : getClasses(jar)) { - byte[] bytes = IOUtils.toByteArray(jar.getInputStream(entry)); - - JavaClass clazz = new ClassParser(new ByteArrayInputStream(bytes), entry.getName()).parse(); - bh.consume(clazz); - } - - jar.close(); - } - - @Benchmark - public void generator(Blackhole bh) throws IOException { - JarFile jar = getJarFile(); - - for (JarEntry entry : getClasses(jar)) { - byte[] bytes = IOUtils.toByteArray(jar.getInputStream(entry)); - - JavaClass clazz = new ClassParser(new ByteArrayInputStream(bytes), entry.getName()).parse(); - - ClassGen cg = new ClassGen(clazz); - - for (Method m : cg.getMethods()) { - MethodGen mg = new MethodGen(m, cg.getClassName(), cg.getConstantPool()); - InstructionList il = mg.getInstructionList(); - - if (il != null) { - mg.getInstructionList().setPositions(); - mg.setMaxLocals(); - mg.setMaxStack(); - } - cg.replaceMethod(m, mg.getMethod()); - } - - bh.consume(cg.getJavaClass().getBytes()); - } - - jar.close(); - } -} diff --git a/src/test/java/org/apache/commons/bcel6/CounterVisitorTestCase.java b/src/test/java/org/apache/commons/bcel6/CounterVisitorTestCase.java deleted file mode 100644 index 31db773a..00000000 --- a/src/test/java/org/apache/commons/bcel6/CounterVisitorTestCase.java +++ /dev/null @@ -1,215 +0,0 @@ -/* - * 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.commons.bcel6; - -import org.apache.commons.bcel6.classfile.JavaClass; - -public class CounterVisitorTestCase extends AbstractCounterVisitorTestCase -{ - @Override - protected JavaClass getTestClass() throws ClassNotFoundException - { - return getTestClass(PACKAGE_BASE_NAME+".data.MarkedType"); - } - - public void testAnnotationsCount() - { - assertEquals("annotationCount", 2, getVisitor().annotationCount); - } - - public void testAnnotationDefaultCount() - { - assertEquals("annotationDefaultCount", 0, getVisitor().annotationDefaultCount); - } - - public void testAnnotationEntryCount() - { - assertEquals("annotationEntryCount", 2, getVisitor().annotationEntryCount); - } - - public void testCodeCount() - { - assertEquals("codeCount", 1, getVisitor().codeCount); - } - - public void testCodeExceptionCount() - { - assertEquals("codeExceptionCount", 0, getVisitor().codeExceptionCount); - } - - public void testConstantClassCount() - { - assertEquals("constantClassCount", 2, getVisitor().constantClassCount); - } - - public void testConstantDoubleCount() - { - assertEquals("constantDoubleCount", 0, getVisitor().constantDoubleCount); - } - - public void testConstantFieldrefCount() - { - assertEquals("constantFieldrefCount", 0, getVisitor().constantFieldrefCount); - } - - public void testConstantFloatCount() - { - assertEquals("constantFloatCount", 0, getVisitor().constantFloatCount); - } - - public void testConstantIntegerCount() - { - assertEquals("constantIntegerCount", 0, getVisitor().constantIntegerCount); - } - - public void testConstantInterfaceMethodrefCount() - { - assertEquals("constantInterfaceMethodrefCount", 0, getVisitor().constantInterfaceMethodrefCount); - } - - public void testConstantLongCount() - { - assertEquals("constantLongCount", 0, getVisitor().constantLongCount); - } - - public void testConstantMethodrefCount() - { - assertEquals("constantMethodrefCount", 1, getVisitor().constantMethodrefCount); - } - - public void testConstantNameAndTypeCount() - { - assertEquals("constantNameAndTypeCount", 1, getVisitor().constantNameAndTypeCount); - } - - public void testConstantPoolCount() - { - assertEquals("constantPoolCount", 1, getVisitor().constantPoolCount); - } - - public void testConstantStringCount() - { - assertEquals("constantStringCount", 0, getVisitor().constantStringCount); - } - - public void testConstantValueCount() - { - assertEquals("constantValueCount", 0, getVisitor().constantValueCount); - } - - public void testDeprecatedCount() - { - assertEquals("deprecatedCount", 0, getVisitor().deprecatedCount); - } - - public void testEnclosingMethodCount() - { - assertEquals("enclosingMethodCount", 0, getVisitor().enclosingMethodCount); - } - - public void testExceptionTableCount() - { - assertEquals("exceptionTableCount", 0, getVisitor().exceptionTableCount); - } - - public void testFieldCount() - { - assertEquals("fieldCount", 0, getVisitor().fieldCount); - } - - public void testInnerClassCount() - { - assertEquals("innerClassCount", 0, getVisitor().innerClassCount); - } - - public void testInnerClassesCount() - { - assertEquals("innerClassesCount", 0, getVisitor().innerClassesCount); - } - - public void testJavaClassCount() - { - assertEquals("javaClassCount", 1, getVisitor().javaClassCount); - } - - public void testLineNumberCount() - { - assertEquals("lineNumberCount", 1, getVisitor().lineNumberCount); - } - - public void testLineNumberTableCount() - { - assertEquals("lineNumberTableCount", 1, getVisitor().lineNumberTableCount); - } - - public void testLocalVariableCount() - { - assertEquals("localVariableCount", 1, getVisitor().localVariableCount); - } - - public void testLocalVariableTableCount() - { - assertEquals("localVariableTableCount", 1, getVisitor().localVariableTableCount); - } - - public void testLocalVariableTypeTableCount() - { - assertEquals("localVariableTypeTableCount", 0, getVisitor().localVariableTypeTableCount); - } - - public void testMethodCount() - { - assertEquals("methodCount", 1, getVisitor().methodCount); - } - - public void testParameterAnnotationCount() - { - assertEquals("parameterAnnotationCount", 0, getVisitor().parameterAnnotationCount); - } - - public void testSignatureCount() - { - assertEquals("signatureAnnotationCount", 0, getVisitor().signatureAnnotationCount); - } - - public void testSourceFileCount() - { - assertEquals("sourceFileCount", 1, getVisitor().sourceFileCount); - } - - public void testStackMapCount() - { - assertEquals("stackMapCount", 0, getVisitor().stackMapCount); - } - - public void testStackMapEntryCount() - { - assertEquals("stackMapEntryCount", 0, getVisitor().stackMapEntryCount); - } - - public void testSyntheticCount() - { - assertEquals("syntheticCount", 0, getVisitor().syntheticCount); - } - - public void testUnknownCount() - { - assertEquals("unknownCount", 0, getVisitor().unknownCount); - } -} diff --git a/src/test/java/org/apache/commons/bcel6/ElementValueGenTestCase.java b/src/test/java/org/apache/commons/bcel6/ElementValueGenTestCase.java deleted file mode 100644 index 1e8db56d..00000000 --- a/src/test/java/org/apache/commons/bcel6/ElementValueGenTestCase.java +++ /dev/null @@ -1,240 +0,0 @@ -/* - * 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.commons.bcel6; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -import org.apache.commons.bcel6.generic.ClassElementValueGen; -import org.apache.commons.bcel6.generic.ClassGen; -import org.apache.commons.bcel6.generic.ConstantPoolGen; -import org.apache.commons.bcel6.generic.ElementValueGen; -import org.apache.commons.bcel6.generic.EnumElementValueGen; -import org.apache.commons.bcel6.generic.ObjectType; -import org.apache.commons.bcel6.generic.SimpleElementValueGen; - -public class ElementValueGenTestCase extends AbstractTestCase -{ - private ClassGen createClassGen(final String classname) - { - return new ClassGen(classname, "java.lang.Object", "", - Const.ACC_PUBLIC | Const.ACC_SUPER, null); - } - - /** - * Create primitive element values - */ - public void testCreateIntegerElementValue() - { - ClassGen cg = createClassGen("HelloWorld"); - ConstantPoolGen cp = cg.getConstantPool(); - SimpleElementValueGen evg = new SimpleElementValueGen( - ElementValueGen.PRIMITIVE_INT, cp, 555); - // Creation of an element like that should leave a new entry in the - // cpool - assertTrue("Should have the same index in the constantpool but " - + evg.getIndex() + "!=" + cp.lookupInteger(555), - evg.getIndex() == cp.lookupInteger(555)); - checkSerialize(evg, cp); - } - - public void testCreateFloatElementValue() - { - ClassGen cg = createClassGen("HelloWorld"); - ConstantPoolGen cp = cg.getConstantPool(); - SimpleElementValueGen evg = new SimpleElementValueGen( - ElementValueGen.PRIMITIVE_FLOAT, cp, 111.222f); - // Creation of an element like that should leave a new entry in the - // cpool - assertTrue("Should have the same index in the constantpool but " - + evg.getIndex() + "!=" + cp.lookupFloat(111.222f), evg - .getIndex() == cp.lookupFloat(111.222f)); - checkSerialize(evg, cp); - } - - public void testCreateDoubleElementValue() - { - ClassGen cg = createClassGen("HelloWorld"); - ConstantPoolGen cp = cg.getConstantPool(); - SimpleElementValueGen evg = new SimpleElementValueGen( - ElementValueGen.PRIMITIVE_DOUBLE, cp, 333.44); - // Creation of an element like that should leave a new entry in the - // cpool - int idx = cp.lookupDouble(333.44); - assertTrue("Should have the same index in the constantpool but " - + evg.getIndex() + "!=" + idx, evg.getIndex() == idx); - checkSerialize(evg, cp); - } - - public void testCreateLongElementValue() - { - ClassGen cg = createClassGen("HelloWorld"); - ConstantPoolGen cp = cg.getConstantPool(); - SimpleElementValueGen evg = new SimpleElementValueGen( - ElementValueGen.PRIMITIVE_LONG, cp, 3334455L); - // Creation of an element like that should leave a new entry in the - // cpool - int idx = cp.lookupLong(3334455L); - assertTrue("Should have the same index in the constantpool but " - + evg.getIndex() + "!=" + idx, evg.getIndex() == idx); - checkSerialize(evg, cp); - } - - public void testCreateCharElementValue() - { - ClassGen cg = createClassGen("HelloWorld"); - ConstantPoolGen cp = cg.getConstantPool(); - SimpleElementValueGen evg = new SimpleElementValueGen( - ElementValueGen.PRIMITIVE_CHAR, cp, 't'); - // Creation of an element like that should leave a new entry in the - // cpool - int idx = cp.lookupInteger('t'); - assertTrue("Should have the same index in the constantpool but " - + evg.getIndex() + "!=" + idx, evg.getIndex() == idx); - checkSerialize(evg, cp); - } - - public void testCreateByteElementValue() - { - ClassGen cg = createClassGen("HelloWorld"); - ConstantPoolGen cp = cg.getConstantPool(); - SimpleElementValueGen evg = new SimpleElementValueGen( - ElementValueGen.PRIMITIVE_CHAR, cp, (byte) 'z'); - // Creation of an element like that should leave a new entry in the - // cpool - int idx = cp.lookupInteger((byte) 'z'); - assertTrue("Should have the same index in the constantpool but " - + evg.getIndex() + "!=" + idx, evg.getIndex() == idx); - checkSerialize(evg, cp); - } - - public void testCreateBooleanElementValue() - { - ClassGen cg = createClassGen("HelloWorld"); - ConstantPoolGen cp = cg.getConstantPool(); - SimpleElementValueGen evg = new SimpleElementValueGen( - ElementValueGen.PRIMITIVE_BOOLEAN, cp, true); - // Creation of an element like that should leave a new entry in the - // cpool - int idx = cp.lookupInteger(1); // 1 == true - assertTrue("Should have the same index in the constantpool but " - + evg.getIndex() + "!=" + idx, evg.getIndex() == idx); - checkSerialize(evg, cp); - } - - public void testCreateShortElementValue() - { - ClassGen cg = createClassGen("HelloWorld"); - ConstantPoolGen cp = cg.getConstantPool(); - SimpleElementValueGen evg = new SimpleElementValueGen( - ElementValueGen.PRIMITIVE_SHORT, cp, (short) 42); - // Creation of an element like that should leave a new entry in the - // cpool - int idx = cp.lookupInteger(42); - assertTrue("Should have the same index in the constantpool but " - + evg.getIndex() + "!=" + idx, evg.getIndex() == idx); - checkSerialize(evg, cp); - } - - // // - // Create string element values - public void testCreateStringElementValue() - { - // Create HelloWorld - ClassGen cg = createClassGen("HelloWorld"); - ConstantPoolGen cp = cg.getConstantPool(); - SimpleElementValueGen evg = new SimpleElementValueGen( - ElementValueGen.STRING, cp, "hello"); - // Creation of an element like that should leave a new entry in the - // cpool - assertTrue("Should have the same index in the constantpool but " - + evg.getIndex() + "!=" + cp.lookupUtf8("hello"), evg - .getIndex() == cp.lookupUtf8("hello")); - checkSerialize(evg, cp); - } - - // // - // Create enum element value - public void testCreateEnumElementValue() - { - ClassGen cg = createClassGen("HelloWorld"); - ConstantPoolGen cp = cg.getConstantPool(); - ObjectType enumType = new ObjectType("SimpleEnum"); // Supports rainbow - // :) - EnumElementValueGen evg = new EnumElementValueGen(enumType, "Red", cp); - // Creation of an element like that should leave a new entry in the - // cpool - assertTrue( - "The new ElementValue value index should match the contents of the constantpool but " - + evg.getValueIndex() + "!=" + cp.lookupUtf8("Red"), - evg.getValueIndex() == cp.lookupUtf8("Red")); - // BCELBUG: Should the class signature or class name be in the constant - // pool? (see note in ConstantPool) - // assertTrue("The new ElementValue type index should match the contents - // of the constantpool but "+ - // evg.getTypeIndex()+"!="+cp.lookupClass(enumType.getSignature()), - // evg.getTypeIndex()==cp.lookupClass(enumType.getSignature())); - checkSerialize(evg, cp); - } - - // // - // Create class element value - public void testCreateClassElementValue() - { - ClassGen cg = createClassGen("HelloWorld"); - ConstantPoolGen cp = cg.getConstantPool(); - ObjectType classType = new ObjectType("java.lang.Integer"); - ClassElementValueGen evg = new ClassElementValueGen(classType, cp); - assertTrue("Unexpected value for contained class: '" - + evg.getClassString() + "'", evg.getClassString().contains("Integer")); - checkSerialize(evg, cp); - } - - private void checkSerialize(final ElementValueGen evgBefore, final ConstantPoolGen cpg) - { - try - { - String beforeValue = evgBefore.stringifyValue(); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - DataOutputStream dos = new DataOutputStream(baos); - evgBefore.dump(dos); - dos.flush(); - dos.close(); - byte[] bs = baos.toByteArray(); - ByteArrayInputStream bais = new ByteArrayInputStream(bs); - DataInputStream dis = new DataInputStream(bais); - ElementValueGen evgAfter = ElementValueGen.readElementValue(dis, - cpg); - dis.close(); - String afterValue = evgAfter.stringifyValue(); - if (!beforeValue.equals(afterValue)) - { - fail("Deserialization failed: before='" + beforeValue - + "' after='" + afterValue + "'"); - } - } - catch (IOException ioe) - { - fail("Unexpected exception whilst checking serialization: " + ioe); - } - } -} diff --git a/src/test/java/org/apache/commons/bcel6/EnclosingMethodAttributeTestCase.java b/src/test/java/org/apache/commons/bcel6/EnclosingMethodAttributeTestCase.java deleted file mode 100644 index c6ed1f57..00000000 --- a/src/test/java/org/apache/commons/bcel6/EnclosingMethodAttributeTestCase.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * 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.commons.bcel6; - -import java.io.File; -import java.io.IOException; - -import org.apache.commons.bcel6.classfile.Attribute; -import org.apache.commons.bcel6.classfile.ConstantPool; -import org.apache.commons.bcel6.classfile.EnclosingMethod; -import org.apache.commons.bcel6.classfile.JavaClass; -import org.apache.commons.bcel6.util.SyntheticRepository; -import org.junit.Assert; - -public class EnclosingMethodAttributeTestCase extends AbstractTestCase -{ - /** - * Verify for an inner class declared inside the 'main' method that the - * enclosing method attribute is set correctly. - */ - public void testCheckMethodLevelNamedInnerClass() - throws ClassNotFoundException - { - JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.AttributeTestClassEM01$1S"); - ConstantPool pool = clazz.getConstantPool(); - Attribute[] encMethodAttrs = findAttribute("EnclosingMethod", clazz); - assertTrue("Expected 1 EnclosingMethod attribute but found " - + encMethodAttrs.length, encMethodAttrs.length == 1); - EnclosingMethod em = (EnclosingMethod) encMethodAttrs[0]; - String enclosingClassName = em.getEnclosingClass().getBytes(pool); - String enclosingMethodName = em.getEnclosingMethod().getName(pool); - assertTrue( - "Expected class name to be '"+PACKAGE_BASE_SIG+"/data/AttributeTestClassEM01' but was " - + enclosingClassName, enclosingClassName - .equals(PACKAGE_BASE_SIG+"/data/AttributeTestClassEM01")); - assertTrue("Expected method name to be 'main' but was " - + enclosingMethodName, enclosingMethodName.equals("main")); - } - - /** - * Verify for an inner class declared at the type level that the - * EnclosingMethod attribute is set correctly (i.e. to a null value) - */ - public void testCheckClassLevelNamedInnerClass() - throws ClassNotFoundException - { - JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.AttributeTestClassEM02$1"); - ConstantPool pool = clazz.getConstantPool(); - Attribute[] encMethodAttrs = findAttribute("EnclosingMethod", clazz); - assertTrue("Expected 1 EnclosingMethod attribute but found " - + encMethodAttrs.length, encMethodAttrs.length == 1); - EnclosingMethod em = (EnclosingMethod) encMethodAttrs[0]; - String enclosingClassName = em.getEnclosingClass().getBytes(pool); - assertTrue( - "The class is not within a method, so method_index should be null, but it is " - + em.getEnclosingMethodIndex(), em - .getEnclosingMethodIndex() == 0); - assertTrue( - "Expected class name to be '"+PACKAGE_BASE_SIG+"/data/AttributeTestClassEM02' but was " - + enclosingClassName, enclosingClassName - .equals(PACKAGE_BASE_SIG+"/data/AttributeTestClassEM02")); - } - - /** - * Check that we can save and load the attribute correctly. - */ - public void testAttributeSerializtion() throws ClassNotFoundException, - IOException - { - JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.AttributeTestClassEM02$1"); - ConstantPool pool = clazz.getConstantPool(); - Attribute[] encMethodAttrs = findAttribute("EnclosingMethod", clazz); - assertTrue("Expected 1 EnclosingMethod attribute but found " - + encMethodAttrs.length, encMethodAttrs.length == 1); - // Write it out - File tfile = createTestdataFile("AttributeTestClassEM02$1.class"); - clazz.dump(tfile); - // Read in the new version and check it is OK - SyntheticRepository repos2 = createRepos("."); - JavaClass clazz2 = repos2.loadClass("AttributeTestClassEM02$1"); - Assert.assertNotNull(clazz2); // Use the variable to avoid a warning - EnclosingMethod em = (EnclosingMethod) encMethodAttrs[0]; - String enclosingClassName = em.getEnclosingClass().getBytes(pool); - assertTrue( - "The class is not within a method, so method_index should be null, but it is " - + em.getEnclosingMethodIndex(), em - .getEnclosingMethodIndex() == 0); - assertTrue( - "Expected class name to be '"+PACKAGE_BASE_SIG+"/data/AttributeTestClassEM02' but was " - + enclosingClassName, enclosingClassName - .equals(PACKAGE_BASE_SIG+"/data/AttributeTestClassEM02")); - tfile.deleteOnExit(); - } -} diff --git a/src/test/java/org/apache/commons/bcel6/EnumAccessFlagTestCase.java b/src/test/java/org/apache/commons/bcel6/EnumAccessFlagTestCase.java deleted file mode 100644 index 035dcb50..00000000 --- a/src/test/java/org/apache/commons/bcel6/EnumAccessFlagTestCase.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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.commons.bcel6; - -import org.apache.commons.bcel6.classfile.JavaClass; - -public class EnumAccessFlagTestCase extends AbstractTestCase -{ - /** - * An enumerated type, once compiled, should result in a class file that is - * marked such that we can determine from the access flags (through BCEL) - * that it was originally an enum type declaration. - */ - public void testEnumClassSaysItIs() throws ClassNotFoundException - { - JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.SimpleEnum"); - assertTrue( - "Expected SimpleEnum class to say it was an enum - but it didn't !", - clazz.isEnum()); - clazz = getTestClass(PACKAGE_BASE_NAME+".data.SimpleClass"); - assertTrue( - "Expected SimpleClass class to say it was not an enum - but it didn't !", - !clazz.isEnum()); - } -} diff --git a/src/test/java/org/apache/commons/bcel6/InstructionFinderTestCase.java b/src/test/java/org/apache/commons/bcel6/InstructionFinderTestCase.java deleted file mode 100644 index ad3cb955..00000000 --- a/src/test/java/org/apache/commons/bcel6/InstructionFinderTestCase.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * 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.commons.bcel6; - -import java.util.Iterator; - -import org.apache.commons.bcel6.classfile.JavaClass; -import org.apache.commons.bcel6.classfile.Method; -import org.apache.commons.bcel6.generic.InstructionHandle; -import org.apache.commons.bcel6.generic.InstructionList; -import org.apache.commons.bcel6.util.InstructionFinder; - -public class InstructionFinderTestCase extends AbstractTestCase -{ - public void testSearchAll() throws Exception - { - JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".util.InstructionFinder"); - Method[] methods = clazz.getMethods(); - Method searchM = null; - for (Method m : methods) - { - if (m.getName().equals("search") && (m.getArgumentTypes().length == 3)) - { - searchM = m; - break; - } - } - - if (searchM == null) { - throw new Exception("search method not found"); - } - - byte[] bytes = searchM.getCode().getCode(); - InstructionList il = new InstructionList(bytes); - InstructionFinder finder = new InstructionFinder(il); - Iterator it = finder.search(".*", il.getStart(), null); - - InstructionHandle[] ihs = (InstructionHandle[])it.next(); - int size = 0; - for (InstructionHandle ih : ihs) - { - size += ih.getInstruction().getLength(); - } - assertEquals(bytes.length, size); - - } -} diff --git a/src/test/java/org/apache/commons/bcel6/NanoTimer.java b/src/test/java/org/apache/commons/bcel6/NanoTimer.java deleted file mode 100644 index 83f4edea..00000000 --- a/src/test/java/org/apache/commons/bcel6/NanoTimer.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * 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.commons.bcel6; - -public class NanoTimer { - - private long time = 0; - - public NanoTimer start() { - time -= System.nanoTime(); - return this; - } - - public void stop() { - time += System.nanoTime(); - } - - public void subtract(final NanoTimer o) { - time -= o.time; - } - - public void reset() { - time = 0; - } - - /** - * May ony be called after stop has been called as many times as start. - */ - @Override - public String toString() { - return ((double) time / 1000000000) + " s"; - } - - - -} diff --git a/src/test/java/org/apache/commons/bcel6/PLSETestCase.java b/src/test/java/org/apache/commons/bcel6/PLSETestCase.java deleted file mode 100644 index f4dd1a3c..00000000 --- a/src/test/java/org/apache/commons/bcel6/PLSETestCase.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * 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.commons.bcel6; - -import org.apache.commons.bcel6.classfile.JavaClass; -import org.apache.commons.bcel6.classfile.LocalVariableTable; -import org.apache.commons.bcel6.classfile.Method; -import org.apache.commons.bcel6.generic.ClassGen; -import org.apache.commons.bcel6.generic.ConstantPoolGen; -import org.apache.commons.bcel6.generic.MethodGen; -import org.apache.commons.bcel6.generic.Type; - -public class PLSETestCase extends AbstractTestCase -{ - /** - * BCEL-208: A couple of methods in MethodGen.java need to test for - * an empty instruction list. - */ - public void testB208() throws ClassNotFoundException - { - JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.PLSETestClass"); - ClassGen gen = new ClassGen(clazz); - ConstantPoolGen pool = gen.getConstantPool(); - Method m = gen.getMethodAt(1); - MethodGen mg = new MethodGen(m, gen.getClassName(), pool); - mg.setInstructionList(null); - mg.addLocalVariable("local2", Type.INT, null, null); - // currently, this will cause null pointer exception - mg.getLocalVariableTable(pool); - } - - /** - * BCEL-79: - */ - public void testB79() throws ClassNotFoundException - { - JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.PLSETestClass"); - ClassGen gen = new ClassGen(clazz); - ConstantPoolGen pool = gen.getConstantPool(); - Method m = gen.getMethodAt(2); - LocalVariableTable lvt = m.getLocalVariableTable(); - //System.out.println(lvt); - //System.out.println(lvt.getTableLength()); - MethodGen mg = new MethodGen(m, gen.getClassName(), pool); - LocalVariableTable new_lvt = mg.getLocalVariableTable(mg.getConstantPool()); - //System.out.println(new_lvt); - assertEquals("number of locals", lvt.getTableLength(), new_lvt.getTableLength()); - } -} diff --git a/src/test/java/org/apache/commons/bcel6/PerformanceTest.java b/src/test/java/org/apache/commons/bcel6/PerformanceTest.java deleted file mode 100644 index ea99266c..00000000 --- a/src/test/java/org/apache/commons/bcel6/PerformanceTest.java +++ /dev/null @@ -1,152 +0,0 @@ -/* - * 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.commons.bcel6; - -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.FileFilter; -import java.io.IOException; -import java.io.InputStream; -import java.util.Enumeration; -import java.util.jar.JarEntry; -import java.util.jar.JarFile; - -import org.apache.commons.bcel6.classfile.ClassParser; -import org.apache.commons.bcel6.classfile.JavaClass; -import org.apache.commons.bcel6.classfile.Method; -import org.apache.commons.bcel6.generic.ClassGen; -import org.apache.commons.bcel6.generic.InstructionList; -import org.apache.commons.bcel6.generic.MethodGen; -import org.junit.Assert; - -import junit.framework.TestCase; - -public final class PerformanceTest extends TestCase { - - private static final boolean REPORT = Boolean.parseBoolean(System.getProperty("PerformanceTest.report", "true"));; - - private static byte[] read(final InputStream is) throws IOException { - if (is == null) { - throw new IOException("Class not found"); - } - byte[] b = new byte[is.available()]; - int len = 0; - while (true) { - int n = is.read(b, len, b.length - len); - if (n == -1) { - if (len < b.length) { - byte[] c = new byte[len]; - System.arraycopy(b, 0, c, 0, len); - b = c; - } - return b; - } - len += n; - if (len == b.length) { - byte[] c = new byte[b.length + 1000]; - System.arraycopy(b, 0, c, 0, len); - b = c; - } - } - } - - private static void test(final File lib) throws IOException { - NanoTimer total = new NanoTimer(); - NanoTimer parseTime = new NanoTimer(); - NanoTimer cgenTime = new NanoTimer(); - NanoTimer mgenTime = new NanoTimer(); - NanoTimer mserTime = new NanoTimer(); - NanoTimer serTime = new NanoTimer(); - - System.out.println("parsing " + lib); - - total.start(); - JarFile jar = new JarFile(lib); - Enumeration en = jar.entries(); - - while (en.hasMoreElements()) { - JarEntry e = (JarEntry) en.nextElement(); - if (e.getName().endsWith(".class")) { - InputStream in = jar.getInputStream(e); - byte[] bytes = read(in); - - parseTime.start(); - JavaClass clazz = new ClassParser(new ByteArrayInputStream(bytes), e.getName()) - .parse(); - parseTime.stop(); - - cgenTime.start(); - ClassGen cg = new ClassGen(clazz); - cgenTime.stop(); - - Method[] methods = cg.getMethods(); - for (Method m : methods) { - mgenTime.start(); - MethodGen mg = new MethodGen(m, cg.getClassName(), cg.getConstantPool()); - InstructionList il = mg.getInstructionList(); - mgenTime.stop(); - - mserTime.start(); - if (il != null) { - mg.getInstructionList().setPositions(); - mg.setMaxLocals(); - mg.setMaxStack(); - } - cg.replaceMethod(m, mg.getMethod()); - mserTime.stop(); - } - - serTime.start(); - cg.getJavaClass().getBytes(); - serTime.stop(); - } - } - - jar.close(); - total.stop(); - if (REPORT) { - System.out.println("ClassParser.parse: " + parseTime); - System.out.println("ClassGen.init: " + cgenTime); - System.out.println("MethodGen.init: " + mgenTime); - System.out.println("MethodGen.getMethod: " + mserTime); - System.out.println("ClassGen.getJavaClass.getBytes: " + serTime); - System.out.println("Total: " + total); - System.out.println(); - } - } - - public void testPerformance() throws IOException { - File javaLib = new File(System.getProperty("java.home") + "/lib"); - javaLib.listFiles(new FileFilter() { - - @Override - public boolean accept(final File file) { - if(file.getName().endsWith(".jar")) { - try { - test(file); - } catch (IOException e) { - Assert.fail(e.getMessage()); - } - } - return false; - } - }); - } - -} diff --git a/src/test/java/org/apache/commons/bcel6/classfile/JDKClassDumpTestCase.java b/src/test/java/org/apache/commons/bcel6/classfile/JDKClassDumpTestCase.java deleted file mode 100644 index 277b4bd9..00000000 --- a/src/test/java/org/apache/commons/bcel6/classfile/JDKClassDumpTestCase.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * 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.commons.bcel6.classfile; - -import static org.junit.Assert.assertEquals; - -import java.io.ByteArrayOutputStream; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.File; -import java.io.FileFilter; -import java.io.InputStream; -import java.util.Enumeration; -import java.util.jar.JarEntry; -import java.util.jar.JarFile; - -import org.junit.Assert; -import org.junit.Test; - -/** - * Test that dump() methods work on the JDK classes - */ -public class JDKClassDumpTestCase { - - @Test - public void testPerformance() throws Exception { - File javaLib = new File(System.getProperty("java.home") + "/lib"); - javaLib.listFiles(new FileFilter() { - - @Override - public boolean accept(final File file) { - if(file.getName().endsWith(".jar")) { - try { - testJar(file); - } catch (Exception e) { - Assert.fail(e.getMessage()); - } - } - return false; - } - }); - } - - - private void testJar(final File file) throws Exception { - System.out.println("parsing " + file); - JarFile jar = new JarFile(file); - Enumeration en = jar.entries(); - - while (en.hasMoreElements()) { - JarEntry e = en.nextElement(); - final String name = e.getName(); - if (name.endsWith(".class")) { -// System.out.println("parsing " + name); - InputStream in = jar.getInputStream(e); - ClassParser parser = new ClassParser(in, name); - JavaClass jc = parser.parse(); - compare(jc, jar.getInputStream(e), name); - } - } - jar.close(); - } - - private void compare(final JavaClass jc, final InputStream inputStream, final String name) throws Exception { - final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - DataOutputStream dos = new DataOutputStream(baos); - jc.dump(dos); - dos.close(); - DataInputStream src = new DataInputStream(inputStream); - int i=0; - for(int out : baos.toByteArray()) { - int in = src.read(); - assertEquals(name + ": Mismatch at "+i, in, out&0xFF); - i++; - } - src.close(); - } - - -} diff --git a/src/test/java/org/apache/commons/bcel6/classfile/UtilityTestCase.java b/src/test/java/org/apache/commons/bcel6/classfile/UtilityTestCase.java deleted file mode 100644 index 6a0394ac..00000000 --- a/src/test/java/org/apache/commons/bcel6/classfile/UtilityTestCase.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * 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.commons.bcel6.classfile; - -import junit.framework.TestCase; - -public class UtilityTestCase extends TestCase { - - public void testSignatureToStringWithGenerics() throws Exception { - assertEquals("generic signature", - "java.util.Map>", - Utility.signatureToString("Ljava/util/Map;>;")); - assertEquals("generic signature", - "java.util.Set" - , Utility.signatureToString("Ljava/util/Set<+Ljava/nio/file/OpenOption;>;")); - assertEquals("generic signature", - "java.nio.file.attribute.FileAttribute...[]", - Utility.signatureToString("[Ljava/nio/file/attribute/FileAttribute<*>;")); - } -} diff --git a/src/test/java/org/apache/commons/bcel6/data/AnnotatedFields.java b/src/test/java/org/apache/commons/bcel6/data/AnnotatedFields.java deleted file mode 100644 index d8391a0d..00000000 --- a/src/test/java/org/apache/commons/bcel6/data/AnnotatedFields.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * 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.commons.bcel6.data; - -public class AnnotatedFields { - @SimpleAnnotation(id=1) int i; - - @SimpleAnnotation(id=2) String s; -} diff --git a/src/test/java/org/apache/commons/bcel6/data/AnnotatedWithCombinedAnnotation.java b/src/test/java/org/apache/commons/bcel6/data/AnnotatedWithCombinedAnnotation.java deleted file mode 100644 index e514ad0b..00000000 --- a/src/test/java/org/apache/commons/bcel6/data/AnnotatedWithCombinedAnnotation.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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.commons.bcel6.data; - -@CombinedAnnotation( { @SimpleAnnotation(id = 4) }) -public class AnnotatedWithCombinedAnnotation -{ - public AnnotatedWithCombinedAnnotation(final int param1, @SimpleAnnotation(id=42) final int param2) { - } - - @CombinedAnnotation( {}) - public void methodWithArrayOfZeroAnnotations() { - } - - @CombinedAnnotation( { @SimpleAnnotation(id=1, fruit="apples"), @SimpleAnnotation(id= 2, fruit="oranges")}) - public void methodWithArrayOfTwoAnnotations() { - } -} diff --git a/src/test/java/org/apache/commons/bcel6/data/AnnotatedWithEnumClass.java b/src/test/java/org/apache/commons/bcel6/data/AnnotatedWithEnumClass.java deleted file mode 100644 index 8aad494a..00000000 --- a/src/test/java/org/apache/commons/bcel6/data/AnnotatedWithEnumClass.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * 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.commons.bcel6.data; - -@AnnotationEnumElement(enumval = SimpleEnum.Red) -public class AnnotatedWithEnumClass -{ -} diff --git a/src/test/java/org/apache/commons/bcel6/data/AnnotationEnumElement.java b/src/test/java/org/apache/commons/bcel6/data/AnnotationEnumElement.java deleted file mode 100644 index 63b333d5..00000000 --- a/src/test/java/org/apache/commons/bcel6/data/AnnotationEnumElement.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * 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.commons.bcel6.data; - -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; - -@Retention(RetentionPolicy.RUNTIME) -public @interface AnnotationEnumElement -{ - SimpleEnum enumval(); -} diff --git a/src/test/java/org/apache/commons/bcel6/data/AnonymousClassTest.java b/src/test/java/org/apache/commons/bcel6/data/AnonymousClassTest.java deleted file mode 100644 index 070451f7..00000000 --- a/src/test/java/org/apache/commons/bcel6/data/AnonymousClassTest.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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.commons.bcel6.data; - -public class AnonymousClassTest -{ - public void foo() - { - new Runnable() - { - @Override - public void run() - { - } - }.run(); - } - - class X - { - } - - static class Y - { - } -} diff --git a/src/test/java/org/apache/commons/bcel6/data/AttributeTestClassEM01.java b/src/test/java/org/apache/commons/bcel6/data/AttributeTestClassEM01.java deleted file mode 100644 index c4ea20b2..00000000 --- a/src/test/java/org/apache/commons/bcel6/data/AttributeTestClassEM01.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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.commons.bcel6.data; - -public class AttributeTestClassEM01 -{ - public static void main(final String[] argv) - { - @SuppressWarnings("unused") - class S - { - public void sayhello() - { - System.err.println("hello"); - } - } - } -} diff --git a/src/test/java/org/apache/commons/bcel6/data/AttributeTestClassEM02.java b/src/test/java/org/apache/commons/bcel6/data/AttributeTestClassEM02.java deleted file mode 100644 index 16c85eaa..00000000 --- a/src/test/java/org/apache/commons/bcel6/data/AttributeTestClassEM02.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * 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.commons.bcel6.data; - -public class AttributeTestClassEM02 -{ - Runnable r = new Runnable() - { - @Override - public void run() - { - System.err.println("hello"); - } - }; - - public static void main(final String[] argv) - { - } -} diff --git a/src/test/java/org/apache/commons/bcel6/data/CombinedAnnotation.java b/src/test/java/org/apache/commons/bcel6/data/CombinedAnnotation.java deleted file mode 100644 index efcbeb71..00000000 --- a/src/test/java/org/apache/commons/bcel6/data/CombinedAnnotation.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * 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.commons.bcel6.data; - -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; - -@Retention(RetentionPolicy.RUNTIME) -public @interface CombinedAnnotation -{ - public SimpleAnnotation[] value(); -} diff --git a/src/test/java/org/apache/commons/bcel6/data/ComplexAnnotatedClass.java b/src/test/java/org/apache/commons/bcel6/data/ComplexAnnotatedClass.java deleted file mode 100644 index 51748c51..00000000 --- a/src/test/java/org/apache/commons/bcel6/data/ComplexAnnotatedClass.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * 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.commons.bcel6.data; - -@ComplexAnnotation(ival = 4, bval = 2, cval = '5', fval = 3.0f, dval = 33.4, zval = false, jval = 56, sval = 99) -public class ComplexAnnotatedClass -{ -} diff --git a/src/test/java/org/apache/commons/bcel6/data/ComplexAnnotation.java b/src/test/java/org/apache/commons/bcel6/data/ComplexAnnotation.java deleted file mode 100644 index 39f0be45..00000000 --- a/src/test/java/org/apache/commons/bcel6/data/ComplexAnnotation.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * 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.commons.bcel6.data; - -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; - -@Retention(RetentionPolicy.RUNTIME) -public @interface ComplexAnnotation -{ - int ival(); - - byte bval(); - - char cval(); - - long jval(); - - double dval(); - - boolean zval(); - - short sval(); - - float fval(); -} diff --git a/src/test/java/org/apache/commons/bcel6/data/MarkedType.java b/src/test/java/org/apache/commons/bcel6/data/MarkedType.java deleted file mode 100644 index 56f97df2..00000000 --- a/src/test/java/org/apache/commons/bcel6/data/MarkedType.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * 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.commons.bcel6.data; - -@MarkerAnnotationInvisible -@MarkerAnnotation -public class MarkedType -{ -} diff --git a/src/test/java/org/apache/commons/bcel6/data/MarkerAnnotation.java b/src/test/java/org/apache/commons/bcel6/data/MarkerAnnotation.java deleted file mode 100644 index 714a0a98..00000000 --- a/src/test/java/org/apache/commons/bcel6/data/MarkerAnnotation.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * 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.commons.bcel6.data; - -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; - -@Retention(RetentionPolicy.RUNTIME) -public @interface MarkerAnnotation -{ -} diff --git a/src/test/java/org/apache/commons/bcel6/data/MarkerAnnotationInvisible.java b/src/test/java/org/apache/commons/bcel6/data/MarkerAnnotationInvisible.java deleted file mode 100644 index 20563b6b..00000000 --- a/src/test/java/org/apache/commons/bcel6/data/MarkerAnnotationInvisible.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * 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.commons.bcel6.data; - -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; - -@Retention(RetentionPolicy.CLASS) -public @interface MarkerAnnotationInvisible { } diff --git a/src/test/java/org/apache/commons/bcel6/data/PLSETestClass.java b/src/test/java/org/apache/commons/bcel6/data/PLSETestClass.java deleted file mode 100644 index 6a1cf31d..00000000 --- a/src/test/java/org/apache/commons/bcel6/data/PLSETestClass.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * 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.commons.bcel6.data; - -import java.util.ArrayList; - -public class PLSETestClass -{ - public void meth1(final int arg1) - { - @SuppressWarnings("unused") - int local1 = arg1; - } - - public void meth2(final int arg1, final ArrayList arg2, final int arg3) - { - @SuppressWarnings("unused") - int local1 = arg1; - } -} diff --git a/src/test/java/org/apache/commons/bcel6/data/SimpleAnnotatedClass.java b/src/test/java/org/apache/commons/bcel6/data/SimpleAnnotatedClass.java deleted file mode 100644 index 37831da4..00000000 --- a/src/test/java/org/apache/commons/bcel6/data/SimpleAnnotatedClass.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * 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.commons.bcel6.data; - -@SimpleAnnotation(id = 4) -public class SimpleAnnotatedClass -{ -} diff --git a/src/test/java/org/apache/commons/bcel6/data/SimpleAnnotation.java b/src/test/java/org/apache/commons/bcel6/data/SimpleAnnotation.java deleted file mode 100644 index 4b4140fc..00000000 --- a/src/test/java/org/apache/commons/bcel6/data/SimpleAnnotation.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * 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.commons.bcel6.data; - -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; - -@Retention(RetentionPolicy.RUNTIME) -public @interface SimpleAnnotation -{ - int id(); - - String fruit() default "bananas"; -} diff --git a/src/test/java/org/apache/commons/bcel6/data/SimpleClass.java b/src/test/java/org/apache/commons/bcel6/data/SimpleClass.java deleted file mode 100644 index 9fa9ac01..00000000 --- a/src/test/java/org/apache/commons/bcel6/data/SimpleClass.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * 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.commons.bcel6.data; - -public class SimpleClass -{ - public static void main(final String[] argv) - { - // Nothing unusual in this class - } -} diff --git a/src/test/java/org/apache/commons/bcel6/data/SimpleEnum.java b/src/test/java/org/apache/commons/bcel6/data/SimpleEnum.java deleted file mode 100644 index 743de36a..00000000 --- a/src/test/java/org/apache/commons/bcel6/data/SimpleEnum.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * 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.commons.bcel6.data; - -public enum SimpleEnum { Red,Orange,Yellow,Green,Blue,Indigo,Violet } diff --git a/src/test/java/org/apache/commons/bcel6/generic/AnnotationGenTestCase.java b/src/test/java/org/apache/commons/bcel6/generic/AnnotationGenTestCase.java deleted file mode 100644 index 0bebb801..00000000 --- a/src/test/java/org/apache/commons/bcel6/generic/AnnotationGenTestCase.java +++ /dev/null @@ -1,166 +0,0 @@ -/* - * 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.commons.bcel6.generic; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -import org.apache.commons.bcel6.AbstractTestCase; -import org.apache.commons.bcel6.Const; -import org.apache.commons.bcel6.classfile.Annotations; -import org.apache.commons.bcel6.classfile.Attribute; -import org.apache.commons.bcel6.classfile.RuntimeInvisibleAnnotations; -import org.apache.commons.bcel6.classfile.RuntimeVisibleAnnotations; - -public class AnnotationGenTestCase extends AbstractTestCase -{ - private ClassGen createClassGen(final String classname) - { - return new ClassGen(classname, "java.lang.Object", "", - Const.ACC_PUBLIC | Const.ACC_SUPER, null); - } - - /** - * Programmatically construct an mutable annotation (AnnotationGen) object. - */ - public void testConstructMutableAnnotation() - { - // Create the containing class - ClassGen cg = createClassGen("HelloWorld"); - ConstantPoolGen cp = cg.getConstantPool(); - // Create the simple primitive value '4' of type 'int' - SimpleElementValueGen evg = new SimpleElementValueGen( - ElementValueGen.PRIMITIVE_INT, cp, 4); - // Give it a name, call it 'id' - ElementValuePairGen nvGen = new ElementValuePairGen("id", evg, - cp); - // Check it looks right - assertTrue( - "Should include string 'id=4' but says: " + nvGen.toString(), - nvGen.toString().contains("id=4")); - ObjectType t = new ObjectType("SimpleAnnotation"); - List elements = new ArrayList<>(); - elements.add(nvGen); - // Build an annotation of type 'SimpleAnnotation' with 'id=4' as the - // only value :) - AnnotationEntryGen a = new AnnotationEntryGen(t, elements, true, cp); - // Check we can save and load it ok - checkSerialize(a, cp); - } - - public void testVisibleInvisibleAnnotationGen() - { - // Create the containing class - ClassGen cg = createClassGen("HelloWorld"); - ConstantPoolGen cp = cg.getConstantPool(); - // Create the simple primitive value '4' of type 'int' - SimpleElementValueGen evg = new SimpleElementValueGen( - ElementValueGen.PRIMITIVE_INT, cp, 4); - // Give it a name, call it 'id' - ElementValuePairGen nvGen = new ElementValuePairGen("id", evg, - cp); - // Check it looks right - assertTrue( - "Should include string 'id=4' but says: " + nvGen.toString(), - nvGen.toString().contains("id=4")); - ObjectType t = new ObjectType("SimpleAnnotation"); - List elements = new ArrayList<>(); - elements.add(nvGen); - // Build a RV annotation of type 'SimpleAnnotation' with 'id=4' as the - // only value :) - AnnotationEntryGen a = new AnnotationEntryGen(t, elements, true, cp); - List v = new ArrayList<>(); - v.add(a); - Attribute[] attributes = AnnotationEntryGen.getAnnotationAttributes(cp, v.toArray(new AnnotationEntryGen[0])); - boolean foundRV = false; - for (Attribute attribute : attributes) { - if (attribute instanceof RuntimeVisibleAnnotations) - { - assertTrue(((Annotations) attribute).isRuntimeVisible()); - foundRV = true; - } - } - assertTrue("Should have seen a RuntimeVisibleAnnotation", foundRV); - // Build a RIV annotation of type 'SimpleAnnotation' with 'id=4' as the - // only value :) - AnnotationEntryGen a2 = new AnnotationEntryGen(t, elements, false, cp); - List v2 = new ArrayList<>(); - v2.add(a2); - Attribute[] attributes2 = AnnotationEntryGen.getAnnotationAttributes(cp, v2.toArray(new AnnotationEntryGen[0])); - boolean foundRIV = false; - for (Attribute attribute : attributes2) { - if (attribute instanceof RuntimeInvisibleAnnotations) - { - assertFalse(((Annotations) attribute).isRuntimeVisible()); - foundRIV = true; - } - } - assertTrue("Should have seen a RuntimeInvisibleAnnotation", foundRIV); - } - - private void checkSerialize(final AnnotationEntryGen a, final ConstantPoolGen cpg) - { - try - { - String beforeName = a.getTypeName(); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - DataOutputStream dos = new DataOutputStream(baos); - a.dump(dos); - dos.flush(); - dos.close(); - byte[] bs = baos.toByteArray(); - ByteArrayInputStream bais = new ByteArrayInputStream(bs); - DataInputStream dis = new DataInputStream(bais); - AnnotationEntryGen annAfter = AnnotationEntryGen.read(dis, cpg, a - .isRuntimeVisible()); - dis.close(); - String afterName = annAfter.getTypeName(); - if (!beforeName.equals(afterName)) - { - fail("Deserialization failed: before type='" + beforeName - + "' after type='" + afterName + "'"); - } - if (a.getValues().size() != annAfter.getValues().size()) - { - fail("Different numbers of element name value pairs?? " - + a.getValues().size() + "!=" - + annAfter.getValues().size()); - } - for (int i = 0; i < a.getValues().size(); i++) - { - ElementValuePairGen beforeElement = a.getValues().get(i); - ElementValuePairGen afterElement = annAfter.getValues().get(i); - if (!beforeElement.getNameString().equals( - afterElement.getNameString())) - { - fail("Different names?? " + beforeElement.getNameString() - + "!=" + afterElement.getNameString()); - } - } - } - catch (IOException ioe) - { - fail("Unexpected exception whilst checking serialization: " + ioe); - } - } -} diff --git a/src/test/java/org/apache/commons/bcel6/generic/BranchHandleTestCase.java b/src/test/java/org/apache/commons/bcel6/generic/BranchHandleTestCase.java deleted file mode 100644 index 7c4f8f72..00000000 --- a/src/test/java/org/apache/commons/bcel6/generic/BranchHandleTestCase.java +++ /dev/null @@ -1,37 +0,0 @@ -package org.apache.commons.bcel6.generic; - -import org.junit.Assert; -import org.junit.Test; - -public class BranchHandleTestCase { - - // Test that setInstruction only allows BranchInstructions - @Test(expected=ClassGenException.class) - public void testsetInstructionNull() { - BranchHandle bh = BranchHandle.getBranchHandle(new GOTO(null));// have to start with a valid BI - Assert.assertNotNull(bh); - bh.setInstruction(null); - Assert.assertNotNull(bh); - } - - @Test - public void testsetInstructionBI() { - BranchHandle bh = BranchHandle.getBranchHandle(new GOTO(null));// have to start with a valid BI - Assert.assertNotNull(bh); - bh.setInstruction(new GOTO(null)); - Assert.assertNotNull(bh); - } - - @Test(expected=ClassGenException.class) - public void testsetInstructionnotBI() { - BranchHandle bh = BranchHandle.getBranchHandle(new GOTO(null));// have to start with a valid BI - Assert.assertNotNull(bh); - bh.setInstruction(new NOP()); - Assert.assertNotNull(bh); - } - - @Test(expected=ClassGenException.class) - public void testGetBHnull() { - BranchHandle.getBranchHandle(null); - } -} diff --git a/src/test/java/org/apache/commons/bcel6/generic/FieldAnnotationsTestCase.java b/src/test/java/org/apache/commons/bcel6/generic/FieldAnnotationsTestCase.java deleted file mode 100644 index 306ecb2b..00000000 --- a/src/test/java/org/apache/commons/bcel6/generic/FieldAnnotationsTestCase.java +++ /dev/null @@ -1,167 +0,0 @@ -/* - * 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.commons.bcel6.generic; - -import java.io.File; -import java.io.IOException; - -import org.apache.commons.bcel6.AbstractTestCase; -import org.apache.commons.bcel6.classfile.AnnotationEntry; -import org.apache.commons.bcel6.classfile.ElementValuePair; -import org.apache.commons.bcel6.classfile.Field; -import org.apache.commons.bcel6.classfile.JavaClass; -import org.apache.commons.bcel6.util.SyntheticRepository; - -public class FieldAnnotationsTestCase extends AbstractTestCase -{ - /** - * Check field AnnotationEntrys are retrievable. - */ - public void testFieldAnnotationEntrys() throws ClassNotFoundException - { - JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.AnnotatedFields"); - // TODO L...;? - checkAnnotatedField(clazz, "i", "L"+PACKAGE_BASE_SIG+"/data/SimpleAnnotation;", "id", "1"); - checkAnnotatedField(clazz, "s", "L"+PACKAGE_BASE_SIG+"/data/SimpleAnnotation;", "id", "2"); - } - - /** - * Check field AnnotationEntrys (de)serialize ok. - */ - public void testFieldAnnotationEntrysReadWrite() throws ClassNotFoundException, - IOException - { - JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.AnnotatedFields"); - checkAnnotatedField(clazz, "i", "L"+PACKAGE_BASE_SIG+"/data/SimpleAnnotation;", "id", "1"); - checkAnnotatedField(clazz, "s", "L"+PACKAGE_BASE_SIG+"/data/SimpleAnnotation;", "id", "2"); - // Write it out - File tfile = createTestdataFile("AnnotatedFields.class"); - clazz.dump(tfile); - SyntheticRepository repos2 = createRepos("."); - repos2.loadClass("AnnotatedFields"); - checkAnnotatedField(clazz, "i", "L"+PACKAGE_BASE_SIG+"/data/SimpleAnnotation;", "id", "1"); - checkAnnotatedField(clazz, "s", "L"+PACKAGE_BASE_SIG+"/data/SimpleAnnotation;", "id", "2"); - assertTrue(tfile.delete()); - } - - /** - * Check we can load in a class, modify its field AnnotationEntrys, save it, - * reload it and everything is correct. - */ - public void testFieldAnnotationModification() - throws ClassNotFoundException - { - boolean dbg = false; - JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.AnnotatedFields"); - ClassGen clg = new ClassGen(clazz); - Field f = clg.getFields()[0]; - if (dbg) { - System.err.println("Field in freshly constructed class is: " + f); - } - if (dbg) { - System.err.println("AnnotationEntrys on field are: " - + dumpAnnotationEntries(f.getAnnotationEntries())); - } - AnnotationEntryGen fruitBasedAnnotationEntry = createFruitAnnotationEntry(clg - .getConstantPool(), "Tomato", false); - FieldGen fg = new FieldGen(f, clg.getConstantPool()); - if (dbg) { - System.err.println("Adding AnnotationEntry to the field"); - } - fg.addAnnotationEntry(fruitBasedAnnotationEntry); - if (dbg) { - System.err.println("FieldGen (mutable field) is " + fg); - } - if (dbg) { - System.err.println("with AnnotationEntrys: " - + dumpAnnotationEntries(fg.getAnnotationEntries())); - } - if (dbg) { - System.err - .println("Replacing original field with new field that has extra AnnotationEntry"); - } - clg.removeField(f); - clg.addField(fg.getField()); - f = clg.getFields()[1]; // there are two fields in the class, removing - // and readding has changed the order - // so this time index [1] is the 'int i' field - if (dbg) { - System.err.println("Field now looks like this: " + f); - } - if (dbg) { - System.err.println("With AnnotationEntrys: " - + dumpAnnotationEntries(f.getAnnotationEntries())); - } - assertTrue("Should be 2 AnnotationEntrys on this field, but there are " - + f.getAnnotationEntries().length, f.getAnnotationEntries().length == 2); - } - - // helper methods - public void checkAnnotatedField(final JavaClass clazz, final String fieldname, - final String AnnotationEntryName, final String AnnotationEntryElementName, - final String AnnotationEntryElementValue) - { - Field[] fields = clazz.getFields(); - for (Field f : fields) { - AnnotationEntry[] fieldAnnotationEntrys = f.getAnnotationEntries(); - if (f.getName().equals(fieldname)) - { - checkAnnotationEntry(fieldAnnotationEntrys[0], AnnotationEntryName, - AnnotationEntryElementName, AnnotationEntryElementValue); - } - } - } - - private void checkAnnotationEntry(final AnnotationEntry a, final String name, final String elementname, - final String elementvalue) - { - assertTrue("Expected AnnotationEntry to have name " + name - + " but it had name " + a.getAnnotationType(), a.getAnnotationType() - .equals(name)); - assertTrue("Expected AnnotationEntry to have one element but it had " - + a.getElementValuePairs().length, a.getElementValuePairs().length == 1); - ElementValuePair envp = a.getElementValuePairs()[0]; - assertTrue("Expected element name " + elementname + " but was " - + envp.getNameString(), elementname - .equals(envp.getNameString())); - assertTrue("Expected element value " + elementvalue + " but was " - + envp.getValue().stringifyValue(), elementvalue.equals(envp - .getValue().stringifyValue())); - } - - // helper methods - public void checkValue(final AnnotationEntry a, final String name, final String tostring) - { - for (int i = 0; i < a.getElementValuePairs().length; i++) - { - ElementValuePair element = a.getElementValuePairs()[i]; - if (element.getNameString().equals(name)) - { - if (!element.getValue().stringifyValue().equals(tostring)) - { - fail("Expected element " + name + " to have value " - + tostring + " but it had value " - + element.getValue().stringifyValue()); - } - return; - } - } - fail("Didnt find named element " + name); - } -} diff --git a/src/test/java/org/apache/commons/bcel6/generic/GeneratingAnnotatedClassesTestCase.java b/src/test/java/org/apache/commons/bcel6/generic/GeneratingAnnotatedClassesTestCase.java deleted file mode 100644 index b63700d2..00000000 --- a/src/test/java/org/apache/commons/bcel6/generic/GeneratingAnnotatedClassesTestCase.java +++ /dev/null @@ -1,680 +0,0 @@ -/* - * 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.commons.bcel6.generic; - -import java.io.File; -import java.util.ArrayList; -import java.util.List; - -import org.apache.commons.bcel6.AbstractTestCase; -import org.apache.commons.bcel6.Const; -import org.apache.commons.bcel6.classfile.AnnotationEntry; -import org.apache.commons.bcel6.classfile.ArrayElementValue; -import org.apache.commons.bcel6.classfile.ElementValue; -import org.apache.commons.bcel6.classfile.ElementValuePair; -import org.apache.commons.bcel6.classfile.JavaClass; -import org.apache.commons.bcel6.classfile.Method; -import org.apache.commons.bcel6.classfile.ParameterAnnotationEntry; -import org.apache.commons.bcel6.classfile.SimpleElementValue; -import org.apache.commons.bcel6.util.SyntheticRepository; - -/** - * The program that some of the tests generate looks like this: - * - *
- * public class HelloWorld
- * {
- *  public static void main(String[] argv)
- *  {
- *      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
- *      String name = null;
- *
- *      try
- *      {
- *          name = "Andy";
- *      }
- *      catch (IOException e)
- *      {
- *          return;
- *      }
- *      System.out.println("Hello, " + name);
- *  }
- * }
- * 
- */ -public class GeneratingAnnotatedClassesTestCase extends AbstractTestCase -{ - /** - * Steps in the test: - *
    - *
  1. Programmatically construct the HelloWorld program
  2. - *
  3. Add two simple annotations at the class level
  4. - *
  5. Save the class to disk
  6. - *
  7. Reload the class using the 'static' variant of the BCEL classes
  8. - *
  9. Check the attributes are OK
  10. - *
- */ - public void testGenerateClassLevelAnnotations() - throws ClassNotFoundException - { - // Create HelloWorld - ClassGen cg = createClassGen("HelloWorld"); - cg.setMajor(49); - cg.setMinor(0); - ConstantPoolGen cp = cg.getConstantPool(); - InstructionList il = new InstructionList(); - cg.addAnnotationEntry(createSimpleVisibleAnnotation(cp)); - cg.addAnnotationEntry(createSimpleInvisibleAnnotation(cp)); - buildClassContents(cg, cp, il); - //System.out.println(cg.getJavaClass().toString()); - dumpClass(cg, "HelloWorld.class"); - JavaClass jc = getClassFrom(".", "HelloWorld"); - AnnotationEntry[] as = jc.getAnnotationEntries(); - assertTrue("Should be two AnnotationEntries but found " + as.length, - as.length == 2); - // TODO L??; - assertTrue( - "Name of annotation 1 should be LSimpleAnnotation; but it is " - + as[0].getAnnotationType(), as[0].getAnnotationType() - .equals("LSimpleAnnotation;")); - assertTrue( - "Name of annotation 2 should be LSimpleAnnotation; but it is " - + as[1].getAnnotationType(), as[1].getAnnotationType() - .equals("LSimpleAnnotation;")); - ElementValuePair[] vals = as[0].getElementValuePairs(); - ElementValuePair nvp = vals[0]; - assertTrue( - "Name of element in SimpleAnnotation should be 'id' but it is " - + nvp.getNameString(), nvp.getNameString().equals("id")); - ElementValue ev = nvp.getValue(); - assertTrue("Type of element value should be int but it is " - + ev.getElementValueType(), - ev.getElementValueType() == ElementValue.PRIMITIVE_INT); - assertTrue("Value of element should be 4 but it is " - + ev.stringifyValue(), ev.stringifyValue().equals("4")); - assertTrue(createTestdataFile("HelloWorld.class").delete()); - } - - /** - * Just check that we can dump a class that has a method annotation on it - * and it is still there when we read it back in - */ - public void testGenerateMethodLevelAnnotations1() - throws ClassNotFoundException - { - // Create HelloWorld - ClassGen cg = createClassGen("HelloWorld"); - ConstantPoolGen cp = cg.getConstantPool(); - InstructionList il = new InstructionList(); - buildClassContentsWithAnnotatedMethods(cg, cp, il); - // Check annotation is OK - int i = cg.getMethods()[0].getAnnotationEntries().length; - assertTrue( - "Prior to dumping, main method should have 1 annotation but has " - + i, i == 1); - dumpClass(cg, "temp1" + File.separator + "HelloWorld.class"); - JavaClass jc2 = getClassFrom("temp1", "HelloWorld"); - // Check annotation is OK - i = jc2.getMethods()[0].getAnnotationEntries().length; - assertTrue("JavaClass should say 1 annotation on main method but says " - + i, i == 1); - ClassGen cg2 = new ClassGen(jc2); - // Check it now it is a ClassGen - Method[] m = cg2.getMethods(); - i = m[0].getAnnotationEntries().length; - assertTrue("The main 'Method' should have one annotation but has " + i, - i == 1); - MethodGen mg = new MethodGen(m[0], cg2.getClassName(), cg2 - .getConstantPool()); - // Check it finally when the Method is changed to a MethodGen - i = mg.getAnnotationEntries().length; - assertTrue("The main 'MethodGen' should have one annotation but has " - + i, i == 1); - - assertTrue(wipe("temp1", "HelloWorld.class")); - } - - /** - * Going further than the last test - when we reload the method back in, - * let's change it (adding a new annotation) and then store that, read it - * back in and verify both annotations are there ! - */ - public void testGenerateMethodLevelAnnotations2() - throws ClassNotFoundException - { - // Create HelloWorld - ClassGen cg = createClassGen("HelloWorld"); - ConstantPoolGen cp = cg.getConstantPool(); - InstructionList il = new InstructionList(); - buildClassContentsWithAnnotatedMethods(cg, cp, il); - dumpClass(cg, "temp2", "HelloWorld.class"); - JavaClass jc2 = getClassFrom("temp2", "HelloWorld"); - ClassGen cg2 = new ClassGen(jc2); - // Main method after reading the class back in - Method mainMethod1 = jc2.getMethods()[0]; - assertTrue("The 'Method' should have one annotations but has " - + mainMethod1.getAnnotationEntries().length, mainMethod1 - .getAnnotationEntries().length == 1); - MethodGen mainMethod2 = new MethodGen(mainMethod1, cg2.getClassName(), - cg2.getConstantPool()); - assertTrue("The 'MethodGen' should have one annotations but has " - + mainMethod2.getAnnotationEntries().length, mainMethod2 - .getAnnotationEntries().length == 1); - mainMethod2.addAnnotationEntry(createFruitAnnotation(cg2 - .getConstantPool(), "Pear")); - cg2.removeMethod(mainMethod1); - cg2.addMethod(mainMethod2.getMethod()); - dumpClass(cg2, "temp3", "HelloWorld.class"); - JavaClass jc3 = getClassFrom("temp3", "HelloWorld"); - ClassGen cg3 = new ClassGen(jc3); - Method mainMethod3 = cg3.getMethods()[1]; - int i = mainMethod3.getAnnotationEntries().length; - assertTrue("The 'Method' should now have two annotations but has " + i, - i == 2); - assertTrue(wipe("temp2", "HelloWorld.class")); - assertTrue(wipe("temp3", "HelloWorld.class")); - } - - // J5TODO: Need to add deleteFile calls to many of these tests - /** - * Transform simple class from an immutable to a mutable object. - */ - public void testTransformClassToClassGen_SimpleTypes() - throws ClassNotFoundException - { - JavaClass jc = getTestClass(PACKAGE_BASE_NAME+".data.SimpleAnnotatedClass"); - ClassGen cgen = new ClassGen(jc); - // Check annotations are correctly preserved - AnnotationEntryGen[] annotations = cgen.getAnnotationEntries(); - assertTrue("Expected one annotation but found " + annotations.length, - annotations.length == 1); - } - - /** - * Transform simple class from an immutable to a mutable object. The class - * is annotated with an annotation that uses an enum. - */ - public void testTransformClassToClassGen_EnumType() - throws ClassNotFoundException - { - JavaClass jc = getTestClass(PACKAGE_BASE_NAME+".data.AnnotatedWithEnumClass"); - ClassGen cgen = new ClassGen(jc); - // Check annotations are correctly preserved - AnnotationEntryGen[] annotations = cgen.getAnnotationEntries(); - assertTrue("Expected one annotation but found " + annotations.length, - annotations.length == 1); - } - - /** - * Transform simple class from an immutable to a mutable object. The class - * is annotated with an annotation that uses an array of SimpleAnnotations. - */ - public void testTransformClassToClassGen_ArrayAndAnnotationTypes() - throws ClassNotFoundException - { - JavaClass jc = getTestClass(PACKAGE_BASE_NAME+".data.AnnotatedWithCombinedAnnotation"); - ClassGen cgen = new ClassGen(jc); - // Check annotations are correctly preserved - AnnotationEntryGen[] annotations = cgen.getAnnotationEntries(); - assertTrue("Expected one annotation but found " + annotations.length, - annotations.length == 1); - AnnotationEntryGen a = annotations[0]; - assertTrue("That annotation should only have one value but has " - + a.getValues().size(), a.getValues().size() == 1); - ElementValuePairGen nvp = a.getValues().get(0); - ElementValueGen value = nvp.getValue(); - assertTrue("Value should be ArrayElementValueGen but is " + value, - value instanceof ArrayElementValueGen); - ArrayElementValueGen arrayValue = (ArrayElementValueGen) value; - assertTrue("Array value should be size one but is " - + arrayValue.getElementValuesSize(), arrayValue - .getElementValuesSize() == 1); - ElementValueGen innerValue = arrayValue.getElementValues().get(0); - assertTrue( - "Value in the array should be AnnotationElementValueGen but is " - + innerValue, - innerValue instanceof AnnotationElementValueGen); - AnnotationElementValueGen innerAnnotationValue = (AnnotationElementValueGen) innerValue; - assertTrue("Should be called L"+PACKAGE_BASE_SIG+"/data/SimpleAnnotation; but is called: " - + innerAnnotationValue.getAnnotation().getTypeName(), - innerAnnotationValue.getAnnotation().getTypeSignature().equals( - "L"+PACKAGE_BASE_SIG+"/data/SimpleAnnotation;")); - - // check the three methods - Method[] methods = cgen.getMethods(); - assertEquals(3, methods.length); - for(Method method : methods) - { - String methodName= method.getName(); - if(methodName.equals("")) - { - assertMethodAnnotations(method, 0, 1); - assertParameterAnnotations(method, 0, 1); - } - else if(methodName.equals("methodWithArrayOfZeroAnnotations")) - { - assertMethodAnnotations(method, 1, 0); - } - else if(methodName.equals("methodWithArrayOfTwoAnnotations")) - { - assertMethodAnnotations(method, 1, 2); - } - else - { - fail("unexpected method "+method.getName()); - } - } - } - - private void assertMethodAnnotations(final Method method, final int expectedNumberAnnotations, final int nExpectedArrayValues) - { - String methodName= method.getName(); - AnnotationEntry[] annos= method.getAnnotationEntries(); - assertEquals("For "+methodName, expectedNumberAnnotations, annos.length); - if(expectedNumberAnnotations!=0) - { - assertArrayElementValue(nExpectedArrayValues, annos[0]); - } - } - - private void assertArrayElementValue(final int nExpectedArrayValues, final AnnotationEntry anno) - { - ElementValuePair elementValuePair = anno.getElementValuePairs()[0]; - assertEquals("value", elementValuePair.getNameString()); - ArrayElementValue ev = (ArrayElementValue) elementValuePair.getValue(); - ElementValue[] eva = ev.getElementValuesArray(); - assertEquals(nExpectedArrayValues, eva.length); - } - - private void assertParameterAnnotations(final Method method, final int... expectedNumberOfParmeterAnnotations) - { - String methodName= "For "+method.getName(); - ParameterAnnotationEntry[] parameterAnnotations= method.getParameterAnnotationEntries(); - assertEquals(methodName, expectedNumberOfParmeterAnnotations.length, parameterAnnotations.length); - - int i= 0; - for(ParameterAnnotationEntry parameterAnnotation : parameterAnnotations) - { - AnnotationEntry[] annos= parameterAnnotation.getAnnotationEntries(); - int expectedLength = expectedNumberOfParmeterAnnotations[i++]; - assertEquals(methodName+" parameter "+i, expectedLength, annos.length); - if(expectedLength!=0) - { - assertSimpleElementValue(annos[0]); - } - } - } - - private void assertSimpleElementValue(final AnnotationEntry anno) - { - ElementValuePair elementValuePair = anno.getElementValuePairs()[0]; - assertEquals("id", elementValuePair.getNameString()); - SimpleElementValue ev = (SimpleElementValue)elementValuePair.getValue(); - assertEquals(42, ev.getValueInt()); - } - - /** - * Transform complex class from an immutable to a mutable object. - */ - public void testTransformComplexClassToClassGen() - throws ClassNotFoundException - { - JavaClass jc = getTestClass(PACKAGE_BASE_NAME+".data.ComplexAnnotatedClass"); - ClassGen cgen = new ClassGen(jc); - // Check annotations are correctly preserved - AnnotationEntryGen[] annotations = cgen.getAnnotationEntries(); - assertTrue("Expected one annotation but found " + annotations.length, - annotations.length == 1); - List l = annotations[0].getValues(); - boolean found = false; - for (Object name : l) { - ElementValuePairGen element = (ElementValuePairGen) name; - if (element.getNameString().equals("dval")) - { - if (((SimpleElementValueGen) element.getValue()) - .stringifyValue().equals("33.4")) { - found = true; - } - } - } - assertTrue("Did not find double annotation value with value 33.4", - found); - } - - /** - * Load a class in and modify it with a new attribute - A SimpleAnnotation - * annotation - */ - public void testModifyingClasses1() throws ClassNotFoundException - { - JavaClass jc = getTestClass(PACKAGE_BASE_NAME+".data.SimpleAnnotatedClass"); - ClassGen cgen = new ClassGen(jc); - ConstantPoolGen cp = cgen.getConstantPool(); - cgen.addAnnotationEntry(createFruitAnnotation(cp, "Pineapple")); - assertTrue("Should now have two annotations but has " - + cgen.getAnnotationEntries().length, cgen - .getAnnotationEntries().length == 2); - dumpClass(cgen, "SimpleAnnotatedClass.class"); - assertTrue(wipe("SimpleAnnotatedClass.class")); - } - - /** - * Load a class in and modify it with a new attribute - A ComplexAnnotation - * annotation - */ - public void testModifyingClasses2() throws ClassNotFoundException - { - JavaClass jc = getTestClass(PACKAGE_BASE_NAME+".data.SimpleAnnotatedClass"); - ClassGen cgen = new ClassGen(jc); - ConstantPoolGen cp = cgen.getConstantPool(); - cgen.addAnnotationEntry(createCombinedAnnotation(cp)); - assertTrue("Should now have two annotations but has " - + cgen.getAnnotationEntries().length, cgen - .getAnnotationEntries().length == 2); - dumpClass(cgen, "SimpleAnnotatedClass.class"); - JavaClass jc2 = getClassFrom(".", "SimpleAnnotatedClass"); - jc2.getAnnotationEntries(); - assertTrue(wipe("SimpleAnnotatedClass.class")); - // System.err.println(jc2.toString()); - } - - private void dumpClass(final ClassGen cg, final String fname) - { - try - { - File f = createTestdataFile(fname); - cg.getJavaClass().dump(f); - } - catch (java.io.IOException e) - { - System.err.println(e); - } - } - - private void dumpClass(final ClassGen cg, final String dir, final String fname) - { - dumpClass(cg, dir + File.separator + fname); - } - - private void buildClassContentsWithAnnotatedMethods(final ClassGen cg, - final ConstantPoolGen cp, final InstructionList il) - { - // Create method 'public static void main(String[]argv)' - MethodGen mg = createMethodGen("main", il, cp); - InstructionFactory factory = new InstructionFactory(cg); - mg.addAnnotationEntry(createSimpleVisibleAnnotation(mg - .getConstantPool())); - // We now define some often used types: - ObjectType i_stream = new ObjectType("java.io.InputStream"); - ObjectType p_stream = new ObjectType("java.io.PrintStream"); - // Create variables in and name : We call the constructors, i.e., - // execute BufferedReader(InputStreamReader(System.in)) . The reference - // to the BufferedReader object stays on top of the stack and is stored - // in the newly allocated in variable. - il.append(factory.createNew("java.io.BufferedReader")); - il.append(InstructionConst.DUP); // Use predefined constant - il.append(factory.createNew("java.io.InputStreamReader")); - il.append(InstructionConst.DUP); - il.append(factory.createFieldAccess("java.lang.System", "in", i_stream, - Const.GETSTATIC)); - il.append(factory.createInvoke("java.io.InputStreamReader", "", - Type.VOID, new Type[] { i_stream }, Const.INVOKESPECIAL)); - il.append(factory.createInvoke("java.io.BufferedReader", "", - Type.VOID, new Type[] { new ObjectType("java.io.Reader") }, - Const.INVOKESPECIAL)); - LocalVariableGen lg = mg.addLocalVariable("in", new ObjectType( - "java.io.BufferedReader"), null, null); - int in = lg.getIndex(); - lg.setStart(il.append(new ASTORE(in))); // "in" valid from here - // Create local variable name and initialize it to null - lg = mg.addLocalVariable("name", Type.STRING, null, null); - int name = lg.getIndex(); - il.append(InstructionConst.ACONST_NULL); - lg.setStart(il.append(new ASTORE(name))); // "name" valid from here - // Create try-catch block: We remember the start of the block, read a - // line from the standard input and store it into the variable name . - // InstructionHandle try_start = il.append(factory.createFieldAccess( - // "java.lang.System", "out", p_stream, Constants.GETSTATIC)); - // il.append(new PUSH(cp, "Please enter your name> ")); - // il.append(factory.createInvoke("java.io.PrintStream", "print", - // Type.VOID, new Type[] { Type.STRING }, - // Constants.INVOKEVIRTUAL)); - // il.append(new ALOAD(in)); - // il.append(factory.createInvoke("java.io.BufferedReader", "readLine", - // Type.STRING, Type.NO_ARGS, Constants.INVOKEVIRTUAL)); - InstructionHandle try_start = il.append(new PUSH(cp, "Andy")); - il.append(new ASTORE(name)); - // Upon normal execution we jump behind exception handler, the target - // address is not known yet. - GOTO g = new GOTO(null); - InstructionHandle try_end = il.append(g); - // We add the exception handler which simply returns from the method. - LocalVariableGen var_ex = mg.addLocalVariable("ex", Type - .getType("Ljava.io.IOException;"), null, null); - int var_ex_slot = var_ex.getIndex(); - InstructionHandle handler = il.append(new ASTORE(var_ex_slot)); - var_ex.setStart(handler); - var_ex.setEnd(il.append(InstructionConst.RETURN)); - mg.addExceptionHandler(try_start, try_end, handler, new ObjectType( - "java.io.IOException")); - // "Normal" code continues, now we can set the branch target of the GOTO - // . - InstructionHandle ih = il.append(factory.createFieldAccess( - "java.lang.System", "out", p_stream, Const.GETSTATIC)); - g.setTarget(ih); - // Printing "Hello": String concatenation compiles to StringBuffer - // operations. - il.append(factory.createNew(Type.STRINGBUFFER)); - il.append(InstructionConst.DUP); - il.append(new PUSH(cp, "Hello, ")); - il - .append(factory.createInvoke("java.lang.StringBuffer", - "", Type.VOID, new Type[] { Type.STRING }, - Const.INVOKESPECIAL)); - il.append(new ALOAD(name)); - il.append(factory.createInvoke("java.lang.StringBuffer", "append", - Type.STRINGBUFFER, new Type[] { Type.STRING }, - Const.INVOKEVIRTUAL)); - il.append(factory.createInvoke("java.lang.StringBuffer", "toString", - Type.STRING, Type.NO_ARGS, Const.INVOKEVIRTUAL)); - il - .append(factory.createInvoke("java.io.PrintStream", "println", - Type.VOID, new Type[] { Type.STRING }, - Const.INVOKEVIRTUAL)); - il.append(InstructionConst.RETURN); - // Finalization: Finally, we have to set the stack size, which normally - // would have to be computed on the fly and add a default constructor - // method to the class, which is empty in this case. - mg.setMaxStack(); - mg.setMaxLocals(); - cg.addMethod(mg.getMethod()); - il.dispose(); // Allow instruction handles to be reused - cg.addEmptyConstructor(Const.ACC_PUBLIC); - } - - private void buildClassContents(final ClassGen cg, final ConstantPoolGen cp, - final InstructionList il) - { - // Create method 'public static void main(String[]argv)' - MethodGen mg = createMethodGen("main", il, cp); - InstructionFactory factory = new InstructionFactory(cg); - // We now define some often used types: - ObjectType i_stream = new ObjectType("java.io.InputStream"); - ObjectType p_stream = new ObjectType("java.io.PrintStream"); - // Create variables in and name : We call the constructors, i.e., - // execute BufferedReader(InputStreamReader(System.in)) . The reference - // to the BufferedReader object stays on top of the stack and is stored - // in the newly allocated in variable. - il.append(factory.createNew("java.io.BufferedReader")); - il.append(InstructionConst.DUP); // Use predefined constant - il.append(factory.createNew("java.io.InputStreamReader")); - il.append(InstructionConst.DUP); - il.append(factory.createFieldAccess("java.lang.System", "in", i_stream, - Const.GETSTATIC)); - il.append(factory.createInvoke("java.io.InputStreamReader", "", - Type.VOID, new Type[] { i_stream }, Const.INVOKESPECIAL)); - il.append(factory.createInvoke("java.io.BufferedReader", "", - Type.VOID, new Type[] { new ObjectType("java.io.Reader") }, - Const.INVOKESPECIAL)); - LocalVariableGen lg = mg.addLocalVariable("in", new ObjectType( - "java.io.BufferedReader"), null, null); - int in = lg.getIndex(); - lg.setStart(il.append(new ASTORE(in))); // "in" valid from here - // Create local variable name and initialize it to null - lg = mg.addLocalVariable("name", Type.STRING, null, null); - int name = lg.getIndex(); - il.append(InstructionConst.ACONST_NULL); - lg.setStart(il.append(new ASTORE(name))); // "name" valid from here - // Create try-catch block: We remember the start of the block, read a - // line from the standard input and store it into the variable name . - // InstructionHandle try_start = il.append(factory.createFieldAccess( - // "java.lang.System", "out", p_stream, Constants.GETSTATIC)); - // il.append(new PUSH(cp, "Please enter your name> ")); - // il.append(factory.createInvoke("java.io.PrintStream", "print", - // Type.VOID, new Type[] { Type.STRING }, - // Constants.INVOKEVIRTUAL)); - // il.append(new ALOAD(in)); - // il.append(factory.createInvoke("java.io.BufferedReader", "readLine", - // Type.STRING, Type.NO_ARGS, Constants.INVOKEVIRTUAL)); - InstructionHandle try_start = il.append(new PUSH(cp, "Andy")); - il.append(new ASTORE(name)); - // Upon normal execution we jump behind exception handler, the target - // address is not known yet. - GOTO g = new GOTO(null); - InstructionHandle try_end = il.append(g); - // We add the exception handler which simply returns from the method. - LocalVariableGen var_ex = mg.addLocalVariable("ex", Type - .getType("Ljava.io.IOException;"), null, null); - int var_ex_slot = var_ex.getIndex(); - InstructionHandle handler = il.append(new ASTORE(var_ex_slot)); - var_ex.setStart(handler); - var_ex.setEnd(il.append(InstructionConst.RETURN)); - mg.addExceptionHandler(try_start, try_end, handler, new ObjectType( - "java.io.IOException")); - // "Normal" code continues, now we can set the branch target of the GOTO - // . - InstructionHandle ih = il.append(factory.createFieldAccess( - "java.lang.System", "out", p_stream, Const.GETSTATIC)); - g.setTarget(ih); - // Printing "Hello": String concatenation compiles to StringBuffer - // operations. - il.append(factory.createNew(Type.STRINGBUFFER)); - il.append(InstructionConst.DUP); - il.append(new PUSH(cp, "Hello, ")); - il - .append(factory.createInvoke("java.lang.StringBuffer", - "", Type.VOID, new Type[] { Type.STRING }, - Const.INVOKESPECIAL)); - il.append(new ALOAD(name)); - il.append(factory.createInvoke("java.lang.StringBuffer", "append", - Type.STRINGBUFFER, new Type[] { Type.STRING }, - Const.INVOKEVIRTUAL)); - il.append(factory.createInvoke("java.lang.StringBuffer", "toString", - Type.STRING, Type.NO_ARGS, Const.INVOKEVIRTUAL)); - il - .append(factory.createInvoke("java.io.PrintStream", "println", - Type.VOID, new Type[] { Type.STRING }, - Const.INVOKEVIRTUAL)); - il.append(InstructionConst.RETURN); - // Finalization: Finally, we have to set the stack size, which normally - // would have to be computed on the fly and add a default constructor - // method to the class, which is empty in this case. - mg.setMaxStack(); - mg.setMaxLocals(); - cg.addMethod(mg.getMethod()); - il.dispose(); // Allow instruction handles to be reused - cg.addEmptyConstructor(Const.ACC_PUBLIC); - } - - private JavaClass getClassFrom(final String where, final String clazzname) - throws ClassNotFoundException - { - // System.out.println(where); - SyntheticRepository repos = createRepos(where); - return repos.loadClass(clazzname); - } - - // helper methods - private ClassGen createClassGen(final String classname) - { - return new ClassGen(classname, "java.lang.Object", "", - Const.ACC_PUBLIC | Const.ACC_SUPER, null); - } - - private MethodGen createMethodGen(final String methodname, final InstructionList il, - final ConstantPoolGen cp) - { - return new MethodGen(Const.ACC_STATIC | Const.ACC_PUBLIC, // access - // flags - Type.VOID, // return type - new Type[] { new ArrayType(Type.STRING, 1) }, // argument - // types - new String[] { "argv" }, // arg names - methodname, "HelloWorld", // method, class - il, cp); - } - - public AnnotationEntryGen createSimpleVisibleAnnotation(final ConstantPoolGen cp) - { - SimpleElementValueGen evg = new SimpleElementValueGen( - ElementValueGen.PRIMITIVE_INT, cp, 4); - ElementValuePairGen nvGen = new ElementValuePairGen("id", evg, cp); - ObjectType t = new ObjectType("SimpleAnnotation"); - List elements = new ArrayList<>(); - elements.add(nvGen); - AnnotationEntryGen a = new AnnotationEntryGen(t, elements, true, cp); - return a; - } - - public AnnotationEntryGen createFruitAnnotation(final ConstantPoolGen cp, - final String aFruit) - { - SimpleElementValueGen evg = new SimpleElementValueGen( - ElementValueGen.STRING, cp, aFruit); - ElementValuePairGen nvGen = new ElementValuePairGen("fruit", evg, cp); - ObjectType t = new ObjectType("SimpleStringAnnotation"); - List elements = new ArrayList<>(); - elements.add(nvGen); - return new AnnotationEntryGen(t, elements, true, cp); - } - - public AnnotationEntryGen createCombinedAnnotation(final ConstantPoolGen cp) - { - // Create an annotation instance - AnnotationEntryGen a = createSimpleVisibleAnnotation(cp); - ArrayElementValueGen array = new ArrayElementValueGen(cp); - array.addElement(new AnnotationElementValueGen(a, cp)); - ElementValuePairGen nvp = new ElementValuePairGen("value", array, cp); - List elements = new ArrayList<>(); - elements.add(nvp); - return new AnnotationEntryGen(new ObjectType("CombinedAnnotation"), - elements, true, cp); - } - - public AnnotationEntryGen createSimpleInvisibleAnnotation(final ConstantPoolGen cp) - { - SimpleElementValueGen evg = new SimpleElementValueGen( - ElementValueGen.PRIMITIVE_INT, cp, 4); - ElementValuePairGen nvGen = new ElementValuePairGen("id", evg, cp); - ObjectType t = new ObjectType("SimpleAnnotation"); - List elements = new ArrayList<>(); - elements.add(nvGen); - AnnotationEntryGen a = new AnnotationEntryGen(t, elements, false, cp); - return a; - } -} diff --git a/src/test/java/org/apache/commons/bcel6/generic/InstructionHandleTestCase.java b/src/test/java/org/apache/commons/bcel6/generic/InstructionHandleTestCase.java deleted file mode 100644 index ab9d786c..00000000 --- a/src/test/java/org/apache/commons/bcel6/generic/InstructionHandleTestCase.java +++ /dev/null @@ -1,46 +0,0 @@ -package org.apache.commons.bcel6.generic; - -import org.junit.Assert; -import org.junit.Test; - -public class InstructionHandleTestCase { - - // Test that setInstruction only allows Instructions that are not BranchInstructions - - @Test(expected=ClassGenException.class) - public void testsetInstructionNull() { - InstructionHandle ih = InstructionHandle.getInstructionHandle(new NOP());// have to start with a valid non BI - Assert.assertNotNull(ih); - ih.setInstruction(null); - Assert.assertNotNull(ih); - } - - @Test - public void testsetInstructionI() { - InstructionHandle ih = InstructionHandle.getInstructionHandle(new NOP());// have to start with a valid non BI - Assert.assertNotNull(ih); - ih.setInstruction(new NOP()); - Assert.assertNotNull(ih); - } - - @Test(expected=ClassGenException.class) - public void testsetInstructionnotI() { - InstructionHandle ih = InstructionHandle.getInstructionHandle(new NOP());// have to start with a valid non BI - Assert.assertNotNull(ih); - ih.setInstruction(new GOTO(null)); - Assert.assertNotNull(ih); - } - - @Test(expected=ClassGenException.class) - public void testGetIHnull() { - InstructionHandle.getInstructionHandle(null); - } - - @Test - public void testBCEL195() { - InstructionList il = new InstructionList(); - InstructionHandle ih = il.append(InstructionConst.NOP); - new TABLESWITCH(new int[0], new InstructionHandle[0], ih); - new TABLESWITCH(new int[0], new InstructionHandle[0], ih); - } -} diff --git a/src/test/java/org/apache/commons/bcel6/generic/JDKGenericDumpTestCase.java b/src/test/java/org/apache/commons/bcel6/generic/JDKGenericDumpTestCase.java deleted file mode 100644 index 34e1caf2..00000000 --- a/src/test/java/org/apache/commons/bcel6/generic/JDKGenericDumpTestCase.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * 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.commons.bcel6.generic; - -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.fail; - -import java.io.File; -import java.io.FileFilter; -import java.io.InputStream; -import java.util.Enumeration; -import java.util.jar.JarEntry; -import java.util.jar.JarFile; - -import org.apache.commons.bcel6.classfile.ClassParser; -import org.apache.commons.bcel6.classfile.Code; -import org.apache.commons.bcel6.classfile.JavaClass; -import org.apache.commons.bcel6.classfile.Method; -import org.junit.Test; - -/** - * Test that the generic dump() methods work on the JDK classes - * Reads each class into an instruction list and then dumps - * the instructions. The output bytes should be the same as the input. - */ -public class JDKGenericDumpTestCase { - - @Test - public void testJDKjars() throws Exception { - File[] jars = listJDKjars(); - for(File file : jars) { - testJar(file); - } - } - - private void testJar(final File file) throws Exception { - System.out.println(file); - JarFile jar = new JarFile(file); - Enumeration en = jar.entries(); - - while (en.hasMoreElements()) { - JarEntry e = en.nextElement(); - final String name = e.getName(); - if (name.endsWith(".class")) { -// System.out.println("- " + name); - InputStream in = jar.getInputStream(e); - ClassParser parser = new ClassParser(in, name); - JavaClass jc = parser.parse(); - for(Method m : jc.getMethods()) { - compare(name, m); - } - } - } - jar.close(); - } - - private void compare(final String name, final Method m) { -// System.out.println("Method: " + m); - Code c = m.getCode(); - if (c==null) { - return; // e.g. abstract method - } - byte[] src = c.getCode(); - InstructionList il = new InstructionList(src); - byte[] out = il.getByteCode(); - if (src.length == out.length) { - assertArrayEquals(name + ": "+m.toString(), src, out); - } else { - System.out.println(name + ": "+m.toString() +" "+ src.length+" "+out.length); - System.out.println(bytesToHex(src)); - System.out.println(bytesToHex(out)); - for(InstructionHandle ih : il) { - System.out.println(ih.toString(false)); - } - fail("Array comparison failure"); - } - } - - private File[] listJDKjars() throws Exception { - File javaLib = new File(System.getProperty("java.home") + "/lib"); - return javaLib.listFiles(new FileFilter() { - @Override - public boolean accept(final File file) { - return file.getName().endsWith(".jar"); - } - }); - } - - private static final char[] hexArray = "0123456789ABCDEF".toCharArray(); - private static String bytesToHex(final byte[] bytes) { - char[] hexChars = new char[bytes.length * 3]; - int i=0; - for (byte b : bytes) { - int v = b & 0xFF; - hexChars[i++] = hexArray[v >>> 4]; - hexChars[i++] = hexArray[v & 0x0F]; - hexChars[i++] = ' '; - } - return new String(hexChars); - } -} diff --git a/src/test/java/org/apache/commons/bcel6/generic/MethodGenTestCase.java b/src/test/java/org/apache/commons/bcel6/generic/MethodGenTestCase.java deleted file mode 100644 index d432d127..00000000 --- a/src/test/java/org/apache/commons/bcel6/generic/MethodGenTestCase.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * 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.commons.bcel6.generic; - -import java.util.Arrays; - -import org.apache.commons.bcel6.Repository; -import org.apache.commons.bcel6.classfile.JavaClass; -import org.apache.commons.bcel6.classfile.Method; - -import junit.framework.TestCase; - -public class MethodGenTestCase extends TestCase { - - public static class Foo { - public void bar() { - @SuppressWarnings("unused") - int a = 1; - } - } - - private MethodGen getMethod(final Class cls, final String name) throws ClassNotFoundException { - JavaClass jc = Repository.lookupClass(cls); - ConstantPoolGen cp = new ConstantPoolGen(jc.getConstantPool()); - for (Method method : jc.getMethods()) { - if (method.getName().equals(name)) { - return new MethodGen(method, jc.getClassName(), cp); - } - } - - fail("Method " + name + " not found in class " + cls); - return null; - } - - public void testRemoveLocalVariable() throws Exception { - MethodGen mg = getMethod(Foo.class, "bar"); - - LocalVariableGen lv = mg.getLocalVariables()[1]; - assertEquals("variable name", "a", lv.getName()); - InstructionHandle start = lv.getStart(); - InstructionHandle end = lv.getEnd(); - assertNotNull("scope start", start); - assertNotNull("scope end", end); - assertTrue("scope start not targeted by the local variable", Arrays.asList(start.getTargeters()).contains(lv)); - assertTrue("scope end not targeted by the local variable", Arrays.asList(end.getTargeters()).contains(lv)); - - // now let's remove the local variable - mg.removeLocalVariable(lv); - - assertFalse("scope start still targeted by the removed variable", Arrays.asList(start.getTargeters()).contains(lv)); - assertFalse("scope end still targeted by the removed variable", Arrays.asList(end.getTargeters()).contains(lv)); - assertNull("scope start", lv.getStart()); - assertNull("scope end", lv.getEnd()); - } - - public void testRemoveLocalVariables() throws Exception { - MethodGen mg = getMethod(Foo.class, "bar"); - - LocalVariableGen lv = mg.getLocalVariables()[1]; - assertEquals("variable name", "a", lv.getName()); - InstructionHandle start = lv.getStart(); - InstructionHandle end = lv.getEnd(); - assertNotNull("scope start", start); - assertNotNull("scope end", end); - assertTrue("scope start not targeted by the local variable", Arrays.asList(start.getTargeters()).contains(lv)); - assertTrue("scope end not targeted by the local variable", Arrays.asList(end.getTargeters()).contains(lv)); - - // now let's remove the local variables - mg.removeLocalVariables(); - - assertFalse("scope start still targeted by the removed variable", Arrays.asList(start.getTargeters()).contains(lv)); - assertFalse("scope end still targeted by the removed variable", Arrays.asList(end.getTargeters()).contains(lv)); - assertNull("scope start", lv.getStart()); - assertNull("scope end", lv.getEnd()); - } -} diff --git a/src/test/java/org/apache/commons/bcel6/util/BCELifierTestCase.java b/src/test/java/org/apache/commons/bcel6/util/BCELifierTestCase.java deleted file mode 100644 index b42b94e9..00000000 --- a/src/test/java/org/apache/commons/bcel6/util/BCELifierTestCase.java +++ /dev/null @@ -1,78 +0,0 @@ -package org.apache.commons.bcel6.util; - -import static org.junit.Assert.*; - -import java.io.BufferedInputStream; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileOutputStream; -import java.io.InputStream; -import java.io.OutputStream; -import org.apache.commons.bcel6.classfile.JavaClass; -import org.junit.Test; - - -public class BCELifierTestCase { - - @Test - public void test() throws Exception { - OutputStream os = new ByteArrayOutputStream(); - JavaClass java_class = BCELifier.getJavaClass("Java8Example"); - assertNotNull(java_class); - BCELifier bcelifier = new BCELifier(java_class, os); - bcelifier.start(); - } - - /* - * Dump a class using "javap" and compare with the same class recreated - * using BCELifier, "javac", "java" and dumped with "javap" - * TODO: detect if JDK present and skip test if not - */ - @Test - @org.junit.Ignore // does not work properly on some systems. Also the output is rather different - public void testJavapCompare() throws Exception { - testClassOnPath("target/test-classes/Java8Example.class"); - } - - private void testClassOnPath(final String javaClass) throws Exception { - // Get javap of the input class - final String initial = exec(null, "javap", "-p", "-c", javaClass); - - final File workDir = new File("target"); - File infile = new File(javaClass); - JavaClass java_class = BCELifier.getJavaClass(infile.getName().replace(".class", "")); - assertNotNull(java_class); - File outfile = new File(workDir,infile.getName().replace(".class", "Creator.java")); - FileOutputStream fos = new FileOutputStream(outfile); - BCELifier bcelifier = new BCELifier(java_class, fos); - bcelifier.start(); - fos.close(); - exec(workDir, "javac", "-cp", "classes", outfile.getName()); - exec(workDir, "java", "-cp", ".:classes", outfile.getName().replace(".java", "")); - final String output = exec(workDir, "javap", "-p", "-c", infile.getName()); - assertEquals(initial, output); - } - - private String exec(final File workDir, final String ... args) throws Exception { -// System.err.println(java.util.Arrays.toString(args)); - ProcessBuilder pb = new ProcessBuilder( args ); - pb.directory(workDir); - Process proc = pb.start(); - BufferedInputStream is = new BufferedInputStream(proc.getInputStream()); - InputStream es = proc.getErrorStream(); - proc.waitFor(); - byte []buff=new byte[2048]; - int len; - while((len=es.read(buff)) != -1) { - System.err.print(new String(buff,0,len)); - } - - StringBuilder sb = new StringBuilder(); - while((len=is.read(buff)) != -1) { - sb.append(new String(buff,0,len)); - } - is.close(); - return sb.toString(); - } - -} diff --git a/src/test/java/org/apache/commons/bcel6/util/Class2HTMLTestCase.java b/src/test/java/org/apache/commons/bcel6/util/Class2HTMLTestCase.java deleted file mode 100644 index be468a32..00000000 --- a/src/test/java/org/apache/commons/bcel6/util/Class2HTMLTestCase.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * 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.commons.bcel6.util; - -import java.io.File; -import java.io.FileInputStream; - -import org.apache.commons.bcel6.classfile.ClassParser; -import org.junit.Assert; - -import junit.framework.TestCase; - -public class Class2HTMLTestCase extends TestCase { - - public void testConvertJavaUtil() throws Exception { - File outputDir = new File("target/test-output/html"); - if (!outputDir.mkdirs()) { // either was not created or already existed - Assert.assertTrue(outputDir.isDirectory()); // fail if missing - } - - FileInputStream file = new FileInputStream("target/test-classes/Java8Example.class"); - - ClassParser parser = new ClassParser(file, "Java8Example.class"); - - new Class2HTML(parser.parse(), outputDir.getAbsolutePath() + "/"); - } -} diff --git a/src/test/java/org/apache/commons/bcel6/util/InstructionFinderTest.java b/src/test/java/org/apache/commons/bcel6/util/InstructionFinderTest.java deleted file mode 100644 index 7701e841..00000000 --- a/src/test/java/org/apache/commons/bcel6/util/InstructionFinderTest.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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.commons.bcel6.util; - -import java.util.Iterator; - -import org.apache.commons.bcel6.AbstractTestCase; -import org.apache.commons.bcel6.generic.IADD; -import org.apache.commons.bcel6.generic.ILOAD; -import org.apache.commons.bcel6.generic.ISTORE; -import org.apache.commons.bcel6.generic.InstructionHandle; -import org.apache.commons.bcel6.generic.InstructionList; - -public class InstructionFinderTest extends AbstractTestCase { - - public void testSearch() { - InstructionList il = new InstructionList(); - il.append(new ILOAD(1)); - il.append(new ILOAD(2)); - il.append(new IADD()); - il.append(new ISTORE(3)); - InstructionFinder finder = new InstructionFinder(il); - - Iterator it = finder.search("ILOAD IADD", il.getInstructionHandles()[0], null ); - InstructionHandle[] ihs = (InstructionHandle[])it.next(); - assertEquals(2, ihs.length); - assertEquals(ihs[0].getInstruction(), new ILOAD(2)); - assertEquals(ihs[1].getInstruction(), new IADD()); - } -} diff --git a/src/test/java/org/apache/commons/bcel6/verifier/AbstractVerifierTestCase.java b/src/test/java/org/apache/commons/bcel6/verifier/AbstractVerifierTestCase.java deleted file mode 100644 index b773e8ba..00000000 --- a/src/test/java/org/apache/commons/bcel6/verifier/AbstractVerifierTestCase.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * 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.commons.bcel6.verifier; - -import org.apache.commons.bcel6.Repository; -import org.apache.commons.bcel6.classfile.JavaClass; - -import junit.framework.TestCase; - -public abstract class AbstractVerifierTestCase extends TestCase { - - public static final String TEST_PACKAGE = AbstractVerifierTestCase.class.getPackage().getName() + ".tests."; - - /** - * Asserts that the verification of the given class is OK. If it isn't it throws an AssertionFailedError with the given message. - * - * @param classname simple classname of the class to verify - * @param message message displayed if assertion fails - */ - public void assertVerifyOK(final String classname, final String message) { - final String testClassname = TEST_PACKAGE + classname; - assertTrue(message, doAllPasses(testClassname)); - } - - /** - * Asserts that the verification of the given class is rejected. - * If it isn't it throws an AssertionFailedError with the given message. - * - * @param classname simple classname of the class to verify - * @param message message displayed if assertion fails - */ - public void assertVerifyRejected(final String classname, final String message) { - final String testClassname = TEST_PACKAGE + classname; - assertFalse(message, doAllPasses(testClassname)); - } - - /** - * Executes all the verification on the given class. - * - * @param classname name of the class to verify - * @return false if the verification fails, true otherwise - */ - public boolean doAllPasses(final String classname) { - int nbMethods = 0; - - try { - JavaClass jc = Repository.lookupClass(classname); - nbMethods = jc.getMethods().length; - } catch (ClassNotFoundException e) { - fail(e.getMessage()); - return false; - } - - Verifier verifier = VerifierFactory.getVerifier(classname); - VerificationResult result = verifier.doPass1(); - if (result.getStatus() != VerificationResult.VERIFIED_OK) { - return false; - } - - result = verifier.doPass2(); - if (result.getStatus() != VerificationResult.VERIFIED_OK) { - return false; - } - - for (int i = nbMethods; --i >= 0;) { - result = verifier.doPass3a(i); - if (result.getStatus() != VerificationResult.VERIFIED_OK) { - return false; - } - result = verifier.doPass3b(i); - if (result.getStatus() != VerificationResult.VERIFIED_OK) { - return false; - } - } - - return true; - } - -} diff --git a/src/test/java/org/apache/commons/bcel6/verifier/VerifierArrayAccessTestCase.java b/src/test/java/org/apache/commons/bcel6/verifier/VerifierArrayAccessTestCase.java deleted file mode 100644 index 7408e87f..00000000 --- a/src/test/java/org/apache/commons/bcel6/verifier/VerifierArrayAccessTestCase.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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.commons.bcel6.verifier; - -import java.io.IOException; - -import org.apache.commons.bcel6.verifier.tests.TestArrayAccess02Creator; -import org.apache.commons.bcel6.verifier.tests.TestArrayAccess03Creator; -import org.apache.commons.bcel6.verifier.tests.TestArrayAccess04Creator; - -public class VerifierArrayAccessTestCase extends AbstractVerifierTestCase { - - public void testInvalidArrayAccess() throws IOException { - new TestArrayAccess03Creator().create(); - assertVerifyRejected("TestArrayAccess03", "Verification of an arraystore instruction on an object must fail."); - new TestArrayAccess04Creator().create(); - assertVerifyRejected("TestArrayAccess04", - "Verification of an arraystore instruction of an int on an array of references must fail."); - } - - public void testValidArrayAccess() throws IOException { - assertVerifyOK("TestArrayAccess01", - "Verification of an arraystore instruction on an array that is not compatible with the stored element must pass."); - new TestArrayAccess02Creator().create(); - assertVerifyOK("TestArrayAccess02", - "Verification of an arraystore instruction on an array that is not compatible with the stored element must pass."); - } - -} diff --git a/src/test/java/org/apache/commons/bcel6/verifier/VerifierInvokeTestCase.java b/src/test/java/org/apache/commons/bcel6/verifier/VerifierInvokeTestCase.java deleted file mode 100644 index 6a01a902..00000000 --- a/src/test/java/org/apache/commons/bcel6/verifier/VerifierInvokeTestCase.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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.commons.bcel6.verifier; - - -public class VerifierInvokeTestCase extends AbstractVerifierTestCase { - - public void testLegalInvokeVirtual() { - assertVerifyOK("TestLegalInvokeVirtual01", "Verification of invokevirtual on method defined in superclass must pass."); - assertVerifyOK("TestLegalInvokeVirtual02", "Verification of invokevirtual on method defined in superinterface must pass."); - } - - public void testLegalInvokeStatic() { - assertVerifyOK("TestLegalInvokeStatic01", "Verification of invokestatic on method defined in superclass must pass."); - } - - public void testLegalInvokeInterface() { - assertVerifyOK("TestLegalInvokeInterface01", "Verification of invokeinterface on method defined in superinterface must pass."); - } - - public void testLegalInvokeSpecial() { - assertVerifyOK("TestLegalInvokeSpecial01", "Verification of invokespecial on method defined in superclass must pass."); - assertVerifyOK("TestLegalInvokeSpecial02", "Verification of invokespecial on method defined in superclass must pass."); - } -} diff --git a/src/test/java/org/apache/commons/bcel6/verifier/VerifierReturnTestCase.java b/src/test/java/org/apache/commons/bcel6/verifier/VerifierReturnTestCase.java deleted file mode 100644 index dd6554d4..00000000 --- a/src/test/java/org/apache/commons/bcel6/verifier/VerifierReturnTestCase.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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.commons.bcel6.verifier; - -import java.io.IOException; - -import org.apache.commons.bcel6.verifier.tests.TestReturn01Creator; -import org.apache.commons.bcel6.verifier.tests.TestReturn03Creator; - -public class VerifierReturnTestCase extends AbstractVerifierTestCase { - - public void testInvalidReturn() throws IOException { - new TestReturn01Creator().create(); - assertVerifyRejected("TestReturn01", "Verification of a void method that returns an object must fail."); - new TestReturn03Creator().create(); - assertVerifyRejected("TestReturn03", "Verification of an int method that returns null must fail."); - } - - public void testValidReturn() { - assertVerifyOK("TestReturn02", "Verification of a method that returns a newly created object must pass."); - assertVerifyOK("TestArray01", "Verification of a method that returns an array must pass."); - } -} diff --git a/src/test/java/org/apache/commons/bcel6/verifier/VerifierTestCase.java b/src/test/java/org/apache/commons/bcel6/verifier/VerifierTestCase.java deleted file mode 100644 index 38ea8df5..00000000 --- a/src/test/java/org/apache/commons/bcel6/verifier/VerifierTestCase.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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.commons.bcel6.verifier; - -import java.util.Collection; - -import junit.framework.TestCase; - -public class VerifierTestCase extends TestCase { - - public void testDefaultMethodValidation() { - String classname = Collection.class.getName(); - - Verifier verifier = VerifierFactory.getVerifier(classname); - VerificationResult result = verifier.doPass1(); - - assertEquals("Pass 1 verification of " + classname + " failed: " + result.getMessage(), VerificationResult.VERIFIED_OK, - result.getStatus()); - - result = verifier.doPass2(); - - assertEquals("Pass 2 verification of " + classname + " failed: " + result.getMessage(), VerificationResult.VERIFIED_OK, - result.getStatus()); - } -} diff --git a/src/test/java/org/apache/commons/bcel6/verifier/tests/TestArray01.java b/src/test/java/org/apache/commons/bcel6/verifier/tests/TestArray01.java deleted file mode 100644 index e97aa1c8..00000000 --- a/src/test/java/org/apache/commons/bcel6/verifier/tests/TestArray01.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * 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.commons.bcel6.verifier.tests; - -import java.io.Serializable; - -public class TestArray01{ - - public static Object test1(){ - String[] a = new String[4]; - a[0] = ""; - a.equals(null); - test2(a); - test3(a); - test4(a); - return a; - } - - public static void test2(final Object o){ - } - - public static void test3(final Serializable o){ - } - - public static void test4(final Cloneable o){ - } - - public static Serializable test5(){ - return new Object[1]; - } - - public static Cloneable test6(){ - return new Object[1]; - } - - public static Object foo(final String s){ - return s; - } -} diff --git a/src/test/java/org/apache/commons/bcel6/verifier/tests/TestArrayAccess01.java b/src/test/java/org/apache/commons/bcel6/verifier/tests/TestArrayAccess01.java deleted file mode 100644 index 7815c194..00000000 --- a/src/test/java/org/apache/commons/bcel6/verifier/tests/TestArrayAccess01.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * 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.commons.bcel6.verifier.tests; - - -public class TestArrayAccess01 extends XTestArray01{ - - public static void test(){ - XTestArray01[] array = new TestArrayAccess01[1]; - array[0] = new XTestArray01(); - } - -} - -class XTestArray01 { - -} diff --git a/src/test/java/org/apache/commons/bcel6/verifier/tests/TestArrayAccess02Creator.java b/src/test/java/org/apache/commons/bcel6/verifier/tests/TestArrayAccess02Creator.java deleted file mode 100644 index 5442121b..00000000 --- a/src/test/java/org/apache/commons/bcel6/verifier/tests/TestArrayAccess02Creator.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * 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.commons.bcel6.verifier.tests; - -import java.io.IOException; -import java.io.OutputStream; - -import org.apache.commons.bcel6.Const; -import org.apache.commons.bcel6.generic.ClassGen; -import org.apache.commons.bcel6.generic.ConstantPoolGen; -import org.apache.commons.bcel6.generic.InstructionConst; -import org.apache.commons.bcel6.generic.InstructionFactory; -import org.apache.commons.bcel6.generic.InstructionHandle; -import org.apache.commons.bcel6.generic.InstructionList; -import org.apache.commons.bcel6.generic.MethodGen; -import org.apache.commons.bcel6.generic.ObjectType; -import org.apache.commons.bcel6.generic.PUSH; -import org.apache.commons.bcel6.generic.Type; -import org.junit.Assert; - -public class TestArrayAccess02Creator extends TestCreator { - private final InstructionFactory _factory; - private final ConstantPoolGen _cp; - private final ClassGen _cg; - - public TestArrayAccess02Creator() { - _cg = new ClassGen(TEST_PACKAGE+".TestArrayAccess02", "java.lang.Object", "TestArrayAccess02.java", - Const.ACC_PUBLIC | Const.ACC_SUPER, new String[] { }); - - _cp = _cg.getConstantPool(); - _factory = new InstructionFactory(_cg, _cp); - } - - @Override -public void create(final OutputStream out) throws IOException { - createMethod_0(); - createMethod_1(); - _cg.getJavaClass().dump(out); - } - - private void createMethod_0() { - InstructionList il = new InstructionList(); - MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] { }, "", - TEST_PACKAGE+".TestArrayAccess02", il, _cp); - - InstructionHandle ih_0 = il.append(InstructionFactory.createLoad(Type.OBJECT, 0)); - Assert.assertNotNull(ih_0); // TODO why is this not used - il.append(_factory.createInvoke("java.lang.Object", "", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL)); - InstructionHandle ih_4 = il.append(InstructionFactory.createReturn(Type.VOID)); - Assert.assertNotNull(ih_4); // TODO why is this not used - method.setMaxStack(); - method.setMaxLocals(); - _cg.addMethod(method.getMethod()); - il.dispose(); - } - - private void createMethod_1() { - InstructionList il = new InstructionList(); - MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.VOID, Type.NO_ARGS, new String[] { }, - "test", TEST_PACKAGE+".TestArrayAccess02", il, _cp); - - InstructionHandle ih_0 = il.append(new PUSH(_cp, 1)); - Assert.assertNotNull(ih_0); // TODO why is this not used - il.append(_factory.createNewArray(new ObjectType(TEST_PACKAGE+".TestArrayAccess02"), (short) 1)); - il.append(InstructionFactory.createStore(Type.OBJECT, 0)); - InstructionHandle ih_5 = il.append(new PUSH(_cp, 1)); - Assert.assertNotNull(ih_5); // TODO why is this not used - il.append(_factory.createNewArray(Type.STRING, (short) 1)); - il.append(InstructionFactory.createStore(Type.OBJECT, 1)); - InstructionHandle ih_10 = il.append(InstructionFactory.createLoad(Type.OBJECT, 1)); - Assert.assertNotNull(ih_10); // TODO why is this not used - il.append(new PUSH(_cp, 0)); - il.append(_factory.createNew(TEST_PACKAGE+".TestArrayAccess02")); - il.append(InstructionConst.DUP); - il.append(_factory.createInvoke(TEST_PACKAGE+".TestArrayAccess02", "", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL)); - il.append(InstructionConst.AASTORE); - InstructionHandle ih_20 = il.append(InstructionFactory.createReturn(Type.VOID)); - Assert.assertNotNull(ih_20); // TODO why is this not used - method.setMaxStack(); - method.setMaxLocals(); - _cg.addMethod(method.getMethod()); - il.dispose(); - } -} diff --git a/src/test/java/org/apache/commons/bcel6/verifier/tests/TestArrayAccess03Creator.java b/src/test/java/org/apache/commons/bcel6/verifier/tests/TestArrayAccess03Creator.java deleted file mode 100644 index d9cae0b8..00000000 --- a/src/test/java/org/apache/commons/bcel6/verifier/tests/TestArrayAccess03Creator.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * 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.commons.bcel6.verifier.tests; - -import java.io.IOException; -import java.io.OutputStream; - -import org.apache.commons.bcel6.Const; -import org.apache.commons.bcel6.generic.ClassGen; -import org.apache.commons.bcel6.generic.ConstantPoolGen; -import org.apache.commons.bcel6.generic.InstructionConst; -import org.apache.commons.bcel6.generic.InstructionFactory; -import org.apache.commons.bcel6.generic.InstructionHandle; -import org.apache.commons.bcel6.generic.InstructionList; -import org.apache.commons.bcel6.generic.MethodGen; -import org.apache.commons.bcel6.generic.ObjectType; -import org.apache.commons.bcel6.generic.PUSH; -import org.apache.commons.bcel6.generic.Type; -import org.junit.Assert; - -public class TestArrayAccess03Creator extends TestCreator { - private final InstructionFactory _factory; - private final ConstantPoolGen _cp; - private final ClassGen _cg; - - public TestArrayAccess03Creator() { - _cg = new ClassGen(TEST_PACKAGE+".TestArrayAccess03", "java.lang.Object", "TestArrayAccess03.java", - Const.ACC_PUBLIC | Const.ACC_SUPER, new String[] { }); - - _cp = _cg.getConstantPool(); - _factory = new InstructionFactory(_cg, _cp); - } - - @Override -public void create(final OutputStream out) throws IOException { - createMethod_0(); - createMethod_1(); - _cg.getJavaClass().dump(out); - } - - private void createMethod_0() { - InstructionList il = new InstructionList(); - MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] { }, "", - TEST_PACKAGE+".TestArrayAccess03", il, _cp); - - InstructionHandle ih_0 = il.append(InstructionFactory.createLoad(Type.OBJECT, 0)); - Assert.assertNotNull(ih_0); // TODO why is this not used - il.append(_factory.createInvoke("java.lang.Object", "", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL)); - InstructionHandle ih_4 = il.append(InstructionFactory.createReturn(Type.VOID)); - Assert.assertNotNull(ih_4); // TODO why is this not used - method.setMaxStack(); - method.setMaxLocals(); - _cg.addMethod(method.getMethod()); - il.dispose(); - } - - private void createMethod_1() { - InstructionList il = new InstructionList(); - MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.VOID, new Type[] { Type.OBJECT }, - new String[] { "arg0" }, "test", TEST_PACKAGE+".TestArrayAccess03", il, _cp); - - InstructionHandle ih_0 = il.append(new PUSH(_cp, 1)); - Assert.assertNotNull(ih_0); // TODO why is this not used - il.append(_factory.createNewArray(new ObjectType(TEST_PACKAGE+".TestArrayAccess03"), (short) 1)); - il.append(InstructionFactory.createStore(Type.OBJECT, 1)); - InstructionHandle ih_5 = il.append(InstructionFactory.createLoad(Type.OBJECT, 0)); - Assert.assertNotNull(ih_5); // TODO why is this not used - il.append(new PUSH(_cp, 0)); - il.append(_factory.createNew(TEST_PACKAGE+".TestArrayAccess03")); - il.append(InstructionConst.DUP); - il.append(_factory.createInvoke(TEST_PACKAGE+".TestArrayAccess03", "", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL)); - il.append(InstructionConst.AASTORE); - InstructionHandle ih_15 = il.append(InstructionFactory.createReturn(Type.VOID)); - Assert.assertNotNull(ih_15); // TODO why is this not used - method.setMaxStack(); - method.setMaxLocals(); - _cg.addMethod(method.getMethod()); - il.dispose(); - } -} diff --git a/src/test/java/org/apache/commons/bcel6/verifier/tests/TestArrayAccess04Creator.java b/src/test/java/org/apache/commons/bcel6/verifier/tests/TestArrayAccess04Creator.java deleted file mode 100644 index d4c2bf8b..00000000 --- a/src/test/java/org/apache/commons/bcel6/verifier/tests/TestArrayAccess04Creator.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * 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.commons.bcel6.verifier.tests; - -import java.io.IOException; -import java.io.OutputStream; - -import org.apache.commons.bcel6.Const; -import org.apache.commons.bcel6.generic.ClassGen; -import org.apache.commons.bcel6.generic.ConstantPoolGen; -import org.apache.commons.bcel6.generic.InstructionConst; -import org.apache.commons.bcel6.generic.InstructionFactory; -import org.apache.commons.bcel6.generic.InstructionHandle; -import org.apache.commons.bcel6.generic.InstructionList; -import org.apache.commons.bcel6.generic.MethodGen; -import org.apache.commons.bcel6.generic.PUSH; -import org.apache.commons.bcel6.generic.Type; -import org.junit.Assert; - -public class TestArrayAccess04Creator extends TestCreator { - private final InstructionFactory _factory; - private final ConstantPoolGen _cp; - private final ClassGen _cg; - - public TestArrayAccess04Creator() { - _cg = new ClassGen(TEST_PACKAGE+".TestArrayAccess04", "java.lang.Object", "TestArrayAccess04.java", - Const.ACC_PUBLIC | Const.ACC_SUPER, new String[] { }); - - _cp = _cg.getConstantPool(); - _factory = new InstructionFactory(_cg, _cp); - } - - @Override -public void create(final OutputStream out) throws IOException { - createMethod_0(); - createMethod_1(); - _cg.getJavaClass().dump(out); - } - - private void createMethod_0() { - InstructionList il = new InstructionList(); - MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] { }, "", - TEST_PACKAGE+".TestArrayAccess04", il, _cp); - - InstructionHandle ih_0 = il.append(InstructionFactory.createLoad(Type.OBJECT, 0)); - Assert.assertNotNull(ih_0); // TODO why is this not used - il.append(_factory.createInvoke("java.lang.Object", "", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL)); - InstructionHandle ih_4 = il.append(InstructionFactory.createReturn(Type.VOID)); - Assert.assertNotNull(ih_4); // TODO why is this not used - method.setMaxStack(); - method.setMaxLocals(); - _cg.addMethod(method.getMethod()); - il.dispose(); - } - - private void createMethod_1() { - InstructionList il = new InstructionList(); - MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.VOID, new Type[] { Type.OBJECT }, - new String[] { "arg0" }, "test", TEST_PACKAGE+".TestArrayAccess04", il, _cp); - - InstructionHandle ih_0 = il.append(new PUSH(_cp, 1)); - Assert.assertNotNull(ih_0); // TODO why is this not used - il.append(_factory.createNewArray(Type.OBJECT, (short) 1)); - il.append(InstructionFactory.createStore(Type.OBJECT, 1)); - InstructionHandle ih_5 = il.append(new PUSH(_cp, 1)); - Assert.assertNotNull(ih_5); // TODO why is this not used - il.append(InstructionFactory.createStore(Type.INT, 2)); - InstructionHandle ih_7 = il.append(InstructionFactory.createLoad(Type.OBJECT, 1)); - Assert.assertNotNull(ih_7); // TODO why is this not used - il.append(new PUSH(_cp, 0)); - il.append(InstructionFactory.createLoad(Type.INT, 2)); - il.append(InstructionConst.AASTORE); - InstructionHandle ih_11 = il.append(InstructionFactory.createReturn(Type.VOID)); - Assert.assertNotNull(ih_11); // TODO why is this not used - method.setMaxStack(); - method.setMaxLocals(); - _cg.addMethod(method.getMethod()); - il.dispose(); - } -} diff --git a/src/test/java/org/apache/commons/bcel6/verifier/tests/TestCreator.java b/src/test/java/org/apache/commons/bcel6/verifier/tests/TestCreator.java deleted file mode 100644 index 1ae14722..00000000 --- a/src/test/java/org/apache/commons/bcel6/verifier/tests/TestCreator.java +++ /dev/null @@ -1,64 +0,0 @@ -package org.apache.commons.bcel6.verifier.tests; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.net.URISyntaxException; - -/* - * 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. - * - */ -public abstract class TestCreator { - - // Common package base name for generated test classes - protected static final String TEST_PACKAGE = TestCreator.class.getPackage().getName(); - - public void create() throws IOException { - File classFile = new File(getPackageFolder(), getClassName()); - FileOutputStream out = new FileOutputStream(classFile); - try { - create(out); - } - finally { - out.close(); - } - } - - private String getClassName() { - String name = getClass().getName(); - return name.substring(name.lastIndexOf('.')+1).replace("Creator", ".class"); - } - - private File getPackageFolder() throws IOException { - return new File(getClassesFolder(), getPackageName()); - } - - protected String getPackageName() { - return getClass().getPackage().getName().replace('.', '/'); - } - - private File getClassesFolder() throws IOException { - try { - return new File(getClass().getProtectionDomain().getCodeSource().getLocation().toURI()); - } catch (URISyntaxException e) { - throw new IOException(e); - } - } - - public abstract void create(OutputStream out) throws IOException; -} diff --git a/src/test/java/org/apache/commons/bcel6/verifier/tests/TestLegalInvokeInterface01.java b/src/test/java/org/apache/commons/bcel6/verifier/tests/TestLegalInvokeInterface01.java deleted file mode 100755 index 784ab213..00000000 --- a/src/test/java/org/apache/commons/bcel6/verifier/tests/TestLegalInvokeInterface01.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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.commons.bcel6.verifier.tests; - -public class TestLegalInvokeInterface01{ - - public static void test1(final Interface01 t){ - t.run(); - } -} - -interface Interface01 extends Runnable { - -} diff --git a/src/test/java/org/apache/commons/bcel6/verifier/tests/TestLegalInvokeSpecial01.java b/src/test/java/org/apache/commons/bcel6/verifier/tests/TestLegalInvokeSpecial01.java deleted file mode 100755 index df428a75..00000000 --- a/src/test/java/org/apache/commons/bcel6/verifier/tests/TestLegalInvokeSpecial01.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * 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.commons.bcel6.verifier.tests; - -public class TestLegalInvokeSpecial01{ - - public static void test1(){ - new TestLegalInvokeSpecial01().getClass(); - } - -} diff --git a/src/test/java/org/apache/commons/bcel6/verifier/tests/TestLegalInvokeSpecial02.java b/src/test/java/org/apache/commons/bcel6/verifier/tests/TestLegalInvokeSpecial02.java deleted file mode 100755 index 346f762a..00000000 --- a/src/test/java/org/apache/commons/bcel6/verifier/tests/TestLegalInvokeSpecial02.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * 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.commons.bcel6.verifier.tests; - -public abstract class TestLegalInvokeSpecial02 implements Runnable{ - - public static void test1(final TestLegalInvokeSpecial02 t, final int i){ - if(i > 0){ - t.run(); - } - } - -} diff --git a/src/test/java/org/apache/commons/bcel6/verifier/tests/TestLegalInvokeStatic01.java b/src/test/java/org/apache/commons/bcel6/verifier/tests/TestLegalInvokeStatic01.java deleted file mode 100755 index 927d8867..00000000 --- a/src/test/java/org/apache/commons/bcel6/verifier/tests/TestLegalInvokeStatic01.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * 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.commons.bcel6.verifier.tests; - -public class TestLegalInvokeStatic01 extends Thread{ - - public static void test1() throws InterruptedException{ - Thread.sleep(0); - } - -} diff --git a/src/test/java/org/apache/commons/bcel6/verifier/tests/TestLegalInvokeVirtual01.java b/src/test/java/org/apache/commons/bcel6/verifier/tests/TestLegalInvokeVirtual01.java deleted file mode 100755 index a94066e2..00000000 --- a/src/test/java/org/apache/commons/bcel6/verifier/tests/TestLegalInvokeVirtual01.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * 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.commons.bcel6.verifier.tests; - -public class TestLegalInvokeVirtual01 { - - public static void test1(){ - new TestLegalInvokeVirtual01().toString(); - } - -} diff --git a/src/test/java/org/apache/commons/bcel6/verifier/tests/TestLegalInvokeVirtual02.java b/src/test/java/org/apache/commons/bcel6/verifier/tests/TestLegalInvokeVirtual02.java deleted file mode 100755 index b5a579ba..00000000 --- a/src/test/java/org/apache/commons/bcel6/verifier/tests/TestLegalInvokeVirtual02.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * 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.commons.bcel6.verifier.tests; - -public abstract class TestLegalInvokeVirtual02 implements Runnable{ - - public static void test1(final TestLegalInvokeVirtual02 t, final int i){ - if(i > 0){ - t.run(); - } - } - -} diff --git a/src/test/java/org/apache/commons/bcel6/verifier/tests/TestReturn01Creator.java b/src/test/java/org/apache/commons/bcel6/verifier/tests/TestReturn01Creator.java deleted file mode 100644 index a2764d6e..00000000 --- a/src/test/java/org/apache/commons/bcel6/verifier/tests/TestReturn01Creator.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * 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.commons.bcel6.verifier.tests; - -import java.io.IOException; -import java.io.OutputStream; - -import org.apache.commons.bcel6.Const; -import org.apache.commons.bcel6.generic.ClassGen; -import org.apache.commons.bcel6.generic.ConstantPoolGen; -import org.apache.commons.bcel6.generic.InstructionConst; -import org.apache.commons.bcel6.generic.InstructionFactory; -import org.apache.commons.bcel6.generic.InstructionHandle; -import org.apache.commons.bcel6.generic.InstructionList; -import org.apache.commons.bcel6.generic.MethodGen; -import org.apache.commons.bcel6.generic.Type; -import org.junit.Assert; - -public class TestReturn01Creator extends TestCreator { - private final InstructionFactory _factory; - private final ConstantPoolGen _cp; - private final ClassGen _cg; - - public TestReturn01Creator() { - _cg = new ClassGen(TEST_PACKAGE+".TestReturn01", "java.lang.Object", "TestReturn01.java", - Const.ACC_PUBLIC | Const.ACC_SUPER, new String[] { }); - - _cp = _cg.getConstantPool(); - _factory = new InstructionFactory(_cg, _cp); - } - - @Override -public void create(final OutputStream out) throws IOException { - createMethod_0(); - createMethod_1(); - _cg.getJavaClass().dump(out); - } - - private void createMethod_0() { - InstructionList il = new InstructionList(); - MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] { }, - "", TEST_PACKAGE+".TestReturn01", il, _cp); - - InstructionHandle ih_0 = il.append(InstructionFactory.createLoad(Type.OBJECT, 0)); - Assert.assertNotNull(ih_0); // TODO why is this not used - il.append(_factory.createInvoke("java.lang.Object", "", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL)); - InstructionHandle ih_4 = il.append(InstructionFactory.createReturn(Type.VOID)); - Assert.assertNotNull(ih_4); // TODO why is this not used - method.setMaxStack(); - method.setMaxLocals(); - _cg.addMethod(method.getMethod()); - il.dispose(); - } - - private void createMethod_1() { - InstructionList il = new InstructionList(); - MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.VOID, Type.NO_ARGS, - new String[] { }, "foo", TEST_PACKAGE+".TestReturn01", il, _cp); - - InstructionHandle ih_0 = il.append(_factory.createNew("java.lang.Object")); - Assert.assertNotNull(ih_0); // TODO why is this not used - il.append(InstructionConst.DUP); - il.append(_factory.createInvoke("java.lang.Object", "", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL)); - il.append(InstructionConst.NOP); - InstructionHandle ih_8 = il.append(InstructionFactory.createReturn(Type.OBJECT)); - Assert.assertNotNull(ih_8); // TODO why is this not used - method.setMaxStack(); - method.setMaxLocals(); - _cg.addMethod(method.getMethod()); - il.dispose(); - } -} diff --git a/src/test/java/org/apache/commons/bcel6/verifier/tests/TestReturn02.java b/src/test/java/org/apache/commons/bcel6/verifier/tests/TestReturn02.java deleted file mode 100644 index 0e44e3bd..00000000 --- a/src/test/java/org/apache/commons/bcel6/verifier/tests/TestReturn02.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * 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.commons.bcel6.verifier.tests; - -public class TestReturn02 { - - public static String test1(final char[] data, final int offset, final int count) { - return new String(data, offset, count); - } - - public static Object test2(){ - return new Object(); - } - - public static boolean test3(){ - return true; - } - - public static byte test4(){ - return 1; - } - - public static short test5(){ - return 1; - } - - public static char test6(){ - return 'a'; - } - - public static int test7(){ - return 1; - } - - public static long test8(){ - return 1L; - } - - public static float test9(){ - return 1.0f; - } - - public static double test10(){ - return 1.0; - } - - public static Object test11(){ - return null; - } -} diff --git a/src/test/java/org/apache/commons/bcel6/verifier/tests/TestReturn03Creator.java b/src/test/java/org/apache/commons/bcel6/verifier/tests/TestReturn03Creator.java deleted file mode 100644 index 7e500d19..00000000 --- a/src/test/java/org/apache/commons/bcel6/verifier/tests/TestReturn03Creator.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * 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.commons.bcel6.verifier.tests; - -import java.io.IOException; -import java.io.OutputStream; - -import org.apache.commons.bcel6.Const; -import org.apache.commons.bcel6.generic.ClassGen; -import org.apache.commons.bcel6.generic.ConstantPoolGen; -import org.apache.commons.bcel6.generic.InstructionConst; -import org.apache.commons.bcel6.generic.InstructionFactory; -import org.apache.commons.bcel6.generic.InstructionHandle; -import org.apache.commons.bcel6.generic.InstructionList; -import org.apache.commons.bcel6.generic.MethodGen; -import org.apache.commons.bcel6.generic.Type; -import org.junit.Assert; - -public class TestReturn03Creator extends TestCreator { - private final InstructionFactory _factory; - private final ConstantPoolGen _cp; - private final ClassGen _cg; - - public TestReturn03Creator() { - _cg = new ClassGen(TEST_PACKAGE+".TestReturn03", "java.lang.Object", "TestReturn03.java", - Const.ACC_PUBLIC | Const.ACC_SUPER, new String[] { }); - - _cp = _cg.getConstantPool(); - _factory = new InstructionFactory(_cg, _cp); - } - - @Override -public void create(final OutputStream out) throws IOException { - createMethod_0(); - createMethod_1(); - _cg.getJavaClass().dump(out); - } - - private void createMethod_0() { - InstructionList il = new InstructionList(); - MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] { }, - "", TEST_PACKAGE+".TestReturn03", il, _cp); - - InstructionHandle ih_0 = il.append(InstructionFactory.createLoad(Type.OBJECT, 0)); - Assert.assertNotNull(ih_0); // TODO why is this not used - il.append(_factory.createInvoke("java.lang.Object", "", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL)); - InstructionHandle ih_4 = il.append(InstructionFactory.createReturn(Type.VOID)); - Assert.assertNotNull(ih_4); // TODO why is this not used - method.setMaxStack(); - method.setMaxLocals(); - _cg.addMethod(method.getMethod()); - il.dispose(); - } - - private void createMethod_1() { - InstructionList il = new InstructionList(); - MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.INT, Type.NO_ARGS, - new String[] { }, "test3", TEST_PACKAGE+".TestReturn03", il, _cp); - - InstructionHandle ih_0 = il.append(InstructionConst.ACONST_NULL); - Assert.assertNotNull(ih_0); // TODO why is this not used - il.append(InstructionFactory.createReturn(Type.OBJECT)); - method.setMaxStack(); - method.setMaxLocals(); - _cg.addMethod(method.getMethod()); - il.dispose(); - } -} diff --git a/src/test/java/org/apache/commons/bcel6/visitors/CounterVisitor.java b/src/test/java/org/apache/commons/bcel6/visitors/CounterVisitor.java deleted file mode 100644 index cd578538..00000000 --- a/src/test/java/org/apache/commons/bcel6/visitors/CounterVisitor.java +++ /dev/null @@ -1,423 +0,0 @@ -/* - * 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.commons.bcel6.visitors; - -import org.apache.commons.bcel6.classfile.AnnotationDefault; -import org.apache.commons.bcel6.classfile.AnnotationEntry; -import org.apache.commons.bcel6.classfile.Annotations; -import org.apache.commons.bcel6.classfile.BootstrapMethods; -import org.apache.commons.bcel6.classfile.Code; -import org.apache.commons.bcel6.classfile.CodeException; -import org.apache.commons.bcel6.classfile.ConstantClass; -import org.apache.commons.bcel6.classfile.ConstantDouble; -import org.apache.commons.bcel6.classfile.ConstantFieldref; -import org.apache.commons.bcel6.classfile.ConstantFloat; -import org.apache.commons.bcel6.classfile.ConstantInteger; -import org.apache.commons.bcel6.classfile.ConstantInterfaceMethodref; -import org.apache.commons.bcel6.classfile.ConstantInvokeDynamic; -import org.apache.commons.bcel6.classfile.ConstantLong; -import org.apache.commons.bcel6.classfile.ConstantMethodHandle; -import org.apache.commons.bcel6.classfile.ConstantMethodType; -import org.apache.commons.bcel6.classfile.ConstantMethodref; -import org.apache.commons.bcel6.classfile.ConstantNameAndType; -import org.apache.commons.bcel6.classfile.ConstantPool; -import org.apache.commons.bcel6.classfile.ConstantString; -import org.apache.commons.bcel6.classfile.ConstantUtf8; -import org.apache.commons.bcel6.classfile.ConstantValue; -import org.apache.commons.bcel6.classfile.Deprecated; -import org.apache.commons.bcel6.classfile.EnclosingMethod; -import org.apache.commons.bcel6.classfile.ExceptionTable; -import org.apache.commons.bcel6.classfile.Field; -import org.apache.commons.bcel6.classfile.InnerClass; -import org.apache.commons.bcel6.classfile.InnerClasses; -import org.apache.commons.bcel6.classfile.JavaClass; -import org.apache.commons.bcel6.classfile.LineNumber; -import org.apache.commons.bcel6.classfile.LineNumberTable; -import org.apache.commons.bcel6.classfile.LocalVariable; -import org.apache.commons.bcel6.classfile.LocalVariableTable; -import org.apache.commons.bcel6.classfile.LocalVariableTypeTable; -import org.apache.commons.bcel6.classfile.Method; -import org.apache.commons.bcel6.classfile.MethodParameters; -import org.apache.commons.bcel6.classfile.ParameterAnnotationEntry; -import org.apache.commons.bcel6.classfile.ParameterAnnotations; -import org.apache.commons.bcel6.classfile.Signature; -import org.apache.commons.bcel6.classfile.SourceFile; -import org.apache.commons.bcel6.classfile.StackMap; -import org.apache.commons.bcel6.classfile.StackMapEntry; -import org.apache.commons.bcel6.classfile.Synthetic; -import org.apache.commons.bcel6.classfile.Unknown; -import org.apache.commons.bcel6.classfile.Visitor; - -public class CounterVisitor implements Visitor -{ - // CHECKSTYLE:OFF (public mutable fields in test code) - public int unknownCount = 0; - - public int syntheticCount = 0; - - public int stackMapEntryCount = 0; - - public int stackMapCount = 0; - - public int sourceFileCount = 0; - - public int signatureAnnotationCount = 0; - - public int parameterAnnotationCount = 0; - - public int methodCount = 0; - - public int localVariableTypeTableCount = 0; - - public int localVariableTableCount = 0; - - public int localVariableCount = 0; - - public int lineNumberTableCount = 0; - - public int lineNumberCount = 0; - - public int javaClassCount = 0; - - public int innerClassesCount = 0; - - public int innerClassCount = 0; - - public int fieldCount = 0; - - public int exceptionTableCount = 0; - - public int enclosingMethodCount = 0; - - public int deprecatedCount = 0; - - public int constantValueCount = 0; - - public int constantUtf8Count = 0; - - public int constantStringCount = 0; - - public int constantNameAndTypeCount = 0; - - public int constantPoolCount = 0; - - public int constantMethodrefCount = 0; - - public int constantLongCount = 0; - - public int constantIntegerCount = 0; - - public int constantInterfaceMethodrefCount = 0; - - public int constantFloatCount = 0; - - public int constantFieldrefCount = 0; - - public int constantClassCount = 0; - - public int constantDoubleCount = 0; - - public int codeExceptionCount = 0; - - public int codeCount = 0; - - public int annotationEntryCount = 0; - - public int annotationDefaultCount = 0; - - public int annotationCount = 0; - - /** @since 6.0 */ - public int bootstrapMethodsCount = 0; - - /** @since 6.0 */ - public int methodParametersCount = 0; - - /** @since 6.0 */ - public int constantInvokeDynamic = 0; - // CHECKSTYLE:ON - - - @Override - public void visitAnnotation(final Annotations obj) - { - annotationCount++; - } - - @Override - public void visitAnnotationDefault(final AnnotationDefault obj) - { - annotationDefaultCount++; - } - - @Override - public void visitAnnotationEntry(final AnnotationEntry obj) - { - annotationEntryCount++; - } - - @Override - public void visitCode(final Code obj) - { - codeCount++; - } - - @Override - public void visitCodeException(final CodeException obj) - { - codeExceptionCount++; - } - - @Override - public void visitConstantClass(final ConstantClass obj) - { - constantClassCount++; - } - - @Override - public void visitConstantDouble(final ConstantDouble obj) - { - constantDoubleCount++; - } - - @Override - public void visitConstantFieldref(final ConstantFieldref obj) - { - constantFieldrefCount++; - } - - @Override - public void visitConstantFloat(final ConstantFloat obj) - { - constantFloatCount++; - } - - @Override - public void visitConstantInteger(final ConstantInteger obj) - { - constantIntegerCount++; - } - - @Override - public void visitConstantInterfaceMethodref(final ConstantInterfaceMethodref obj) - { - constantInterfaceMethodrefCount++; - } - - @Override - public void visitConstantLong(final ConstantLong obj) - { - constantLongCount++; - } - - @Override - public void visitConstantMethodref(final ConstantMethodref obj) - { - constantMethodrefCount++; - } - - @Override - public void visitConstantNameAndType(final ConstantNameAndType obj) - { - constantNameAndTypeCount++; - } - - @Override - public void visitConstantPool(final ConstantPool obj) - { - constantPoolCount++; - } - - @Override - public void visitConstantString(final ConstantString obj) - { - constantStringCount++; - } - - @Override - public void visitConstantUtf8(final ConstantUtf8 obj) - { - constantUtf8Count++; - } - - @Override - public void visitConstantValue(final ConstantValue obj) - { - constantValueCount++; - } - - @Override - public void visitDeprecated(final Deprecated obj) - { - deprecatedCount++; - } - - @Override - public void visitEnclosingMethod(final EnclosingMethod obj) - { - enclosingMethodCount++; - } - - @Override - public void visitExceptionTable(final ExceptionTable obj) - { - exceptionTableCount++; - } - - @Override - public void visitField(final Field obj) - { - fieldCount++; - } - - @Override - public void visitInnerClass(final InnerClass obj) - { - innerClassCount++; - } - - @Override - public void visitInnerClasses(final InnerClasses obj) - { - innerClassesCount++; - } - - @Override - public void visitJavaClass(final JavaClass obj) - { - javaClassCount++; - } - - @Override - public void visitLineNumber(final LineNumber obj) - { - lineNumberCount++; - } - - @Override - public void visitLineNumberTable(final LineNumberTable obj) - { - lineNumberTableCount++; - } - - @Override - public void visitLocalVariable(final LocalVariable obj) - { - localVariableCount++; - } - - @Override - public void visitLocalVariableTable(final LocalVariableTable obj) - { - localVariableTableCount++; - } - - @Override - public void visitLocalVariableTypeTable(final LocalVariableTypeTable obj) - { - localVariableTypeTableCount++; - } - - @Override - public void visitMethod(final Method obj) - { - methodCount++; - } - - @Override - public void visitParameterAnnotation(final ParameterAnnotations obj) - { - parameterAnnotationCount++; - } - - @Override - public void visitSignature(final Signature obj) - { - signatureAnnotationCount++; - } - - @Override - public void visitSourceFile(final SourceFile obj) - { - sourceFileCount++; - } - - @Override - public void visitStackMap(final StackMap obj) - { - stackMapCount++; - } - - @Override - public void visitStackMapEntry(final StackMapEntry obj) - { - stackMapEntryCount++; - } - - @Override - public void visitSynthetic(final Synthetic obj) - { - syntheticCount++; - } - - @Override - public void visitUnknown(final Unknown obj) - { - unknownCount++; - } - - /** @since 6.0 */ - @Override - public void visitBootstrapMethods(final BootstrapMethods obj) - { - bootstrapMethodsCount++; - } - - /** @since 6.0 */ - @Override - public void visitMethodParameters(final MethodParameters obj) - { - methodParametersCount++; - } - - /** @since 6.0 */ - @Override - public void visitConstantInvokeDynamic(final ConstantInvokeDynamic obj) - { - constantInvokeDynamic++; - } - - /** @since 6.0 */ - @Override - public void visitConstantMethodType(final ConstantMethodType obj) { - // TODO Auto-generated method stub - } - - /** @since 6.0 */ - @Override - public void visitConstantMethodHandle(final ConstantMethodHandle constantMethodHandle) { - // TODO Auto-generated method stub - } - - /** @since 6.0 */ - @Override - public void visitParameterAnnotationEntry(final ParameterAnnotationEntry parameterAnnotationEntry) { - // TODO Auto-generated method stub - } -} -- cgit v1.2.3