Questions tagged [defensive-copy]
31 questions
1
vote
2 answers
Cloning "By": Defensive Programming with Selenium
So I'm writing a Panel Object API for a website I'm testing with Selenium, and since this one is likely to be used by many people outside of my control in the future, I thought I may as well make it defensive (as seen in Effective Java). However,…

FelixMarcus
- 380
- 3
- 14
0
votes
0 answers
How to get MyBatis to work with defensive copies?
Let's assume this simple schema:
create table User (
identity BIGINT UNSIGNED, PRIMARY KEY (identity),
name VARCHAR(32) NOT NULL);
create table Role (
identity BIGINT UNSIGNED, PRIMARY KEY (identity),
name VARCHAR(32) NOT NULL);
create…

Muton
- 761
- 2
- 7
- 16
0
votes
1 answer
swaping a collection with defensive copy
This is an example I have seen in the Effective C# book:
private BindingList data;
public IBindingList MyCollection
{
get { return data; }
}
public void UpdateData()
{
// Unreliable operation might fail:
var temp =…

Elad Benda
- 35,076
- 87
- 265
- 471
0
votes
0 answers
Is there a way to use the 'in' keyword with delegates, Func<>s or IComparers?
I apologize if the question is poorly-worded, this has been a really hard question to phrase or to search for an answer for and it doesn't help that the 'in' keyword is used for contravarient delegates. This is the best way that I can describe…

sqldatabunnies
- 1
- 1
0
votes
0 answers
C#: Why the order of these code blocks cause such drastic performance change?
Just started learning C#, I was testing any performance difference between passing an arguement to a method using in modifier and without it. So I wrote the code below which is behaving quite strangely,
class Program
{
static void Main(string[]…

stackhasoverflowed
- 23
- 3
0
votes
1 answer
More succinct code for a defensive copy using Guava's immutable types?
I want to make a defensive copy of a collection passed into a method using Guava's immutable types, e.g. an ImmutableList. I must also be able to deal with null input and treat that like an empty collection.
The cleanest I could come up with was…

Hein Blöd
- 1,553
- 1
- 18
- 25
0
votes
2 answers
add a defensive copy of an object to a hashset
Currently I have this code:
public final class Tutor {
private String name;
private final Set tutees;
public Tutor(String name, Student[] students){
this.name = name;
tutees = new HashSet();
for (int i = 0;…

pxdr0
- 73
- 7
0
votes
1 answer
Passing an immutable list to ImmutableList.copyOf()?
I have the following code:
private static final ImmutableMultimap namesToAddress;
public static List getAddresses(String name){
return ImmutableList.copyOf(namesToAddress.get(name));
}
My question is wheter the defensive…

user1974753
- 1,359
- 1
- 18
- 32
0
votes
3 answers
'Collections.unmodifiableCollection' in constructor
From time to time during code reviews I see constructors like that one:
Foo(Collection words) {
this.words = Collections.unmodifiableCollection(words);
}
Is this proper way of protecting internal state of the class? If not, what's the…

Michal Kordas
- 10,475
- 7
- 58
- 103
0
votes
2 answers
Sonar Violation:Security-Array is stored directly
I m trying to change my codes in my application with using Sonar.
How to fix it and why?
Thanks.
public class BeanResultSetHandler extends BasicResultSetHandler {
T instance;
Class clas;
Object[] selectFields;
/**
* Constructor
…

nurdan karaman
- 203
- 2
- 4
- 13
0
votes
3 answers
Can this class be made more immutable?
package main;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public final class Tutor {
private final String name;
private final Set tutees;
public Tutor(String name,…

Anthony
- 13
- 5
0
votes
2 answers
Can defensive copies by used the assist with thread-safety?
Is making a defensive copy of the fileName argument necessary in this example?
public static Context getInstanceFromFile(final String fileName)
throws IOException, FileNotFoundException, ContextException {
if (fileName == null) {
throw…

Hendré
- 262
- 10
- 27
0
votes
4 answers
Do I need defensive copying constructing an immutable class with *non final* albeit immutable fields?
One should not expose the reference to a non final field if one wants to construct an immutable class - but even for immutable objects like Strings ?
public final class Test { // Test class is meant to be immutable
private String s; // CAN'T…

Mr_and_Mrs_D
- 32,208
- 39
- 178
- 361
0
votes
0 answers
Defensive copy of streams and process
I'm having an Process proc running which I need to destroy and recreate if an error occurs.
That is because of an issue with sending commands via stdin to the process which it can't resolve.
My workaround is to reset the "connection" I…

Zhedar
- 3,480
- 1
- 21
- 44
-1
votes
2 answers
Adding objects (copied through Defensive copying using BeanUtils) in ArrayList giving stale data: Spring 3
public class CustomerDTO {
private int customerId;
private String customerName;
private String customerAddress;
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
…

ritesh
- 907
- 3
- 11
- 31