|
| 1 | +/* |
| 2 | + * Copyright Splunk Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.splunk.android.sample; |
| 18 | + |
| 19 | +import android.os.Bundle; |
| 20 | +import android.view.LayoutInflater; |
| 21 | +import android.view.View; |
| 22 | +import android.view.ViewGroup; |
| 23 | +import android.widget.Button; |
| 24 | +import androidx.annotation.NonNull; |
| 25 | +import androidx.annotation.Nullable; |
| 26 | +import androidx.fragment.app.Fragment; |
| 27 | +import java.lang.reflect.Method; |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.Map; |
| 31 | + |
| 32 | +public class CrashTestFragmentB extends Fragment { |
| 33 | + |
| 34 | + @Nullable |
| 35 | + @Override |
| 36 | + public View onCreateView( |
| 37 | + @NonNull LayoutInflater inflater, |
| 38 | + @Nullable ViewGroup container, |
| 39 | + @Nullable Bundle savedInstanceState) { |
| 40 | + return inflater.inflate(R.layout.fragment_crash_test_b, container, false); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { |
| 45 | + super.onViewCreated(view, savedInstanceState); |
| 46 | + |
| 47 | + Button inliningButton = view.findViewById(R.id.button_inlining); |
| 48 | + inliningButton.setOnClickListener(v -> triggerInliningException()); |
| 49 | + |
| 50 | + Button reflectionButton = view.findViewById(R.id.button_reflection); |
| 51 | + reflectionButton.setOnClickListener(v -> triggerReflectionException()); |
| 52 | + |
| 53 | + Button inheritanceButton = view.findViewById(R.id.button_inheritance); |
| 54 | + inheritanceButton.setOnClickListener(v -> triggerInheritanceException()); |
| 55 | + |
| 56 | + Button oomButton = view.findViewById(R.id.button_oom); |
| 57 | + oomButton.setOnClickListener(v -> triggerOutOfMemoryError()); |
| 58 | + |
| 59 | + Button overloadButton = view.findViewById(R.id.button_overload); |
| 60 | + overloadButton.setOnClickListener(v -> triggerOverloadedMethodCrash()); |
| 61 | + } |
| 62 | + |
| 63 | + private void triggerInliningException() { |
| 64 | + int a = inlinableMethod1(5); |
| 65 | + int b = inlinableMethod2(0); |
| 66 | + int result = a / b; // Division by zero |
| 67 | + } |
| 68 | + |
| 69 | + private int inlinableMethod1(int value) { |
| 70 | + return value * 5; |
| 71 | + } |
| 72 | + |
| 73 | + private int inlinableMethod2(int value) { |
| 74 | + return value; |
| 75 | + } |
| 76 | + |
| 77 | + private void triggerReflectionException() { |
| 78 | + try { |
| 79 | + Method method = String.class.getMethod("nonExistentMethod"); |
| 80 | + |
| 81 | + // won't reach here |
| 82 | + method.invoke("test"); |
| 83 | + } catch (Exception e) { |
| 84 | + throw new RuntimeException("Reflection failed", e); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private void triggerInheritanceException() { |
| 89 | + DeepestChild deepChild = new DeepestChild(); |
| 90 | + |
| 91 | + deepChild.methodThatThrows(); |
| 92 | + } |
| 93 | + |
| 94 | + private static class BaseClass { |
| 95 | + protected void baseMethod() { |
| 96 | + throw new RuntimeException("Exception in base class"); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private static class FirstChild extends BaseClass { |
| 101 | + @Override |
| 102 | + protected void baseMethod() { |
| 103 | + try { |
| 104 | + super.baseMethod(); |
| 105 | + } catch (RuntimeException e) { |
| 106 | + throw new IllegalStateException("Exception in first child", e); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private static class SecondChild extends FirstChild { |
| 112 | + protected void midMethod() { |
| 113 | + baseMethod(); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private static class DeepestChild extends SecondChild { |
| 118 | + public void methodThatThrows() { |
| 119 | + midMethod(); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private void triggerOutOfMemoryError() { |
| 124 | + try { |
| 125 | + ArrayList<byte[]> memoryHog = new ArrayList<>(); |
| 126 | + |
| 127 | + // Allocating memory in chunks until out of memory |
| 128 | + for (int i = 0; i < 100; i++) { |
| 129 | + memoryHog.add(new byte[10 * 1024 * 1024]); // 10MB chunks |
| 130 | + } |
| 131 | + } catch (OutOfMemoryError e) { |
| 132 | + throw e; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + private void triggerOverloadedMethodCrash() { |
| 137 | + crashingMethod("test"); |
| 138 | + } |
| 139 | + |
| 140 | + private void crashingMethod(String param) { |
| 141 | + crashingMethod(param, 10); |
| 142 | + } |
| 143 | + |
| 144 | + private void crashingMethod(String param, int value) { |
| 145 | + crashingMethod(param, value, true); |
| 146 | + } |
| 147 | + |
| 148 | + private void crashingMethod(String param, int value, boolean flag) { |
| 149 | + crashingMethod(param, value, flag, new HashMap<>()); |
| 150 | + } |
| 151 | + |
| 152 | + private void crashingMethod(String param, int value, boolean flag, Map<String, Object> extra) { |
| 153 | + if (param != null && value > 0 && flag) { |
| 154 | + throw new IllegalArgumentException( |
| 155 | + "Crasher called with: " + param + ", " + value + ", " + flag); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments