8056052: Source.getContent() does excess Object.clone()

Reviewed-by: jlaskey, sundar
This commit is contained in:
Attila Szegedi 2014-08-26 15:04:48 +02:00
parent 5eb6a95a44
commit a72b265924
2 changed files with 8 additions and 6 deletions

View file

@ -711,11 +711,16 @@ public final class Source implements Loggable {
}
/**
* Get the content of this source as a char array
* @return content
* Get the content of this source as a char array. Note that the underlying array is returned instead of a
* clone; modifying the char array will cause modification to the source; this should not be done. While
* there is an apparent danger that we allow unfettered access to an underlying mutable array, the
* {@code Source} class is in a restricted {@code jdk.nashorn.internal.*} package and as such it is
* inaccessible by external actors in an environment with a security manager. Returning a clone would be
* detrimental to performance.
* @return content the content of this source as a char array
*/
public char[] getContent() {
return data().clone();
return data();
}
/**

View file

@ -117,9 +117,6 @@ public class SourceTest {
assertEquals(str1, str2);
assertEquals(source1.hashCode(), source2.hashCode());
assertTrue(source1.equals(source2));
// Test for immutability
Arrays.fill(source1.getContent(), (char)0);
Arrays.fill(source2.getContent(), (char)1);
assertTrue(Arrays.equals(source1.getContent(), str1.toCharArray()));
assertTrue(Arrays.equals(source1.getContent(), chars1));
assertTrue(Arrays.equals(source1.getContent(), source2.getContent()));