1 /* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 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 }