Amazon S3 jar - Null Pointer Exception

Issue #175 resolved
Jörg Hagemann created an issue


A large test project using amazon s3 (aws-java-sdk-s3-1.9.8.jar) does not compile with ExtendJ. A NPE is thrown. To reproduce the NPE I assembled a little jar containing the core types involved in this issue:


public class AmazonS3ClientExample {
    public static void main(String[] args) {
        AmazonS3Client amazonS3Client = new AmazonS3Client();

Types of aws-java-sdk-s3.jar


public class AmazonS3Client {

    public AmazonS3Client() {
        this(new AWSCredentialsProviderChain(new InstanceProfileCredentialsProvider()) {
            public Object getCredentials() {
                return new Object();

    public AmazonS3Client(AWSCredentialsProviderChain credentialsProviderChain) {

public interface AWSCredentialsProvider {


public class AWSCredentialsProviderChain {
    public AWSCredentialsProviderChain(AWSCredentialsProvider... credentialsProviders) {

    public Object getCredentials() {
        return null;


public class InstanceProfileCredentialsProvider implements
        AWSCredentialsProvider {


Example call: ##

java -jar extendj.jar -cp lib/aws-java-sdk-s3.jar src/


    lastIndex evaluates to -1
    ParameterDeclaration p = (ParameterDeclaration)parameterList.getChildNoTransform(lastIndex); -> p evaluates to null
    p.getModifiersNoTransform() causes NPE


        at org.extendj.ast.MethodInfo.bodyDecl(
        at org.extendj.ast.BytecodeParser.parseMethods(
        at org.extendj.ast.BytecodeParser.parse(
        at org.extendj.ast.BytecodeParser.parse(
        at org.extendj.ast.Attributes$TypeAttributes.innerClasses(
        at org.extendj.ast.Attributes$TypeAttributes.processAttribute(
        at org.extendj.ast.Attributes.attributes(
        at org.extendj.ast.Attributes$TypeAttributes.<init>(
        at org.extendj.ast.BytecodeParser.parse(
        at org.extendj.ast.Program$
        at org.extendj.ast.BytecodeClassSource.parseCompilationUnit(
        at org.extendj.ast.ClassPath.getCompilationUnit(
        at org.extendj.ast.Program.getCompilationUnit(
        at org.extendj.ast.Program.getLibCompilationUnit(
        at org.extendj.ast.Program.lookupLibraryType(
        at org.extendj.ast.Program.lookupType_compute(
        at org.extendj.ast.Program.lookupType(
        at org.extendj.ast.Program.Define_lookupType(
        at org.extendj.ast.ASTNode.Define_lookupType(
        at org.extendj.ast.Expr.lookupType(
        at org.extendj.ast.TypeAccess.decls(
        at org.extendj.ast.TypeAccess.refined_TypeScopePropagation_TypeAccess_decl(
        at org.extendj.ast.TypeAccess.decl(
        at org.extendj.ast.TypeAccess.collect_contributors_CompilationUnit_problems(
        at org.extendj.ast.ASTNode.collect_contributors_CompilationUnit_problems(
        at org.extendj.ast.SingleTypeImportDecl.collect_contributors_CompilationUnit_problems(
        at org.extendj.ast.ASTNode.collect_contributors_CompilationUnit_problems(
        at org.extendj.ast.ASTNode.collect_contributors_CompilationUnit_problems(
        at org.extendj.ast.CompilationUnit.collect_contributors_CompilationUnit_problems(
        at org.extendj.ast.CompilationUnit.survey_CompilationUnit_problems(
        at org.extendj.ast.CompilationUnit.problems_compute(
        at org.extendj.ast.CompilationUnit.problems(
        at org.extendj.ast.CompilationUnit.errors(
        at org.extendj.ast.Frontend.processCompilationUnit(
        at org.extendj.JavaCompiler.processCompilationUnit(
        at org.extendj.JavaCompiler.main(
        at org.jastadd.extendj.JavaCompiler.main(

Comments (7)

  1. Jesper Öqvist

    This error is caused by the bytecode parser assuming that a constructor of an inner class always has the enclosing class as the first parameter. The bytecode parser then skips the first parameter and builds a parameter list out of the remaining parameters. However, if the constructor is declared ACC_VARARGS, then the bytecode parser will access the last parameter to check the type of the variable arity argument, which in this case leads to a null pointer returned from parameterList.getChild() when it tries to access one-before-the-end of an empty list.

    To fix this the condition determining when to skip the first parameter of a bytecode method needs to be fixed to remove the false positive.

    Here is the bytecode for the constructor causing the error:$1(;
        descriptor: ([Lcom/amazonaws/services/s3/AWSCredentialsProvider;)V
        flags: ACC_VARARGS
          stack=2, locals=2, args_size=2
             0: aload_0
             1: aload_1
             2: invokespecial #1                  // Method com/amazonaws/services/s3/AWSCredentialsProviderChain."<init>":([Lcom/amazonaws/services/s3/AWSCredentialsProvider;)V
             5: return
            line 6: 0
            Start  Length  Slot  Name   Signature
                0       6     0  this   Lcom/amazonaws/services/s3/AmazonS3Client$1;
                0       6     1    x0   [Lcom/amazonaws/services/s3/AWSCredentialsProvider;
  2. Jesper Öqvist

    It is interesting that creating the anonymous instance inside the explicit constructor call (this();) causes the anonymous class to become static, while if you duplicate the same code in a non-static context it will be generated as a non-static anonymous class. I believe ExtendJ has the same behaviour. Seems a little wasteful though since the anonymous class never accesses enclosing scope.

  3. Jesper Öqvist

    Since this is only affecting anonymous classes, which are not accessible from source, it would be okay to keep stripping the first parameter and just do a lastIndex != -1 check.

    ExtendJ could also safely ignore parsing anonymous classes.

  4. Jesper Öqvist

    A more minimal test case for this issue:

    In the Jar:

    package pkg;
    public class Enclosing {
      public Enclosing() {
        this(new VarArgsConstructor() { }); // Static anonymous class.
      Enclosing(Object o) {
    class VarArgsConstructor {
      VarArgsConstructor(String... args) {

    Source code:

    import pkg.Enclosing;
    class Test extends Enclosing {
  5. Jesper Öqvist

    Fix NPE in BC parse: static anon class w/ varargs

    Fix an NPE when parsing bytecode for a static anonymous class with a varargs constructor with only one parameter (variable arity parameter).

    In the future we should avoid parsing bytecode for anonymous classes entirely.

    fixes #175 (bitbucket)

    → <<cset 117904ba777e>>

  6. Log in to comment