Skip to content

Commit fcf89d4

Browse files
authored
refactor(auth, database, firestore): add nullability annotations to java snippets (#425)
* refactor(rtdb): add NonNull annotations to the java snippets * refactor(auth, firestore): add NonNull annotations to the java snippets
1 parent 9f9e1e0 commit fcf89d4

File tree

8 files changed

+30
-25
lines changed

8 files changed

+30
-25
lines changed

auth/app/src/main/java/com/google/firebase/quickstart/auth/MainActivity.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -516,8 +516,8 @@ public void testPhoneVerify() {
516516
.setActivity(this)
517517
.setCallbacks(new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
518518
@Override
519-
public void onCodeSent(String verificationId,
520-
PhoneAuthProvider.ForceResendingToken forceResendingToken) {
519+
public void onCodeSent(@NonNull String verificationId,
520+
@NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
521521
// Save the verification id somewhere
522522
// ...
523523

@@ -526,13 +526,13 @@ public void onCodeSent(String verificationId,
526526
}
527527

528528
@Override
529-
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
529+
public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
530530
// Sign in with the credential
531531
// ...
532532
}
533533

534534
@Override
535-
public void onVerificationFailed(FirebaseException e) {
535+
public void onVerificationFailed(@NonNull FirebaseException e) {
536536
// ...
537537
}
538538
})
@@ -563,14 +563,14 @@ public void testPhoneAutoRetrieve() {
563563
.setActivity(this)
564564
.setCallbacks(new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
565565
@Override
566-
public void onVerificationCompleted(PhoneAuthCredential credential) {
566+
public void onVerificationCompleted(@NonNull PhoneAuthCredential credential) {
567567
// Instant verification is applied and a credential is directly returned.
568568
// ...
569569
}
570570

571571
// [START_EXCLUDE]
572572
@Override
573-
public void onVerificationFailed(FirebaseException e) {
573+
public void onVerificationFailed(@NonNull FirebaseException e) {
574574

575575
}
576576
// [END_EXCLUDE]

auth/app/src/main/java/com/google/firebase/quickstart/auth/PhoneAuthActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ protected void onCreate(Bundle savedInstanceState) {
4545
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
4646

4747
@Override
48-
public void onVerificationCompleted(PhoneAuthCredential credential) {
48+
public void onVerificationCompleted(@NonNull PhoneAuthCredential credential) {
4949
// This callback will be invoked in two situations:
5050
// 1 - Instant verification. In some cases the phone number can be instantly
5151
// verified without needing to send or enter a verification code.
@@ -58,7 +58,7 @@ public void onVerificationCompleted(PhoneAuthCredential credential) {
5858
}
5959

6060
@Override
61-
public void onVerificationFailed(FirebaseException e) {
61+
public void onVerificationFailed(@NonNull FirebaseException e) {
6262
// This callback is invoked in an invalid request for verification is made,
6363
// for instance if the the phone number format is not valid.
6464
Log.w(TAG, "onVerificationFailed", e);

database/app/src/main/java/com/google/firebase/referencecode/database/MainActivity.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package com.google.firebase.referencecode.database;
1717

1818
import android.os.Bundle;
19+
20+
import androidx.annotation.NonNull;
1921
import androidx.appcompat.app.AppCompatActivity;
2022
import android.util.Log;
2123

@@ -48,15 +50,15 @@ public void basicReadWrite() {
4850
// Read from the database
4951
myRef.addValueEventListener(new ValueEventListener() {
5052
@Override
51-
public void onDataChange(DataSnapshot dataSnapshot) {
53+
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
5254
// This method is called once with the initial value and again
5355
// whenever data at this location is updated.
5456
String value = dataSnapshot.getValue(String.class);
5557
Log.d(TAG, "Value is: " + value);
5658
}
5759

5860
@Override
59-
public void onCancelled(DatabaseError error) {
61+
public void onCancelled(@NonNull DatabaseError error) {
6062
// Failed to read value
6163
Log.w(TAG, "Failed to read value.", error.toException());
6264
}

database/app/src/main/java/com/google/firebase/referencecode/database/OfflineActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ private void fullConnectionExample() {
172172
final DatabaseReference connectedRef = database.getReference(".info/connected");
173173
connectedRef.addValueEventListener(new ValueEventListener() {
174174
@Override
175-
public void onDataChange(DataSnapshot snapshot) {
175+
public void onDataChange(@NonNull DataSnapshot snapshot) {
176176
boolean connected = snapshot.getValue(Boolean.class);
177177
if (connected) {
178178
DatabaseReference con = myConnectionsRef.push();
@@ -190,7 +190,7 @@ public void onDataChange(DataSnapshot snapshot) {
190190
}
191191

192192
@Override
193-
public void onCancelled(DatabaseError error) {
193+
public void onCancelled(@NonNull DatabaseError error) {
194194
Log.w(TAG, "Listener was cancelled at .info/connected");
195195
}
196196
});

database/app/src/main/java/com/google/firebase/referencecode/database/QueryActivity.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public void basicListen() {
6969
mMessagesRef = databaseReference.child("messages");
7070
mMessagesListener = new ValueEventListener() {
7171
@Override
72-
public void onDataChange(DataSnapshot dataSnapshot) {
72+
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
7373
// New data at this path. This method will be called after every change in the
7474
// data at this path or a subpath.
7575

@@ -88,7 +88,7 @@ public void onDataChange(DataSnapshot dataSnapshot) {
8888
}
8989

9090
@Override
91-
public void onCancelled(DatabaseError error) {
91+
public void onCancelled(@NonNull DatabaseError error) {
9292
// Could not successfully listen for data, log the error
9393
Log.e(TAG, "messages:onCancelled:" + error.getMessage());
9494
}
@@ -106,11 +106,11 @@ public void basicQuery() {
106106
myTopPostsQuery.addChildEventListener(new ChildEventListener() {
107107
// TODO: implement the ChildEventListener methods as documented above
108108
// [START_EXCLUDE]
109-
public void onChildAdded(DataSnapshot dataSnapshot, String s) { }
110-
public void onChildChanged(DataSnapshot dataSnapshot, String s) { }
111-
public void onChildRemoved(DataSnapshot dataSnapshot) { }
112-
public void onChildMoved(DataSnapshot dataSnapshot, String s) { }
113-
public void onCancelled(DatabaseError databaseError) { }
109+
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, String s) { }
110+
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, String s) { }
111+
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) { }
112+
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, String s) { }
113+
public void onCancelled(@NonNull DatabaseError databaseError) { }
114114
// [END_EXCLUDE]
115115
});
116116
// [END basic_query]
@@ -125,14 +125,14 @@ public void basicQueryValueListener() {
125125
// My top posts by number of stars
126126
myTopPostsQuery.addValueEventListener(new ValueEventListener() {
127127
@Override
128-
public void onDataChange(DataSnapshot dataSnapshot) {
128+
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
129129
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
130130
// TODO: handle the post
131131
}
132132
}
133133

134134
@Override
135-
public void onCancelled(DatabaseError databaseError) {
135+
public void onCancelled(@NonNull DatabaseError databaseError) {
136136
// Getting Post failed, log a message
137137
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
138138
// ...

database/app/src/main/java/com/google/firebase/referencecode/database/ReadAndWriteSnippets.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,9 @@ private String getUid() {
107107
// [START post_stars_transaction]
108108
private void onStarClicked(DatabaseReference postRef) {
109109
postRef.runTransaction(new Transaction.Handler() {
110+
@NonNull
110111
@Override
111-
public Transaction.Result doTransaction(MutableData mutableData) {
112+
public Transaction.Result doTransaction(@NonNull MutableData mutableData) {
112113
Post p = mutableData.getValue(Post.class);
113114
if (p == null) {
114115
return Transaction.success(mutableData);

firestore/app/src/main/java/com/google/example/firestore/DocSnippets.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ public void transactions() {
531531

532532
db.runTransaction(new Transaction.Function<Void>() {
533533
@Override
534-
public Void apply(Transaction transaction) throws FirebaseFirestoreException {
534+
public Void apply(@NonNull Transaction transaction) throws FirebaseFirestoreException {
535535
DocumentSnapshot snapshot = transaction.get(sfDocRef);
536536

537537
// Note: this could be done without a transaction
@@ -563,7 +563,7 @@ public void transactionPromise() {
563563

564564
db.runTransaction(new Transaction.Function<Double>() {
565565
@Override
566-
public Double apply(Transaction transaction) throws FirebaseFirestoreException {
566+
public Double apply(@NonNull Transaction transaction) throws FirebaseFirestoreException {
567567
DocumentSnapshot snapshot = transaction.get(sfDocRef);
568568
double newPopulation = snapshot.getDouble("population") + 1;
569569
if (newPopulation <= 1000000) {

firestore/app/src/main/java/com/google/example/firestore/SolutionAggregation.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.google.example.firestore;
22

3+
import androidx.annotation.NonNull;
4+
35
import com.google.android.gms.tasks.Task;
46
import com.google.firebase.firestore.DocumentReference;
57
import com.google.firebase.firestore.FirebaseFirestore;
@@ -54,7 +56,7 @@ private Task<Void> addRating(final DocumentReference restaurantRef, final float
5456
// In a transaction, add the new rating and update the aggregate totals
5557
return db.runTransaction(new Transaction.Function<Void>() {
5658
@Override
57-
public Void apply(Transaction transaction) throws FirebaseFirestoreException {
59+
public Void apply(@NonNull Transaction transaction) throws FirebaseFirestoreException {
5860
Restaurant restaurant = transaction.get(restaurantRef).toObject(Restaurant.class);
5961

6062
// Compute new number of ratings

0 commit comments

Comments
 (0)