Back to Repositories

Testing Java Bytecode Decompilation Functionality in Alibaba Arthas

This test suite validates the decompiler functionality in Arthas core utilities, focusing on Java bytecode decompilation and source code reconstruction capabilities. The tests ensure accurate decompilation of class files while preserving line numbers and code structure.

Test Coverage Overview

The test coverage focuses on the core decompilation functionality of Arthas.

Key areas tested include:
  • Class file path resolution and loading
  • Decompilation of bytecode to readable Java source
  • Verification of source code line numbers
  • Preservation of original code structure

Implementation Analysis

The testing approach uses JUnit 4 framework with AssertJ assertions for validation. The implementation leverages Java’s Protection Domain and CodeSource APIs to locate test class files, followed by decompilation and content verification.

Notable patterns include:
  • Dynamic class file path resolution
  • Source code pattern matching
  • Line number preservation testing

Technical Details

Testing tools and configuration:
  • JUnit 4 test framework
  • AssertJ assertion library
  • Java Protection Domain API
  • Custom Decompiler utility class
  • File system operations for class file handling

Best Practices Demonstrated

The test suite demonstrates several testing best practices for Java decompilation verification.

Notable practices include:
  • Isolated test environment setup
  • Dynamic resource location
  • Specific assertion criteria
  • Real-world code structure testing

alibaba/arthas

core/src/test/java/com/taobao/arthas/core/util/DecompilerTest.java

            
package com.taobao.arthas.core.util;

import java.io.File;

import org.assertj.core.api.Assertions;
import org.junit.Test;

/**
 * 
 * @author hengyunabc 2021-02-09
 *
 */
public class DecompilerTest {

    @Test
    public void test() {
        String dir = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();

        File classFile = new File(dir, this.getClass().getName().replace('.', '/') + ".class");

        String code = Decompiler.decompile(classFile.getAbsolutePath(), null, true);

        System.err.println(code);

        Assertions.assertThat(code).contains("/*23*/         System.err.println(code);").contains("/*32*/         int i = 0;");
    }

    public void aaa() {

        int jjj = 0;

        for (int i = 0; i < 100; ++i) {
            System.err.println(i);
        }
    }

}