Like attention, no more lost, your support means a lot to me!

🔥 Hi, I’m Chouchou. GitHub · Android-Notebook has been included in this article. Welcome to grow up with Chouchou Peng. (Contact information at GitHub)

preface

  • In the process of reading the source code, you will often see the source code calledSystem.getProperty(...)Gets system properties.
  • In this article, I will lead you to understandSystem.getProperty(...)Source code, if you can help, please be sure to click like plus attention, this is really very important to me.

directory


1. Initialization

First, let’s look at where the system properties are initialized/loaded:

System.java

Private static Properties props; System Properties that cannot be modified private static Properties unchangeableProps; private static native String[] specialProperties(); (simplified) static {1, initialization is not allowed to modify the system properties unchangeableProps = initUnchangeableSystemProperties (); 2, all initialization system properties props = new PropertiesWithNonOverrideableDefaults (unchangeableProps); . } - > (1) has been simplified to private static Properties initUnchangeableSystemProperties () {VMRuntime runtime = VMRuntime. GetRuntime (); Properties p = new Properties(); P.ut ("java.boot.class.path", Runtime.bootclasspath ()); p.put("java.class.path", runtime.classPath()); 1.2 Call specialProperties() to load the property parsePropertyAssignments(p, specialProperties()); . } - > 2 Properties of defaults are not allowed to change the static final class PropertiesWithNonOverrideableDefaults extends the Properties { PropertiesWithNonOverrideableDefaults(Properties defaults) { super(defaults); } @Override public Object put(Object key, Object value) { if (defaults.containsKey(key)) { return defaults.get(key); } return super.put(key, value); } @Override public Object remove(Object key) { if (defaults.containsKey(key)) { return null; } return super.remove(key); }}Copy the code

The above code has been simplified and we focus on the following points:

1, construct the unchangeableProps property object, and initialize all system properties;

2. Construct the props property object and pass in unchangeableProps as the default property. Props is a subclass of PropertiesWithNonOverrideableDefaults, thus ensure the system property read-only cannot write;

1.2. Call specialProperties() to load the property, which is a native method.


2. SpecialProperties () source code analysis

In this section we will examine the source code for specialProperties(), using the Android platform as an example:

System.c

static jobjectArray System_specialProperties(JNIEnv* env, jclass ignored) { jclass stringClass = (*env)->FindClass(env, "java/lang/String"); jobjectArray result = (*env)->NewObjectArray(env, 4, stringClass, NULL); char path[PATH_MAX]; . Path) const char* LIBRARY_path = getenv("LD_LIBRARY_PATH"); #if defined(__ANDROID__) if (library_path == NULL) { android_get_LD_LIBRARY_PATH(path, sizeof(path)); library_path = path; } #endif if (library_path == NULL) { library_path = ""; } char* java_path = malloc(strlen("java.library.path=") + strlen(library_path) + 1); strcpy(java_path, "java.library.path="); strcat(java_path, library_path); jstring java_path_str = (*env)->NewStringUTF(env, java_path); free((void*)java_path); (*env)->SetObjectArrayElement(env, result, 3, java_path_str); return result; }Copy the code

Java.library. path = android_get_LD_LIBRARY_PATH(…); Obtaining path:

linker.cpp

void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) { ... char* end = buffer; From default_LD_PATHS for (size_t I = 0; i < default_ld_paths.size(); ++i) { if (i > 0) *end++ = ':'; end = stpcpy(end, default_ld_paths[i].c_str()); } } } static void init_default_namespace_no_config(bool is_asan) { auto default_ld_paths = is_asan ? kAsanDefaultLdPaths : kDefaultLdPaths; . } #if defined(__LP64__) 64 bit system static const char* const kSystemLibDir = "/system/lib64"; static const char* const kVendorLibDir = "/vendor/lib64"; #else static const char* const kSystemLibDir = "/system/lib"; static const char* const kVendorLibDir = "/vendor/lib"; #endif static const char* const kDefaultLdPaths[] = { kSystemLibDir, kVendorLibDir, nullptr }; Static const char* const kAsanDefaultLdPaths[] = {omitted};Copy the code

The source code is not complicated. When we call System.getProperty(“java.library.path)”, we get two results:

  • For 64-bit systems, the return is", "the"/system/lib64 / vendor/lib64. ""
  • For 32-bit systems, the return is"/ system/lib", "/ vendor/lib"

When we look at these directories, we can see that there are indeed many.so libraries. When we call System.loadLibrary(…) When loading the dynamic library, the system’s built-in.so library is read/loaded from these paths.


3. Obtain and set system properties

3.1 Obtaining System Attributes

To get System properties, call System.getProperty(…). , the source is very simple:

System.java

public static String getProperty(String key) { checkKey(key); SecurityManager sm = getSecurityManager(); if (sm ! = null) { sm.checkPropertyAccess(key); } return props.getProperty(key); } -> public static SecurityManager getSecurityManager() {// no-op on android.return null; }Copy the code

3.2. Set system properties

To set System properties, call System.setProperty(…). , the source is very simple:

System.java

public static String setProperty(String key, String value) { checkKey(key); SecurityManager sm = getSecurityManager(); if (sm ! = null) { sm.checkPermission(new PropertyPermission(key, SecurityConstants.PROPERTY_WRITE_ACTION)); } return (String) props.setProperty(key, value); }Copy the code

Two points to note:

  • Built-in system attributes cannot be modified;
  • For custom attributes,SystemIt’s not persisted.

4. Common system attributes

Here are some common system attributes and their descriptions:


5. To summarize

  • 1, although system attributes are not the core knowledge, but understanding some key conclusions of this article is helpful to read more in-depth source code;
  • 2, the source of system attributes, part by JDK management, part by the specific runtime environment management (through native methodspecialProperties());
  • 3. Built-in system attributes are not allowed to be modified, and the modification of attributes is not stored persistently.

Creation is not easy, your “three lian” is chouchou’s biggest motivation, we will see you next time!