< prev index next >

test/testlibrary/jittester/src/jdk/test/lib/jittester/factories/TryCatchBlockFactory.java

Print this page




   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package jdk.test.lib.jittester.factories;
  25 
  26 import java.util.ArrayList;
  27 import java.util.List;


  28 import jdk.test.lib.jittester.CatchBlock;
  29 import jdk.test.lib.jittester.IRNode;
  30 import jdk.test.lib.jittester.ProductionFailedException;
  31 import jdk.test.lib.jittester.TryCatchBlock;
  32 import jdk.test.lib.jittester.Type;
  33 import jdk.test.lib.jittester.TypeList;
  34 import jdk.test.lib.jittester.utils.TypeUtil;
  35 import jdk.test.lib.jittester.types.TypeKlass;
  36 import jdk.test.lib.jittester.utils.PseudoRandom;
  37 
  38 class TryCatchBlockFactory extends Factory {
  39     private final static double CATCH_SELECTION_COEF = 0.1d;
  40     private final Type returnType;
  41     private final long complexityLimit;
  42     private final int statementLimit, operatorLimit;
  43     private final boolean subBlock;
  44     private final boolean canHaveBreaks;
  45     private final boolean canHaveContinues;
  46     private final boolean canHaveReturn;
  47     private final int level;
  48     private final TypeKlass ownerClass;
  49 
  50     TryCatchBlockFactory(TypeKlass ownerClass, Type returnType,
  51             long complexityLimit, int statementLimit, int operatorLimit,
  52             int level, boolean subBlock, boolean canHaveBreaks,
  53             boolean canHaveContinues, boolean canHaveReturn) {
  54         this.ownerClass = ownerClass;
  55         this.returnType = returnType;
  56         this.complexityLimit = complexityLimit;
  57         this.statementLimit = statementLimit;
  58         this.operatorLimit = operatorLimit;
  59         this.level = level;
  60         this.subBlock = subBlock;
  61         this.canHaveBreaks = canHaveBreaks;
  62         this.canHaveContinues = canHaveContinues;
  63         this.canHaveReturn = canHaveReturn;
  64     }
  65 
  66     @Override
  67     public IRNode produce() throws ProductionFailedException {
  68         if (complexityLimit < 1 || statementLimit < 1) {
  69             throw new ProductionFailedException();
  70         }
  71         List<Type> uncheckedThrowables = getUncheckedThrowables();
  72         IRNodeBuilder builder = new IRNodeBuilder().setOwnerKlass(ownerClass)
  73                 .setResultType(returnType)
  74                 .setOperatorLimit(operatorLimit)
  75                 .setLevel(level)
  76                 .setSubBlock(subBlock)
  77                 .setCanHaveReturn(canHaveReturn)
  78                 .setCanHaveContinues(canHaveContinues)
  79                 .setCanHaveBreaks(canHaveBreaks);
  80         IRNode body = getBlock(builder, 0.6);
  81         int catchBlocksCount = (int) (CATCH_SELECTION_COEF
  82                 * PseudoRandom.random() * uncheckedThrowables.size());
  83         List<CatchBlock> catchBlocks = new ArrayList<>();
  84         List<Type> caught = new ArrayList<>();
  85         for (int i = 0; i < catchBlocksCount; i++) {
  86             List<Type> whatToCatch = new ArrayList<>();
  87             int throwableLimit = 1 + (int) ((1/(2*CATCH_SELECTION_COEF))
  88                     * PseudoRandom.random());
  89             for (int j = 0; j < throwableLimit; j++) {
  90                 whatToCatch.add(selectUniqueThrowable(uncheckedThrowables, caught));
  91             }
  92             catchBlocks.add(new CatchBlock(getBlock(builder, 0.3/catchBlocksCount),
  93                     whatToCatch, level));
  94         }
  95         IRNode finallyBody = PseudoRandom.randomBoolean() || catchBlocksCount == 0 ? getBlock(builder, 0.1) : null;
  96         return new TryCatchBlock(body, finallyBody, catchBlocks, level);
  97     }
  98 
  99     private Type selectUniqueThrowable(List<Type> variants, List<Type> caught) {
 100         Type selected;
 101         do {
 102             int randomIndex = PseudoRandom.randomNotZero(variants.size()) - 1;
 103             selected = variants.get(randomIndex);
 104         } while (caught.contains(selected));
 105         caught.add(selected);
 106         return selected;
 107     }
 108 
 109     private IRNode getBlock(IRNodeBuilder builder, double weight)
 110             throws ProductionFailedException {
 111         long actualComplexityLim = (long) (weight * PseudoRandom.random()
 112                 * complexityLimit);
 113         int actualStatementLim = (int) (weight * PseudoRandom.random()
 114                 * statementLimit);
 115         return builder.setStatementLimit(actualStatementLim)
 116                 .setComplexityLimit(actualComplexityLim)
 117                 .getBlockFactory()
 118                 .produce();
 119     }
 120 
 121     private List<Type> getUncheckedThrowables() {
 122         List<Type> result = new ArrayList<>();
 123         result.addAll(TypeUtil.getImplicitlyCastable(TypeList.getAll(),
 124                 new TypeKlass("java.lang.Error")));
 125         result.addAll(TypeUtil.getImplicitlyCastable(TypeList.getAll(),
 126                 new TypeKlass("java.lang.RuntimeException")));
 127         return result;
 128     }
 129 }


   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package jdk.test.lib.jittester.factories;
  25 
  26 import java.util.ArrayList;
  27 import java.util.List;
  28 
  29 import jdk.test.lib.jittester.Block;
  30 import jdk.test.lib.jittester.CatchBlock;

  31 import jdk.test.lib.jittester.ProductionFailedException;
  32 import jdk.test.lib.jittester.TryCatchBlock;
  33 import jdk.test.lib.jittester.Type;
  34 import jdk.test.lib.jittester.TypeList;
  35 import jdk.test.lib.jittester.utils.TypeUtil;
  36 import jdk.test.lib.jittester.types.TypeKlass;
  37 import jdk.test.lib.jittester.utils.PseudoRandom;
  38 
  39 class TryCatchBlockFactory extends Factory<TryCatchBlock> {
  40     private final static double CATCH_SELECTION_COEF = 0.1d;
  41     private final Type returnType;
  42     private final long complexityLimit;
  43     private final int statementLimit, operatorLimit;
  44     private final boolean subBlock;
  45     private final boolean canHaveBreaks;
  46     private final boolean canHaveContinues;
  47     private final boolean canHaveReturn;
  48     private final int level;
  49     private final TypeKlass ownerClass;
  50 
  51     TryCatchBlockFactory(TypeKlass ownerClass, Type returnType,
  52             long complexityLimit, int statementLimit, int operatorLimit,
  53             int level, boolean subBlock, boolean canHaveBreaks,
  54             boolean canHaveContinues, boolean canHaveReturn) {
  55         this.ownerClass = ownerClass;
  56         this.returnType = returnType;
  57         this.complexityLimit = complexityLimit;
  58         this.statementLimit = statementLimit;
  59         this.operatorLimit = operatorLimit;
  60         this.level = level;
  61         this.subBlock = subBlock;
  62         this.canHaveBreaks = canHaveBreaks;
  63         this.canHaveContinues = canHaveContinues;
  64         this.canHaveReturn = canHaveReturn;
  65     }
  66 
  67     @Override
  68     public TryCatchBlock produce() throws ProductionFailedException {
  69         if (complexityLimit < 1 || statementLimit < 1) {
  70             throw new ProductionFailedException();
  71         }
  72         List<Type> uncheckedThrowables = getUncheckedThrowables();
  73         IRNodeBuilder builder = new IRNodeBuilder().setOwnerKlass(ownerClass)
  74                 .setResultType(returnType)
  75                 .setOperatorLimit(operatorLimit)
  76                 .setLevel(level)
  77                 .setSubBlock(subBlock)
  78                 .setCanHaveReturn(canHaveReturn)
  79                 .setCanHaveContinues(canHaveContinues)
  80                 .setCanHaveBreaks(canHaveBreaks);
  81         Block body = getBlock(builder, 0.6);
  82         int catchBlocksCount = (int) (CATCH_SELECTION_COEF
  83                 * PseudoRandom.random() * uncheckedThrowables.size());
  84         List<CatchBlock> catchBlocks = new ArrayList<>();
  85         List<Type> caught = new ArrayList<>();
  86         for (int i = 0; i < catchBlocksCount; i++) {
  87             List<Type> whatToCatch = new ArrayList<>();
  88             int throwableLimit = 1 + (int) ((1/(2*CATCH_SELECTION_COEF))
  89                     * PseudoRandom.random());
  90             for (int j = 0; j < throwableLimit; j++) {
  91                 whatToCatch.add(selectUniqueThrowable(uncheckedThrowables, caught));
  92             }
  93             catchBlocks.add(new CatchBlock(getBlock(builder, 0.3/catchBlocksCount),
  94                     whatToCatch, level));
  95         }
  96         Block finallyBody = PseudoRandom.randomBoolean() || catchBlocksCount == 0 ? getBlock(builder, 0.1) : null;
  97         return new TryCatchBlock(body, finallyBody, catchBlocks, level);
  98     }
  99 
 100     private Type selectUniqueThrowable(List<Type> variants, List<Type> caught) {
 101         Type selected;
 102         do {
 103             int randomIndex = PseudoRandom.randomNotZero(variants.size()) - 1;
 104             selected = variants.get(randomIndex);
 105         } while (caught.contains(selected));
 106         caught.add(selected);
 107         return selected;
 108     }
 109 
 110     private Block getBlock(IRNodeBuilder builder, double weight)
 111             throws ProductionFailedException {
 112         long actualComplexityLim = (long) (weight * PseudoRandom.random()
 113                 * complexityLimit);
 114         int actualStatementLim = (int) (weight * PseudoRandom.random()
 115                 * statementLimit);
 116         return builder.setStatementLimit(actualStatementLim)
 117                 .setComplexityLimit(actualComplexityLim)
 118                 .getBlockFactory()
 119                 .produce();
 120     }
 121 
 122     private List<Type> getUncheckedThrowables() {
 123         List<Type> result = new ArrayList<>();
 124         result.addAll(TypeUtil.getImplicitlyCastable(TypeList.getAll(),
 125                 new TypeKlass("java.lang.Error")));
 126         result.addAll(TypeUtil.getImplicitlyCastable(TypeList.getAll(),
 127                 new TypeKlass("java.lang.RuntimeException")));
 128         return result;
 129     }
 130 }
< prev index next >