1 /* 2 * Copyright 2014 The Android Open Source Project 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 dalvik.system; 18 19 import java.lang.reflect.Method; 20 21 public class CloseGuard { 22 private static Method m_get; 23 private static Method m_open; 24 private static Method m_close; 25 private static Method m_warnIfOpen; 26 static { 27 try { 28 Class<?> c_closeGuard = Class.forName("dalvik.system.CloseGuard"); 29 30 m_get = c_closeGuard.getDeclaredMethod("get"); 31 m_get.setAccessible(true); 32 33 m_open = c_closeGuard.getDeclaredMethod("open", String.class); 34 m_open.setAccessible(true); 35 36 m_close = c_closeGuard.getDeclaredMethod("close"); 37 m_close.setAccessible(true); 38 39 m_warnIfOpen = c_closeGuard.getDeclaredMethod("warnIfOpen"); 40 m_warnIfOpen.setAccessible(true); 41 } catch (Exception ignored) { 42 } 43 } 44 45 private final Object wrappedGuard; 46 CloseGuard(Object wrappedGuard)47 private CloseGuard(Object wrappedGuard) { 48 this.wrappedGuard = wrappedGuard; 49 } 50 get()51 public static CloseGuard get() { 52 if (m_get != null) { 53 try { 54 return new CloseGuard(m_get.invoke(null)); 55 } catch (Exception ignored) { 56 } 57 } 58 return new CloseGuard(null); 59 } 60 open(String message)61 public void open(String message) { 62 if (wrappedGuard != null && m_open != null) { 63 try { 64 m_open.invoke(wrappedGuard, message); 65 } catch (Exception ignored) { 66 } 67 } 68 } 69 close()70 public void close() { 71 if (wrappedGuard != null && m_close != null) { 72 try { 73 m_close.invoke(wrappedGuard); 74 } catch (Exception ignored) { 75 } 76 } 77 } 78 warnIfOpen()79 public void warnIfOpen() { 80 if (wrappedGuard != null && m_warnIfOpen != null) { 81 try { 82 m_warnIfOpen.invoke(wrappedGuard); 83 } catch (Exception ignored) { 84 } 85 } 86 } 87 } 88