2017-07-28 85 views
-1

這是String類IBM /基於WebSphere的Java 64年8月1日如何從Java中的String類設置disableCopyInSubstring系統屬性?

/** 
    * This is a System property to disable copying the String when offset is non-zero in {@link #substring(int)}} and {@link #substring(int, int)}} 
    */ 
    static boolean disableCopyInSubstring; 

我想設置的屬性,像這樣的一個片段。

System.setProperty("java.lang.String.disableCopyInSubstring", "true"); 

但是,顯然這是不設置它,因爲我調試到String類和變量仍呈現false的正確方法。

從我正在使用的String類添加更大的片段。這是IBM的Websphere Java 1.8.64。當我問我最初以爲我在使用Oracle的Java。以下是該變量及以上的片段。

package java.lang; 

/* 
* Licensed Materials - Property of IBM, 
*  Copyright IBM Corp. 1998, 2016 All Rights Reserved 
*/ 

import java.io.Serializable; 

import java.util.Locale; 
import java.util.Comparator; 
import java.io.UnsupportedEncodingException; 

import java.util.regex.Pattern; 
import java.util.regex.PatternSyntaxException; 
import java.util.Formatter; 
import java.util.StringJoiner; 
import java.util.Iterator; 

import java.nio.charset.Charset; 

/** 
* Strings are objects which represent immutable arrays of 
* characters. 
* 
* @author  OTI 
* @version  initial 
* 
* @see   StringBuffer 
*/ 

public final class String implements Serializable, Comparable<String>, CharSequence { 
    // DO NOT CHANGE OR MOVE THIS LINE 
    // IT MUST BE THE FIRST THING IN THE INITIALIZATION 
    private static final boolean STRING_OPT_IN_HW = StrCheckHWAvailable(); 
    private static final long serialVersionUID = -6849794470754667710L; 

    /** 
    * CaseInsensitiveComparator compares Strings ignoring the case of the 
    * characters. 
    */ 
    private static final class CaseInsensitiveComparator implements Comparator<String>, Serializable { 
     static final long serialVersionUID = 8575799808933029326L; 

     /** 
     * Compare the two objects to determine 
     * the relative ordering. 
     * 
     * @param  o1 an Object to compare 
     * @param  o2 an Object to compare 
     * @return  an int < 0 if object1 is less than object2, 
     *    0 if they are equal, and > 0 if object1 is greater 
     * 
     * @exception ClassCastException when objects are not the correct type 
     */ 
     public int compare(String o1, String o2) { 
      return o1.compareToIgnoreCase(o2); 
     } 
    }; 

    /** 
    * A Comparator which compares Strings ignoring the case of the 
    * characters. 
    */ 
    public static final Comparator<String> CASE_INSENSITIVE_ORDER = new CaseInsensitiveComparator(); 
    private static final char[] ascii; 
    private static String[] stringArray; 
    private static final int stringArraySize = 10; 
    private static class UnsafeHelpers { 
     public final static long valueFieldOffset = getValueFieldOffset(); 
     static long getValueFieldOffset() { 
      try { return sun.misc.Unsafe.getUnsafe().objectFieldOffset(String.class.getDeclaredField("value")); } 
      catch (NoSuchFieldException e) { throw new RuntimeException(e); } 
     } 
    } 
    /** 
    * This is a System property to enable copy in {@link #String(String)}} 
    */ 
    static boolean enableCopy; 

    /** 
    * This is a System property to disable copying the String when offset is non-zero in {@link #substring(int)}} and {@link #substring(int, int)}} 
    */ 
    static boolean disableCopyInSubstring; 
+2

你確定這段代碼來自java.lang.String嗎?我無法在任何地方找到此屬性.. – ggradnig

+1

您是使用OpenJDK還是其他供應商? – talex

+0

我在1.8.77找不到它。但是,由於您在源代碼中找到了該變量,因此請在源代碼中搜索它所在的位置進行更改。 –

回答

0

要設置此係統屬性,請在VM參數中設置以下屬性。

-Djava.lang.string.substring.nocopy=true 

這樣就解決了這個問題。我只在IBM的Java實現中看到過這個問題。