Example usage for org.eclipse.jdt.internal.compiler.codegen CodeStream ifnonnull

List of usage examples for org.eclipse.jdt.internal.compiler.codegen CodeStream ifnonnull

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.codegen CodeStream ifnonnull.

Prototype

public void ifnonnull(BranchLabel lbl) 

Source Link

Usage

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.statemachine.transformer.RoleMigrationImplementor.java

License:Open Source License

private static void doAddMigrateMethod(TypeDeclaration roleClassDecl, char[] selector,
        TypeReference argumentTypeRef, TypeReference returnTypeRef, final String kind, final char[] cacheName) {
    AstGenerator gen = new AstGenerator(roleClassDecl.sourceStart, roleClassDecl.sourceEnd);
    MethodDeclaration migrate = new MethodDeclaration(roleClassDecl.compilationResult) {
        @Override//from   w  w w  . j a  va 2s.  c  om
        protected void endOfMethodHook(ClassFile classfile) {
            // common code for both variants:
            CodeStream codeStream = classfile.codeStream;
            // if (otherTeam == null)
            BranchLabel goOn = new BranchLabel(codeStream);
            codeStream.aload_1(); //otherTeam / otherBase
            codeStream.ifnonnull(goOn);
            // { throw new NullPointerException("Team/base argument must not be null"); }
            ReferenceBinding npeBinding = (ReferenceBinding) this.scope.getType(JAVA_LANG_NULLPOINTEREXCEPTION,
                    3);
            codeStream.new_(npeBinding);
            codeStream.dup();
            MethodBinding npeStringCtor = getStringArgCtor(npeBinding);
            if (npeStringCtor == null)
                throw new InternalCompilerError(
                        "Expected constructor NullPointerException.<init>(String) not found"); //$NON-NLS-1$
            codeStream.ldc(kind + " argument must not be null"); //$NON-NLS-1$
            codeStream.invoke(Opcodes.OPC_invokespecial, npeStringCtor, npeBinding);
            codeStream.athrow();
            goOn.place();

            // specific code:
            if (kind == TEAM)
                genMigrateToTeamInstructions(codeStream, this.scope.enclosingSourceType());
            else
                genMigrateToBaseInstructions(codeStream, this.scope.enclosingSourceType(), this.scope,
                        cacheName);
        }

        private MethodBinding getStringArgCtor(ReferenceBinding npeBinding) {
            MethodBinding[] ctors = npeBinding.getMethods(TypeConstants.INIT);
            for (MethodBinding ctor : ctors) {
                if (ctor.parameters.length == 1 && ctor.parameters[0].id == TypeIds.T_JavaLangString)
                    return ctor;
            }
            return null;
        }

        @Override
        public void analyseCode(ClassScope classScope, FlowContext flowContext, FlowInfo flowInfo) {
            // noop
        }
    };
    gen.setMethodPositions(migrate);
    migrate.isGenerated = true;

    migrate.modifiers = AccPublic | AccSynchronized;
    migrate.typeParameters = new TypeParameter[] {
            gen.unboundedTypeParameter(RoleMigrationImplementor.TYPEPARAM) };
    migrate.returnType = returnTypeRef;
    migrate.selector = selector;
    migrate.arguments = new Argument[] { gen.argument(("other" + kind).toCharArray(), argumentTypeRef) }; //$NON-NLS-1$
    migrate.statements = new Statement[0];
    migrate.hasParsedStatements = true;
    AstEdit.addMethod(roleClassDecl, migrate);
}