summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGo MAEDA <maeda@farend.jp>2024-12-19 01:16:10 +0000
committerGo MAEDA <maeda@farend.jp>2024-12-19 01:16:10 +0000
commitabd46c60dce4bfb1598232be67a4f2b6f62ec649 (patch)
tree2ee730116e3da3d42aa8f57afd392b90bed269b4
parent7a9374047dc7630274645c99b1943ace591daf08 (diff)
downloadredmine-abd46c60dce4bfb1598232be67a4f2b6f62ec649.tar.gz
redmine-abd46c60dce4bfb1598232be67a4f2b6f62ec649.zip
Merged r23408 from trunk to 6.0-stable (#42013).
git-svn-id: https://svn.redmine.org/redmine/branches/6.0-stable@23409 e93f8b46-1217-0410-a6f0-8f06a7374b81
-rw-r--r--Gemfile2
1 files changed, 1 insertions, 1 deletions
diff --git a/Gemfile b/Gemfile
index ff5254406..732333daf 100644
--- a/Gemfile
+++ b/Gemfile
@@ -69,7 +69,7 @@ if File.exist?(database_file)
if adapters.any?
adapters.each do |adapter|
case adapter.strip
- when 'mysql2'
+ when /mysql2/
gem 'mysql2', '~> 0.5.0'
gem "with_advisory_lock"
when /postgresql/
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
/*
 * Javassist, a Java-bytecode translator toolkit.
 * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License.  Alternatively, the contents of this file may be used under
 * the terms of the GNU Lesser General Public License Version 2.1 or later,
 * or the Apache License Version 2.0.
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 */

package javassist.compiler;

import java.util.ArrayList;
import java.util.List;

import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtField;
import javassist.CtMethod;
import javassist.Modifier;
import javassist.NotFoundException;
import javassist.bytecode.AccessFlag;
import javassist.bytecode.Bytecode;
import javassist.bytecode.ClassFile;
import javassist.bytecode.ConstPool;
import javassist.bytecode.Descriptor;
import javassist.bytecode.FieldInfo;
import javassist.bytecode.MethodInfo;
import javassist.bytecode.Opcode;
import javassist.compiler.ast.ASTList;
import javassist.compiler.ast.ASTree;
import javassist.compiler.ast.ArrayInit;
import javassist.compiler.ast.CallExpr;
import javassist.compiler.ast.Declarator;
import javassist.compiler.ast.Expr;
import javassist.compiler.ast.Keyword;
import javassist.compiler.ast.Member;
import javassist.compiler.ast.MethodDecl;
import javassist.compiler.ast.NewExpr;
import javassist.compiler.ast.Pair;
import javassist.compiler.ast.Stmnt;
import javassist.compiler.ast.Symbol;

/* Code generator methods depending on javassist.* classes.
 */
public class MemberCodeGen extends CodeGen {
    protected MemberResolver resolver;
    protected CtClass   thisClass;
    protected MethodInfo thisMethod;

    protected boolean resultStatic;

    public MemberCodeGen(Bytecode b, CtClass cc, ClassPool cp) {
        super(b);
        resolver = new MemberResolver(cp);
        thisClass = cc;
        thisMethod = null;
    }

    /**
     * Returns the major version of the class file
     * targeted by this compilation.
     */
    public int getMajorVersion() {
        ClassFile cf = thisClass.getClassFile2();
        if (cf == null)
            return ClassFile.MAJOR_VERSION;     // JDK 1.3
        return cf.getMajorVersion();
    }

    /**
     * Records the currently compiled method.
     */
    public void setThisMethod(CtMethod m) {
        thisMethod = m.getMethodInfo2();
        if (typeChecker != null)
            typeChecker.setThisMethod(thisMethod);
    }

    public CtClass getThisClass() { return thisClass; }

    /**
     * Returns the JVM-internal representation of this class name.
     */
    @Override
    protected String getThisName() {
        return MemberResolver.javaToJvmName(thisClass.getName());
    }

    /**
     * Returns the JVM-internal representation of this super class name.
     */
    @Override
    protected String getSuperName() throws CompileError {
        return MemberResolver.javaToJvmName(
                        MemberResolver.getSuperclass(thisClass).getName());
    }

    @Override
    protected void insertDefaultSuperCall() throws CompileError {
        bytecode.addAload(0);
        bytecode.addInvokespecial(MemberResolver.getSuperclass(thisClass),
                                  "<init>", "()V");
    }

    static class JsrHook extends ReturnHook {
        List<int[]> jsrList;
        CodeGen cgen;
        int var;

        JsrHook(CodeGen gen) {
            super(gen);
            jsrList = new ArrayList<int[]>();
            cgen = gen;
            var = -1;
        }

        private int getVar(int size) {
            if (var < 0) {
                var = cgen.getMaxLocals();
                cgen.incMaxLocals(size);
            }

            return var;
        }

        private void jsrJmp(Bytecode b) {
            b.addOpcode(Opcode.GOTO);
            jsrList.add(new int[] {b.currentPc(), var});
            b.addIndex(0);
        }

        @Override
        protected boolean doit(Bytecode b, int opcode) {
            switch (opcode) {
            case Opcode.RETURN :
                jsrJmp(b);
                break;
            case ARETURN :
                b.addAstore(getVar(1));
                jsrJmp(b);
                b.addAload(var);
                break;
            case IRETURN :
                b.addIstore(getVar(1));
                jsrJmp(b);
                b.addIload(var);
                break;
            case LRETURN :
                b.addLstore(getVar(2));
                jsrJmp(b);
                b.addLload(var);
                break;
            case DRETURN :
                b.addDstore(getVar(2));
                jsrJmp(b);
                b.addDload(var);
                break;
            case FRETURN :
                b.addFstore(getVar(1));
                jsrJmp(b);
                b.addFload(var);
                break;
            default :
                throw new RuntimeException("fatal");
            }

            return false;
        }
    }

    static class JsrHook2 extends ReturnHook {
        int var;
        int target;

        JsrHook2(CodeGen gen, int[] retTarget) {
            super(gen);
            target = retTarget[0];
            var = retTarget[1];
        }

        @Override
        protected boolean doit(Bytecode b, int opcode) {
            switch (opcode) {
            case Opcode.RETURN :
                break;
            case ARETURN :
                b.addAstore(var);
                break;
            case IRETURN :
                b.addIstore(var);
                break;
            case LRETURN :
                b.addLstore(var);
                break;
            case DRETURN :
                b.addDstore(var);
                break;
            case FRETURN :
                b.addFstore(var);
                break;
            default :
                throw new RuntimeException("fatal");
            }

            b.addOpcode(Opcode.GOTO);
            b.addIndex(target - b.currentPc() + 3);
            return true;
        }
    }

    @Override
    protected void atTryStmnt(Stmnt st) throws CompileError {
        Bytecode bc = bytecode;
        Stmnt body = (Stmnt)st.getLeft();
        if (body == null)
            return;

        ASTList catchList = (ASTList)st.getRight().getLeft();
        Stmnt finallyBlock = (Stmnt)st.getRight().getRight().getLeft();
        List<Integer> gotoList = new ArrayList<Integer>();

        JsrHook jsrHook = null;
        if (finallyBlock != null)
            jsrHook = new JsrHook(this);

        int start = bc.currentPc();
        body.accept(this);
        int end = bc.currentPc();
        if (start == end)
            throw new CompileError("empty try block", st.getLineNumber());

        boolean tryNotReturn = !hasReturned;
        if (tryNotReturn) {
            bc.addOpcode(Opcode.GOTO);
            gotoList.add(bc.currentPc());
            bc.addIndex(0);   // correct later
        }

        int var = getMaxLocals();
        incMaxLocals(1);
        while (catchList != null) {
            // catch clause
            Pair p = (Pair)catchList.head();
            catchList = catchList.tail();
            Declarator decl = (Declarator)p.getLeft();
            Stmnt block = (Stmnt)p.getRight();

            decl.setLocalVar(var);

            CtClass type = resolver.lookupClassByJvmName(decl.getClassName(), st.getLineNumber());
            decl.setClassName(MemberResolver.javaToJvmName(type.getName()));
            bc.addExceptionHandler(start, end, bc.currentPc(), type);
            bc.growStack(1);
            bc.addAstore(var);
            hasReturned = false;
            if (block != null)
                block.accept(this);

            if (!hasReturned) {
                bc.addOpcode(Opcode.GOTO);
                gotoList.add(bc.currentPc());
                bc.addIndex(0);   // correct later
                tryNotReturn = true;
            }
        }

        if (finallyBlock != null) {
            jsrHook.remove(this);
            // catch (any) clause
            int pcAnyCatch = bc.currentPc();
            bc.addExceptionHandler(start, pcAnyCatch, pcAnyCatch, 0);
            bc.growStack(1);
            bc.addAstore(var);
            hasReturned = false;
            finallyBlock.accept(this);
            if (!hasReturned) {
                bc.addAload(var);
                bc.addOpcode(ATHROW);
            }

            addFinally(jsrHook.jsrList, finallyBlock);
        }

        int pcEnd = bc.currentPc();
        patchGoto(gotoList, pcEnd);
        hasReturned = !tryNotReturn;
        if (finallyBlock != null) {
            if (tryNotReturn)
                finallyBlock.accept(this);
        }
    }

    /**
     * Adds a finally clause for earch return statement.
     */
    private void addFinally(List<int[]> returnList, Stmnt finallyBlock)
        throws CompileError
    {
        Bytecode bc = bytecode;
        for (final int[] ret:returnList) {
            int pc = ret[0];
            bc.write16bit(pc, bc.currentPc() - pc + 1);
            ReturnHook hook = new JsrHook2(this, ret);
            finallyBlock.accept(this);
            hook.remove(this);
            if (!hasReturned) {
                bc.addOpcode(Opcode.GOTO);
                bc.addIndex(pc + 3 - bc.currentPc());
            }
        }
    }

    @Override
    public void atNewExpr(NewExpr expr) throws CompileError {
        if (expr.isArray())
            atNewArrayExpr(expr);
        else {
            CtClass clazz = resolver.lookupClassByName(expr.getClassName());
            String cname = clazz.getName();
            ASTList args = expr.getArguments();
            bytecode.addNew(cname);
            bytecode.addOpcode(DUP);

            atMethodCallCore(clazz, MethodInfo.nameInit, args,
                             false, true, -1, null);

            exprType = CLASS;
            arrayDim = 0;
            className = MemberResolver.javaToJvmName(cname);
        }
    }

    public void atNewArrayExpr(NewExpr expr) throws CompileError {
        int type = expr.getArrayType();
        ASTList size = expr.getArraySize();
        ASTList classname = expr.getClassName();
        ArrayInit init = expr.getInitializer();
        if (size.length() > 1) {
            if (init != null)
                throw new CompileError(
                        "sorry, multi-dimensional array initializer " +
                        "for new is not supported", expr.getLineNumber());

            atMultiNewArray(type, classname, size);
            return;
        }

        ASTree sizeExpr = size.head();
        atNewArrayExpr2(type, sizeExpr, Declarator.astToClassName(classname, '/'), init, expr.getLineNumber());
    }

    private void atNewArrayExpr2(int type, ASTree sizeExpr,
                        String jvmClassname, ArrayInit init, int lineNumber) throws CompileError {
        if (init == null)
            if (sizeExpr == null)
                throw new CompileError("no array size", lineNumber);
            else
                sizeExpr.accept(this);
        else
            if (sizeExpr == null) {
                int s = init.size();
                bytecode.addIconst(s);
            }
            else
                throw new CompileError("unnecessary array size specified for new", lineNumber);

        String elementClass;
        if (type == CLASS) {
            elementClass = resolveClassName(jvmClassname, lineNumber);
            bytecode.addAnewarray(MemberResolver.jvmToJavaName(elementClass));
        }
        else {
            elementClass = null;
            int atype = 0;
            switch (type) {
            case BOOLEAN :
                atype = T_BOOLEAN;
                break;
            case CHAR :
                atype = T_CHAR;
                break;
            case FLOAT :
                atype = T_FLOAT;
                break;
            case DOUBLE :
                atype = T_DOUBLE;
                break;
            case BYTE :
                atype = T_BYTE;
                break;
            case SHORT :
                atype = T_SHORT;
                break;
            case INT :
                atype = T_INT;
                break;
            case LONG :
                atype = T_LONG;
                break;
            default :
                badNewExpr(lineNumber);
                break;
            }

            bytecode.addOpcode(NEWARRAY);
            bytecode.add(atype);
        }

        if (init != null) {
            int s = init.size();
            ASTList list = init;
            for (int i = 0; i < s; i++) {
                bytecode.addOpcode(DUP);
                bytecode.addIconst(i);
                list.head().accept(this);
                if (!isRefType(type))
                    atNumCastExpr(exprType, type);

                bytecode.addOpcode(getArrayWriteOp(type, 0));
                list = list.tail();
            }
        }

        exprType = type;
        arrayDim = 1;
        className = elementClass;
    }

    private static void badNewExpr(int lineNumber) throws CompileError {
        throw new CompileError("bad new expression", lineNumber);
    }

    @Override
    protected void atArrayVariableAssign(ArrayInit init, int varType,
                                         int varArray, String varClass) throws CompileError {
        atNewArrayExpr2(varType, null, varClass, init, init.getLineNumber());
    }

    @Override
    public void atArrayInit(ArrayInit init) throws CompileError {
        throw new CompileError("array initializer is not supported", init.getLineNumber());
    }

    protected void atMultiNewArray(int type, ASTList classname, ASTList size)
        throws CompileError
    {
        int count, dim;
        dim = size.length();
        for (count = 0; size != null; size = size.tail()) {
            ASTree s = size.head();
            if (s == null)
                break;          // int[][][] a = new int[3][4][];

            ++count;
            s.accept(this);
            if (exprType != INT)
                throw new CompileError("bad type for array size", classname.getLineNumber());
        }

        String desc;
        exprType = type;
        arrayDim = dim;
        if (type == CLASS) {
            className = resolveClassName(classname);
            desc = toJvmArrayName(className, dim);
        }
        else
            desc = toJvmTypeName(type, dim);

        bytecode.addMultiNewarray(desc, count);
    }

    @Override
    public void atCallExpr(CallExpr expr) throws CompileError {
        String mname = null;
        CtClass targetClass = null;
        ASTree method = expr.oprand1();
        ASTList args = (ASTList)expr.oprand2();
        boolean isStatic = false;
        boolean isSpecial = false;
        int aload0pos = -1;

        MemberResolver.Method cached = expr.getMethod();
        if (method instanceof Member) {
            mname = ((Member)method).get();
            targetClass = thisClass;
            if (inStaticMethod || (cached != null && cached.isStatic()))
                isStatic = true;            // should be static
            else {
                aload0pos = bytecode.currentPc();
                bytecode.addAload(0);       // this
            }
        }
        else if (method instanceof Keyword) {   // constructor
            isSpecial = true;
            mname = MethodInfo.nameInit;        // <init>
            targetClass = thisClass;
            if (inStaticMethod)
                throw new CompileError("a constructor cannot be static", expr.getLineNumber());
            bytecode.addAload(0);   // this

            if (((Keyword)method).get() == SUPER)
                targetClass = MemberResolver.getSuperclass(targetClass);
        }
        else if (method instanceof Expr) {
            Expr e = (Expr)method;
            mname = ((Symbol)e.oprand2()).get();
            int op = e.getOperator();
            if (op == MEMBER) {                 // static method
                targetClass
                    = resolver.lookupClass(((Symbol)e.oprand1()).get(), false, expr.getLineNumber());
                isStatic = true;
            }
            else if (op == '.') {
                ASTree target = e.oprand1();
                String classFollowedByDotSuper = TypeChecker.isDotSuper(target);
                if (classFollowedByDotSuper != null) {
                    isSpecial = true;
                    targetClass = MemberResolver.getSuperInterface(thisClass,
                                                        classFollowedByDotSuper);
                    if (inStaticMethod || (cached != null && cached.isStatic()))
                        isStatic = true;            // should be static
                    else {
                        aload0pos = bytecode.currentPc();
                        bytecode.addAload(0);       // this
                    }
                }
                else {
                    if (target instanceof Keyword)
                        if (((Keyword)target).get() == SUPER)
                            isSpecial = true;

                    try {
                        target.accept(this);
                    }
                    catch (NoFieldException nfe) {
                        if (nfe.getExpr() != target)
                            throw nfe;

                        // it should be a static method.
                        exprType = CLASS;
                        arrayDim = 0;
                        className = nfe.getField(); // JVM-internal
                        isStatic = true;
                    }

                    if (arrayDim > 0)
                        targetClass = resolver.lookupClass(javaLangObject, true, expr.getLineNumber());
                    else if (exprType == CLASS /* && arrayDim == 0 */)
                        targetClass = resolver.lookupClassByJvmName(className, expr.getLineNumber());
                    else
                        badMethod(e.getLineNumber());
                }
            }
            else
                badMethod(expr.getLineNumber());
        }
        else
            fatal(expr.getLineNumber());

        atMethodCallCore(targetClass, mname, args, isStatic, isSpecial,
                         aload0pos, cached);
    }

    private static void badMethod(int lineNumber) throws CompileError {
        throw new CompileError("bad method", lineNumber);
    }

    /*
     * atMethodCallCore() is also called by doit() in NewExpr.ProceedForNew
     *
     * @param targetClass       the class at which method lookup starts.
     * @param found         not null if the method look has been already done.
     */
    public void atMethodCallCore(CtClass targetClass, String mname,
                        ASTList args, boolean isStatic, boolean isSpecial,
                        int aload0pos, MemberResolver.Method found)
        throws CompileError
    {
        int nargs = getMethodArgsLength(args);
        int[] types = new int[nargs];
        int[] dims = new int[nargs];
        String[] cnames = new String[nargs];

        if (!isStatic && found != null && found.isStatic()) {
            bytecode.addOpcode(POP);
            isStatic = true;
        }

        @SuppressWarnings("unused")
        int stack = bytecode.getStackDepth();

        // generate code for evaluating arguments.
        atMethodArgs(args, types, dims, cnames);

        if (found == null)
            found = resolver.lookupMethod(targetClass, thisClass, thisMethod,
                                          mname, types, dims, cnames);

        if (found == null) {
            String msg;
            if (mname.equals(MethodInfo.nameInit))
                msg = "constructor not found";
            else
                msg = "Method " + mname + " not found in "
                    + targetClass.getName();

            throw new CompileError(msg, args.getLineNumber());
        }

        atMethodCallCore2(targetClass, mname, isStatic, isSpecial,
                          aload0pos, found);
    }

    private boolean isFromSameDeclaringClass(CtClass outer, CtClass inner) {
        try {
            while (outer != null) {
                if (isEnclosing(outer, inner))
                    return true;
                outer = outer.getDeclaringClass();
            }
        }
        catch (NotFoundException e) {}
        return false;
    }

    private void atMethodCallCore2(CtClass targetClass, String mname,
                                   boolean isStatic, boolean isSpecial,
                                   int aload0pos,
                                   MemberResolver.Method found)
        throws CompileError
    {
        CtClass declClass = found.declaring;
        MethodInfo minfo = found.info;
        String desc = minfo.getDescriptor();
        int acc = minfo.getAccessFlags();

        if (mname.equals(MethodInfo.nameInit)) {
            isSpecial = true;
            if (declClass != targetClass)
                throw new CompileError("no such constructor: " + targetClass.getName(), targetClass.getLinesCount() - 1);

            if (declClass != thisClass && AccessFlag.isPrivate(acc)) {
                if (declClass.getClassFile().getMajorVersion() < ClassFile.JAVA_8
                        || !isFromSameDeclaringClass(declClass, thisClass)) {
                    desc = getAccessibleConstructor(desc, declClass, minfo);
                    bytecode.addOpcode(Opcode.ACONST_NULL); // the last parameter
                }
            }
        }
        else if (AccessFlag.isPrivate(acc))
            if (declClass == thisClass)
                isSpecial = true;
            else {
                isSpecial = false;
                isStatic = true;
                String origDesc = desc;
                if ((acc & AccessFlag.STATIC) == 0)
                    desc = Descriptor.insertParameter(declClass.getName(),
                                                      origDesc);

                acc = AccessFlag.setPackage(acc) | AccessFlag.STATIC;
                mname = getAccessiblePrivate(mname, origDesc, desc,
                                             minfo, declClass);
            }

        boolean popTarget = false;
        if ((acc & AccessFlag.STATIC) != 0) {
            if (!isStatic) {
                /* this method is static but the target object is
                   on stack.  It must be popped out.  If aload0pos >= 0,
                   then the target object was pushed by aload_0.  It is
                   overwritten by NOP.
                */
                isStatic = true;
                if (aload0pos >= 0)
                    bytecode.write(aload0pos, NOP);
                else
                    popTarget = true;
            }

            bytecode.addInvokestatic(declClass, mname, desc);
        }
        else if (isSpecial)    // if (isSpecial && notStatic(acc))
            bytecode.addInvokespecial(targetClass, mname, desc);
        else {
            if (!Modifier.isPublic(declClass.getModifiers())
                || declClass.isInterface() != targetClass.isInterface())
                declClass = targetClass;

            if (declClass.isInterface()) {
                int nargs = Descriptor.paramSize(desc) + 1;
                bytecode.addInvokeinterface(declClass, mname, desc, nargs);
            }
            else
                if (isStatic)
                    throw new CompileError(mname + " is not static", targetClass.getLinesCount() - 1);
                else
                    bytecode.addInvokevirtual(declClass, mname, desc);
        }

        setReturnType(desc, isStatic, popTarget, targetClass.getLinesCount() - 1);
    }

    /*
     * Finds (or adds if necessary) a hidden accessor if the method
     * is in an enclosing class.
     *
     * @param desc          the descriptor of the method.
     * @param declClass     the class declaring the method.
     */
    protected String getAccessiblePrivate(String methodName, String desc,
                                          String newDesc, MethodInfo minfo,
                                          CtClass declClass)
        throws CompileError
    {
        if (isEnclosing(declClass, thisClass)) {
            AccessorMaker maker = declClass.getAccessorMaker();
            if (maker != null)
                return maker.getMethodAccessor(methodName, desc, newDesc,
                                               minfo);
        }

        throw new CompileError("Method " + methodName
                               + " is private", declClass.getLinesCount() - 1);
    }

    /*
     * Finds (or adds if necessary) a hidden constructor if the given
     * constructor is in an enclosing class.
     *
     * @param desc          the descriptor of the constructor.
     * @param declClass     the class declaring the constructor.
     * @param minfo         the method info of the constructor.
     * @return the descriptor of the hidden constructor.
     */
    protected String getAccessibleConstructor(String desc, CtClass declClass,
                                              MethodInfo minfo)
        throws CompileError
    {
        if (isEnclosing(declClass, thisClass)) {
            AccessorMaker maker = declClass.getAccessorMaker();
            if (maker != null)
                return maker.getConstructor(declClass, desc, minfo);
        }

        throw new CompileError("the called constructor is private in "
                               + declClass.getName(), declClass.getLinesCount() - 1);
    }

    private boolean isEnclosing(CtClass outer, CtClass inner) {
        try {
            while (inner != null) {
                inner = inner.getDeclaringClass();
                if (inner == outer)
                    return true;
            }
        }
        catch (NotFoundException e) {}
        return false;   
    }

    public int getMethodArgsLength(ASTList args) {
        return ASTList.length(args);
    }

    public void atMethodArgs(ASTList args, int[] types, int[] dims,
                             String[] cnames) throws CompileError {
        int i = 0;
        while (args != null) {
            ASTree a = args.head();
            a.accept(this);
            types[i] = exprType;
            dims[i] = arrayDim;
            cnames[i] = className;
            ++i;
            args = args.tail();
        }
    }

    void setReturnType(String desc, boolean isStatic, boolean popTarget, int lineNumber)
        throws CompileError
    {
        int i = desc.indexOf(')');
        if (i < 0)
            badMethod(lineNumber);

        char c = desc.charAt(++i);
        int dim = 0;
        while (c == '[') {
            ++dim;
            c = desc.charAt(++i);
        }

        arrayDim = dim;
        if (c == 'L') {
            int j = desc.indexOf(';', i + 1);
            if (j < 0)
                badMethod(lineNumber);

            exprType = CLASS;
            className = desc.substring(i + 1, j);
        }
        else {
            exprType = MemberResolver.descToType(c, lineNumber);
            className = null;
        }

        int etype = exprType;
        if (isStatic) {
            if (popTarget) {
                if (is2word(etype, dim)) {
                    bytecode.addOpcode(DUP2_X1);
                    bytecode.addOpcode(POP2);
                    bytecode.addOpcode(POP);
                }
                else if (etype == VOID)
                    bytecode.addOpcode(POP);
                else {
                    bytecode.addOpcode(SWAP);
                    bytecode.addOpcode(POP);
                }
            }
        }
    }

    @Override
    protected void atFieldAssign(Expr expr, int op, ASTree left,
                        ASTree right, boolean doDup) throws CompileError
    {
        CtField f = fieldAccess(left, false);
        boolean is_static = resultStatic;
        if (op != '=' && !is_static)
            bytecode.addOpcode(DUP);

        int fi;
        if (op == '=') {
            FieldInfo finfo = f.getFieldInfo2();
            setFieldType(finfo, expr.getLineNumber());
            AccessorMaker maker = isAccessibleField(f, finfo, expr.getLineNumber());
            if (maker == null)
                fi = addFieldrefInfo(f, finfo);
            else
                fi = 0;
        }
        else
            fi = atFieldRead(f, is_static, expr.getLineNumber());

        int fType = exprType;
        int fDim = arrayDim;
        String cname = className;

        atAssignCore(expr, op, right, fType, fDim, cname);

        boolean is2w = is2word(fType, fDim);
        if (doDup) {
            int dup_code;
            if (is_static)
                dup_code = (is2w ? DUP2 : DUP);
            else
                dup_code = (is2w ? DUP2_X1 : DUP_X1);

            bytecode.addOpcode(dup_code);
        }

        atFieldAssignCore(f, is_static, fi, is2w);

        exprType = fType;
        arrayDim = fDim;
        className = cname;
    }

    /* If fi == 0, the field must be a private field in an enclosing class.
     */
    private void atFieldAssignCore(CtField f, boolean is_static, int fi,
                                   boolean is2byte) throws CompileError {
        if (fi != 0) {
            if (is_static) {
               bytecode.add(PUTSTATIC);
               bytecode.growStack(is2byte ? -2 : -1);
            }
            else {
                bytecode.add(PUTFIELD);
                bytecode.growStack(is2byte ? -3 : -2);
            }

            bytecode.addIndex(fi);
        }
        else {
            CtClass declClass = f.getDeclaringClass();
            AccessorMaker maker = declClass.getAccessorMaker();
            // make should be non null.
            FieldInfo finfo = f.getFieldInfo2();
            MethodInfo minfo = maker.getFieldSetter(finfo, is_static);
            bytecode.addInvokestatic(declClass, minfo.getName(),
                                     minfo.getDescriptor());
        }
    }

    /* overwritten in JvstCodeGen.
     */
    @Override
    public void atMember(Member mem) throws CompileError {
        atFieldRead(mem);
    }

    @Override
    protected void atFieldRead(ASTree expr) throws CompileError
    {
        CtField f = fieldAccess(expr, true);
        if (f == null) {
            atArrayLength(expr);
            return;
        }

        boolean is_static = resultStatic;
        ASTree cexpr = TypeChecker.getConstantFieldValue(f, expr.getLineNumber());
        if (cexpr == null)
            atFieldRead(f, is_static,expr.getLineNumber() );
        else {
            cexpr.accept(this);
            setFieldType(f.getFieldInfo2(), expr.getLineNumber());
        }
    }

    private void atArrayLength(ASTree expr) throws CompileError {
        if (arrayDim == 0)
            throw new CompileError(".length applied to a non array", expr.getLineNumber());

        bytecode.addOpcode(ARRAYLENGTH);
        exprType = INT;
        arrayDim = 0;
    }

    /**
     * Generates bytecode for reading a field value.
     * It returns a fieldref_info index or zero if the field is a private
     * one declared in an enclosing class. 
     */
    private int atFieldRead(CtField f, boolean isStatic, int lineNumber) throws CompileError {
        FieldInfo finfo = f.getFieldInfo2();
        boolean is2byte = setFieldType(finfo, lineNumber);
        AccessorMaker maker = isAccessibleField(f, finfo, lineNumber);
        if (maker != null) {
            MethodInfo minfo = maker.getFieldGetter(finfo, isStatic);
            bytecode.addInvokestatic(f.getDeclaringClass(), minfo.getName(),
                                     minfo.getDescriptor());
            return 0;
        }
        int fi = addFieldrefInfo(f, finfo);
        if (isStatic) {
            bytecode.add(GETSTATIC);
            bytecode.growStack(is2byte ? 2 : 1);
        }
        else {
            bytecode.add(GETFIELD);
            bytecode.growStack(is2byte ? 1 : 0);
        }

        bytecode.addIndex(fi);
        return fi;
    }

    /**
     * Returns null if the field is accessible.  Otherwise, it throws
     * an exception or it returns AccessorMaker if the field is a private
     * one declared in an enclosing class.
     */
    private AccessorMaker isAccessibleField(CtField f, FieldInfo finfo, int lineNumber)
        throws CompileError
    {
        if (AccessFlag.isPrivate(finfo.getAccessFlags())
                && f.getDeclaringClass() != thisClass) {
            CtClass declClass = f.getDeclaringClass();
            if (isEnclosing(declClass, thisClass)) {
                AccessorMaker maker = declClass.getAccessorMaker();
                if (maker != null)
                    return maker;
            }
            throw new CompileError("Field " + f.getName() + " in "
                                   + declClass.getName() + " is private.", lineNumber);
        }

        return null;    // accessible field
    }

    /**
     * Sets exprType, arrayDim, and className.
     *
     * @return true if the field type is long or double. 
     */
    private boolean setFieldType(FieldInfo finfo, int lineNumber) throws CompileError {
        String type = finfo.getDescriptor();

        int i = 0;
        int dim = 0;
        char c = type.charAt(i);
        while (c == '[') {
            ++dim;
            c = type.charAt(++i);
        }

        arrayDim = dim;
        exprType = MemberResolver.descToType(c, lineNumber);

        if (c == 'L')
            className = type.substring(i + 1, type.indexOf(';', i + 1));
        else
            className = null;

        boolean is2byte = dim == 0 && (c == 'J' || c == 'D');
        return is2byte;
    }

    private int addFieldrefInfo(CtField f, FieldInfo finfo) {
        ConstPool cp = bytecode.getConstPool();
        String cname = f.getDeclaringClass().getName();
        int ci = cp.addClassInfo(cname);
        String name = finfo.getName();
        String type = finfo.getDescriptor();
        return cp.addFieldrefInfo(ci, name, type);
    }

    @Override
    protected void atClassObject2(String cname) throws CompileError {
        if (getMajorVersion() < ClassFile.JAVA_5)
            super.atClassObject2(cname);
        else
            bytecode.addLdc(bytecode.getConstPool().addClassInfo(cname));
    }

    @Override
    protected void atFieldPlusPlus(int token, boolean isPost,
                                   ASTree oprand, Expr expr, boolean doDup)
        throws CompileError
    {
        CtField f = fieldAccess(oprand, false);
        boolean is_static = resultStatic;
        if (!is_static)
            bytecode.addOpcode(DUP);

        int fi = atFieldRead(f, is_static, oprand.getLineNumber());
        int t = exprType;
        boolean is2w = is2word(t, arrayDim);

        int dup_code;
        if (is_static)
            dup_code = (is2w ? DUP2 : DUP);
        else
            dup_code = (is2w ? DUP2_X1 : DUP_X1);

        atPlusPlusCore(dup_code, doDup, token, isPost, expr);
        atFieldAssignCore(f, is_static, fi, is2w);
    }

    /* This method also returns a value in resultStatic.
     *
     * @param acceptLength      true if array length is acceptable
     */
    protected CtField fieldAccess(ASTree expr, boolean acceptLength)
            throws CompileError
    {
        if (expr instanceof Member) {
            String name = ((Member)expr).get();
            CtField f = null;
            try {
                f = thisClass.getField(name);
            }
            catch (NotFoundException e) {
                // EXPR might be part of a static member access?
                throw new NoFieldException(name, expr);
            }

            boolean is_static = Modifier.isStatic(f.getModifiers());
            if (!is_static)
                if (inStaticMethod)
                    throw new CompileError(
                                "not available in a static method: " + name, expr.getLineNumber());
                else
                    bytecode.addAload(0);       // this

            resultStatic = is_static;
            return f;
        }
        else if (expr instanceof Expr) {
            Expr e = (Expr)expr;
            int op = e.getOperator();
            if (op == MEMBER) {
                /* static member by # (extension by Javassist)
                 * For example, if int.class is parsed, the resulting tree
                 * is (# "java.lang.Integer" "TYPE"). 
                 */
                CtField f = resolver.lookupField(((Symbol)e.oprand1()).get(),
                                         (Symbol)e.oprand2());
                resultStatic = true;
                return f;
            }
            else if (op == '.') {
                CtField f = null;
                try {
                    e.oprand1().accept(this);
                    /* Don't call lookupFieldByJvmName2().
                     * The left operand of . is not a class name but
                     * a normal expression.
                     */
                    if (exprType == CLASS && arrayDim == 0)
                        f = resolver.lookupFieldByJvmName(className,
                                                    (Symbol)e.oprand2());
                    else if (acceptLength && arrayDim > 0
                             && ((Symbol)e.oprand2()).get().equals("length"))
                        return null;    // expr is an array length.
                    else
                        badLvalue(expr.getLineNumber());

                    boolean is_static = Modifier.isStatic(f.getModifiers());
                    if (is_static)
                        bytecode.addOpcode(POP);

                    resultStatic = is_static;
                    return f;
                }
                catch (NoFieldException nfe) {
                    if (nfe.getExpr() != e.oprand1())
                        throw nfe;

                    /* EXPR should be a static field.
                     * If EXPR might be part of a qualified class name,
                     * lookupFieldByJvmName2() throws NoFieldException.
                     */
                    Symbol fname = (Symbol)e.oprand2();
                    String cname = nfe.getField();
                    f = resolver.lookupFieldByJvmName2(cname, fname, expr);
                    resultStatic = true;
                    return f;
                }
            }
            else
                badLvalue(expr.getLineNumber());
        }
        else
            badLvalue(expr.getLineNumber());

        resultStatic = false;
        return null;    // never reach
    }

    private static void badLvalue(int lineNumber) throws CompileError {
        throw new CompileError("bad l-value", lineNumber);
    }

    public CtClass[] makeParamList(MethodDecl md) throws CompileError {
        CtClass[] params;
        ASTList plist = md.getParams();
        if (plist == null)
            params = new CtClass[0];
        else {
            int i = 0;
            params = new CtClass[plist.length()];
            while (plist != null) {
                params[i++] = resolver.lookupClass((Declarator)plist.head());
                plist = plist.tail();
            }
        }

        return params;
    }

    public CtClass[] makeThrowsList(MethodDecl md) throws CompileError {
        CtClass[] clist;
        ASTList list = md.getThrows();
        if (list == null)
            return null;
        int i = 0;
        clist = new CtClass[list.length()];
        while (list != null) {
            clist[i++] = resolver.lookupClassByName((ASTList)list.head());
            list = list.tail();
        }

        return clist;
    }

    /* Converts a class name into a JVM-internal representation.
     *
     * It may also expand a simple class name to java.lang.*.
     * For example, this converts Object into java/lang/Object.
     */
    @Override
    protected String resolveClassName(ASTList name) throws CompileError {
        return resolver.resolveClassName(name);
    }

    /* Expands a simple class name to java.lang.*.
     * For example, this converts Object into java/lang/Object.
     */
    @Override
    protected String resolveClassName(String jvmName, int lineNumber) throws CompileError {
        return resolver.resolveJvmClassName(jvmName, lineNumber);
    }
}