You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
2.2 KiB
107 lines
2.2 KiB
8 years ago
|
/*
|
||
|
* Copyright (c) 2016 - present Facebook, Inc.
|
||
|
* All rights reserved.
|
||
|
*
|
||
|
* This source code is licensed under the BSD style license found in the
|
||
|
* LICENSE file in the root directory of this source tree. An additional grant
|
||
|
* of patent rights can be found in the PATENTS file in the same directory.
|
||
|
*/
|
||
|
|
||
|
package codetoanalyze.java.checkers;
|
||
|
|
||
8 years ago
|
import com.facebook.infer.annotation.ThreadSafe;
|
||
8 years ago
|
|
||
8 years ago
|
import com.google.common.collect.ImmutableList;
|
||
|
import com.google.common.collect.ImmutableList.Builder;
|
||
|
|
||
|
public class Builders {
|
||
|
|
||
|
static class Obj {
|
||
|
final String f;
|
||
|
String g;
|
||
|
|
||
|
public Obj(String f, String g) {
|
||
|
this.f = f;
|
||
|
this.g = g;
|
||
|
}
|
||
|
|
||
|
public static class Builder {
|
||
|
String f;
|
||
|
String g;
|
||
|
|
||
|
public Builder setFromObj(Obj input) {
|
||
|
this.f = input.f;
|
||
|
this.g = input.g;
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
public Obj build() {
|
||
|
return new Obj(f, g);
|
||
|
}
|
||
|
|
||
|
public Builder setF(String f) {
|
||
|
this.f = f;
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
public Builder setG(String g) {
|
||
|
this.g = g;
|
||
|
return this;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
@ThreadSafe
|
||
|
public void guavaBuilderOk() {
|
||
|
ImmutableList.Builder<String> builder = new ImmutableList.Builder();
|
||
8 years ago
|
builder.add("foo");
|
||
|
builder.build();
|
||
|
}
|
||
|
|
||
8 years ago
|
@ThreadSafe
|
||
|
public Obj customBuilderOk1() {
|
||
|
Obj.Builder builder = new Obj.Builder();
|
||
8 years ago
|
builder.setF("f");
|
||
|
builder.setG("g");
|
||
|
return builder.build();
|
||
|
}
|
||
|
|
||
8 years ago
|
@ThreadSafe
|
||
|
public Obj customBuilderOk2() {
|
||
|
Obj.Builder builder = new Obj.Builder();
|
||
8 years ago
|
return builder.setF("f").setG("g").build();
|
||
|
}
|
||
|
|
||
8 years ago
|
@ThreadSafe
|
||
|
public Obj customBuilderOk3() {
|
||
|
Obj obj = new Obj("a", "b");
|
||
8 years ago
|
Obj.Builder builder = new Obj.Builder();
|
||
8 years ago
|
return builder.setFromObj(obj).build();
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
@ThreadSafe
|
||
8 years ago
|
public Obj mutateBad(Obj o) {
|
||
|
o.g = "";
|
||
|
return o;
|
||
|
}
|
||
|
|
||
8 years ago
|
@ThreadSafe
|
||
8 years ago
|
public Obj buildThenMutateBad(Obj input) {
|
||
|
Obj.Builder builder = new Obj.Builder();
|
||
|
Obj output = builder.setFromObj(input).build();
|
||
|
input.g = "";
|
||
|
return output;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
@ThreadSafe
|
||
|
class TopLevelBuilder {
|
||
|
public String g;
|
||
|
|
||
|
public void setG(String g) {
|
||
|
this.g = g; // still want to warn if the builder is annotated ThreadSafe
|
||
|
}
|
||
|
|
||
|
}
|