1/******************************************************************************* 2 * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors 3 * All rights reserved. This program and the accompanying materials 4 * are made available under the terms of the Eclipse Public License v1.0 5 * which accompanies this distribution, and is available at 6 * http://www.eclipse.org/legal/epl-v10.html 7 * 8 * Contributors: 9 * Marc R. Hoffmann - initial API and implementation 10 * 11 *******************************************************************************/ 12package org.jacoco.core.internal.instr; 13 14import org.jacoco.core.internal.data.CRC64; 15import org.jacoco.core.internal.flow.ClassProbesAdapter; 16import org.jacoco.core.runtime.IExecutionDataAccessorGenerator; 17import org.objectweb.asm.ClassReader; 18import org.objectweb.asm.Opcodes; 19 20/** 21 * Factory to find a suitable strategy to access the probe array for a given 22 * class. 23 */ 24public final class ProbeArrayStrategyFactory { 25 26 private ProbeArrayStrategyFactory() { 27 } 28 29 /** 30 * Creates a suitable strategy instance for the class described by the given 31 * reader. Created instance must be used only to process a class or 32 * interface for which it has been created and must be used only once. 33 * 34 * @param reader 35 * reader to get information about the class 36 * @param accessorGenerator 37 * accessor to the coverage runtime 38 * @return strategy instance 39 */ 40 public static IProbeArrayStrategy createFor(final ClassReader reader, 41 final IExecutionDataAccessorGenerator accessorGenerator) { 42 43 final String className = reader.getClassName(); 44 final int version = getVersion(reader); 45 final long classId = CRC64.classId(reader.b); 46 final boolean withFrames = version >= Opcodes.V1_6; 47 48 if (isInterfaceOrModule(reader)) { 49 final ProbeCounter counter = getProbeCounter(reader); 50 if (counter.getCount() == 0) { 51 return new NoneProbeArrayStrategy(); 52 } 53 if (version >= Opcodes.V1_8 && counter.hasMethods()) { 54 return new InterfaceFieldProbeArrayStrategy(className, classId, 55 counter.getCount(), accessorGenerator); 56 } else { 57 return new LocalProbeArrayStrategy(className, classId, 58 counter.getCount(), accessorGenerator); 59 } 60 } else { 61 return new ClassFieldProbeArrayStrategy(className, classId, 62 withFrames, accessorGenerator); 63 } 64 } 65 66 private static boolean isInterfaceOrModule(final ClassReader reader) { 67 return (reader.getAccess() 68 & (Opcodes.ACC_INTERFACE | Opcodes.ACC_MODULE)) != 0; 69 } 70 71 private static int getVersion(final ClassReader reader) { 72 return reader.readShort(6); 73 } 74 75 private static ProbeCounter getProbeCounter(final ClassReader reader) { 76 final ProbeCounter counter = new ProbeCounter(); 77 reader.accept(new ClassProbesAdapter(counter, false), 0); 78 return counter; 79 } 80 81} 82