From 4e45738664179bcee5b81346cf29887afe292159 Mon Sep 17 00:00:00 2001 From: Michael Rebello Date: Mon, 10 Jun 2019 09:53:07 -0700 Subject: [PATCH] java: run clang-format for Java files (#56) `clang-format` can be used for auto-formatting Java files the same way it is for Objective-C and C++. 2 spaces were selected for spacing to maintain parity with upstream. Depends on: envoyproxy/envoy#7210 envoyproxy/envoy#7211 https://github.com/lyft/envoy-mobile/issues/55 Signed-off-by: Michael Rebello Signed-off-by: JP Simard --- mobile/.clang-format | 10 ++ mobile/envoy | 2 +- .../java/hello_world/MainActivity.java | 155 +++++++++--------- .../examples/java/hello_world/Response.java | 12 +- .../ResponseRecyclerViewAdapter.java | 40 ++--- .../java/hello_world/ResponseViewHolder.java | 25 +-- .../java/io/envoyproxy/envoymobile/Envoy.java | 38 ++--- mobile/tools/check_format.sh | 3 +- 8 files changed, 150 insertions(+), 135 deletions(-) diff --git a/mobile/.clang-format b/mobile/.clang-format index 2e4ab5b4479e..4885c0b508d5 100644 --- a/mobile/.clang-format +++ b/mobile/.clang-format @@ -7,6 +7,16 @@ PointerAlignment: Left SortIncludes: false ... +--- +Language: Java +AccessModifierOffset: -2 +ColumnLimit: 100 +DerivePointerAlignment: false +IndentWidth: 2 +PointerAlignment: Left +SortIncludes: false +... + --- Language: ObjC AccessModifierOffset: -2 diff --git a/mobile/envoy b/mobile/envoy index 71f1abaaef67..f056e6c102ce 160000 --- a/mobile/envoy +++ b/mobile/envoy @@ -1 +1 @@ -Subproject commit 71f1abaaef67952928d831fb162fb20259713568 +Subproject commit f056e6c102ce58a59acb3e0ecc35fac5d8c81dac diff --git a/mobile/examples/java/hello_world/MainActivity.java b/mobile/examples/java/hello_world/MainActivity.java index 89c1f8454f70..7dbe32c3b355 100644 --- a/mobile/examples/java/hello_world/MainActivity.java +++ b/mobile/examples/java/hello_world/MainActivity.java @@ -26,92 +26,97 @@ import io.envoyproxy.envoymobile.Envoy; public class MainActivity extends Activity { - private static final String ENDPOINT = "http://0.0.0.0:9001/api.lyft.com/static/demo/hello_world.txt"; - private RecyclerView recyclerView; + private static final String ENDPOINT = + "http://0.0.0.0:9001/api.lyft.com/static/demo/hello_world.txt"; + private RecyclerView recyclerView; - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); - Envoy envoy = new Envoy(); - envoy.load(); - String config = null; - try { - config = loadEnvoyConfig(getBaseContext(), R.raw.config); - } catch (RuntimeException e) { - Log.d("MainActivity", "exception getting config.", e); - throw new RuntimeException("Can't get config to run envoy."); - } - envoy.run(getBaseContext(), config); + Envoy envoy = new Envoy(); + envoy.load(); + String config = null; + try { + config = loadEnvoyConfig(getBaseContext(), R.raw.config); + } catch (RuntimeException e) { + Log.d("MainActivity", "exception getting config.", e); + throw new RuntimeException("Can't get config to run envoy."); + } + envoy.run(getBaseContext(), config); - recyclerView = (RecyclerView) findViewById(R.id.recycler_view); - recyclerView.setLayoutManager(new LinearLayoutManager(this)); + recyclerView = (RecyclerView)findViewById(R.id.recycler_view); + recyclerView.setLayoutManager(new LinearLayoutManager(this)); - final ResponseRecyclerViewAdapter adapter = new ResponseRecyclerViewAdapter(); - recyclerView.setAdapter(adapter); - DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL); - recyclerView.addItemDecoration(dividerItemDecoration); - HandlerThread thread = new HandlerThread(""); - thread.start(); - final Handler handler = new Handler(thread.getLooper()); - handler.postDelayed(new Runnable() { - @Override - public void run() { - try { - final Response response = makeRequest(); - recyclerView.post((Runnable) () -> adapter.add(response)); - } catch (IOException e) { - Log.d("MainActivity", "exception making request.", e); - } - // Make a call again - handler.postDelayed(this, TimeUnit.SECONDS.toMillis(1)); - } - }, 0); - } + final ResponseRecyclerViewAdapter adapter = new ResponseRecyclerViewAdapter(); + recyclerView.setAdapter(adapter); + DividerItemDecoration dividerItemDecoration = + new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL); + recyclerView.addItemDecoration(dividerItemDecoration); + HandlerThread thread = new HandlerThread(""); + thread.start(); - private Response makeRequest() throws IOException { - URL url = new URL(ENDPOINT); - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - int status = connection.getResponseCode(); - if (status != 200) { - throw new IOException("non 200 status: " + status); + final Handler handler = new Handler(thread.getLooper()); + handler.postDelayed(new Runnable() { + @Override + public void run() { + try { + final Response response = makeRequest(); + recyclerView.post((Runnable)() -> adapter.add(response)); + } catch (IOException e) { + Log.d("MainActivity", "exception making request.", e); } - List serverHeaderField = connection.getHeaderFields().get("server"); - InputStream inputStream = connection.getInputStream(); - String body = deserialize(inputStream); - inputStream.close(); - return new Response(body, serverHeaderField != null ? String.join(", ", serverHeaderField) : ""); + // Make a call again + handler.postDelayed(this, TimeUnit.SECONDS.toMillis(1)); + } + }, 0); + } + + private Response makeRequest() throws IOException { + URL url = new URL(ENDPOINT); + HttpURLConnection connection = (HttpURLConnection)url.openConnection(); + int status = connection.getResponseCode(); + if (status != 200) { + throw new IOException("non 200 status: " + status); } - private String deserialize(InputStream inputStream) throws IOException { - StringBuilder stringBuilder = new StringBuilder(); - BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); - String line = bufferedReader.readLine(); - while (line != null) { - stringBuilder.append(line); - line = bufferedReader.readLine(); - } - bufferedReader.close(); - return stringBuilder.toString(); + List serverHeaderField = connection.getHeaderFields().get("server"); + InputStream inputStream = connection.getInputStream(); + String body = deserialize(inputStream); + inputStream.close(); + return new Response(body, + serverHeaderField != null ? String.join(", ", serverHeaderField) : ""); + } + + private String deserialize(InputStream inputStream) throws IOException { + StringBuilder stringBuilder = new StringBuilder(); + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); + String line = bufferedReader.readLine(); + while (line != null) { + stringBuilder.append(line); + line = bufferedReader.readLine(); } + bufferedReader.close(); + return stringBuilder.toString(); + } - private String loadEnvoyConfig(Context context, int configResourceId) throws RuntimeException { - InputStream inputStream = context.getResources().openRawResource(configResourceId); - InputStreamReader inputReader = new InputStreamReader(inputStream); - BufferedReader bufReader = new BufferedReader(inputReader); - StringBuilder text = new StringBuilder(); + private String loadEnvoyConfig(Context context, int configResourceId) throws RuntimeException { + InputStream inputStream = context.getResources().openRawResource(configResourceId); + InputStreamReader inputReader = new InputStreamReader(inputStream); + BufferedReader bufReader = new BufferedReader(inputReader); + StringBuilder text = new StringBuilder(); - try { - String line; - while ((line = bufReader.readLine()) != null) { - text.append(line); - text.append('\n'); - } - } catch (IOException e) { - return null; - } - return text.toString(); + try { + String line; + while ((line = bufReader.readLine()) != null) { + text.append(line); + text.append('\n'); + } + } catch (IOException e) { + return null; } + return text.toString(); + } } diff --git a/mobile/examples/java/hello_world/Response.java b/mobile/examples/java/hello_world/Response.java index cea4330b45df..dd82c36d341c 100644 --- a/mobile/examples/java/hello_world/Response.java +++ b/mobile/examples/java/hello_world/Response.java @@ -3,11 +3,11 @@ import java.util.List; public class Response { - public final String title; - public final String header; + public final String title; + public final String header; - public Response(String title, String header) { - this.title = title; - this.header = header; - } + public Response(String title, String header) { + this.title = title; + this.header = header; + } } diff --git a/mobile/examples/java/hello_world/ResponseRecyclerViewAdapter.java b/mobile/examples/java/hello_world/ResponseRecyclerViewAdapter.java index dd5aed24d09d..76ae3d4ac522 100644 --- a/mobile/examples/java/hello_world/ResponseRecyclerViewAdapter.java +++ b/mobile/examples/java/hello_world/ResponseRecyclerViewAdapter.java @@ -10,28 +10,28 @@ import java.util.List; public class ResponseRecyclerViewAdapter extends RecyclerView.Adapter { - private final List data = new ArrayList<>(); + private final List data = new ArrayList<>(); - @Override - public ResponseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { - Context context = parent.getContext(); - LayoutInflater inflater = LayoutInflater.from(context); - View view = inflater.inflate(R.layout.item, parent, false); - return new ResponseViewHolder(view); - } + @Override + public ResponseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + Context context = parent.getContext(); + LayoutInflater inflater = LayoutInflater.from(context); + View view = inflater.inflate(R.layout.item, parent, false); + return new ResponseViewHolder(view); + } - @Override - public void onBindViewHolder(ResponseViewHolder holder, int position) { - holder.setResult(data.get(position)); - } + @Override + public void onBindViewHolder(ResponseViewHolder holder, int position) { + holder.setResult(data.get(position)); + } - @Override - public int getItemCount() { - return data.size(); - } + @Override + public int getItemCount() { + return data.size(); + } - public void add(Response response) { - data.add(0, response); - notifyItemInserted(0); - } + public void add(Response response) { + data.add(0, response); + notifyItemInserted(0); + } } diff --git a/mobile/examples/java/hello_world/ResponseViewHolder.java b/mobile/examples/java/hello_world/ResponseViewHolder.java index 6addf14b4337..a6650241b040 100644 --- a/mobile/examples/java/hello_world/ResponseViewHolder.java +++ b/mobile/examples/java/hello_world/ResponseViewHolder.java @@ -5,18 +5,19 @@ import android.widget.TextView; public class ResponseViewHolder extends RecyclerView.ViewHolder { - private final TextView responseTextView; - private final TextView headerTextView; + private final TextView responseTextView; + private final TextView headerTextView; - public ResponseViewHolder(View itemView) { - super(itemView); - this.responseTextView = (TextView) itemView.findViewById(R.id.response_text_view); - this.headerTextView = (TextView) itemView.findViewById(R.id.header_text_view); - } - - public void setResult(Response response) { - responseTextView.setText(responseTextView.getResources().getString(R.string.title_string, response.title)); - headerTextView.setText(headerTextView.getResources().getString(R.string.header_string, response.header)); - } + public ResponseViewHolder(View itemView) { + super(itemView); + this.responseTextView = (TextView)itemView.findViewById(R.id.response_text_view); + this.headerTextView = (TextView)itemView.findViewById(R.id.header_text_view); + } + public void setResult(Response response) { + responseTextView.setText( + responseTextView.getResources().getString(R.string.title_string, response.title)); + headerTextView.setText( + headerTextView.getResources().getString(R.string.header_string, response.header)); + } } diff --git a/mobile/library/java/io/envoyproxy/envoymobile/Envoy.java b/mobile/library/java/io/envoyproxy/envoymobile/Envoy.java index 50d798ac8749..860e83395b60 100644 --- a/mobile/library/java/io/envoyproxy/envoymobile/Envoy.java +++ b/mobile/library/java/io/envoyproxy/envoymobile/Envoy.java @@ -8,24 +8,22 @@ public class Envoy { - public void load() { - System.loadLibrary("envoy_jni"); - } - - public void run(Context context, String config) { - Thread thread = new Thread(new Runnable() { - @Override - public void run() { - initialize((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)); - runEnvoy(config.trim()); - } - }); - thread.start(); - } - - private native int initialize(ConnectivityManager connectivityManager); - - private native boolean isAresInitialized(); - - private native int runEnvoy(String config); + public void load() { System.loadLibrary("envoy_jni"); } + + public void run(Context context, String config) { + Thread thread = new Thread(new Runnable() { + @Override + public void run() { + initialize((ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE)); + runEnvoy(config.trim()); + } + }); + thread.start(); + } + + private native int initialize(ConnectivityManager connectivityManager); + + private native boolean isAresInitialized(); + + private native int runEnvoy(String config); } diff --git a/mobile/tools/check_format.sh b/mobile/tools/check_format.sh index cd195d49942f..9572bb53ffa2 100755 --- a/mobile/tools/check_format.sh +++ b/mobile/tools/check_format.sh @@ -18,5 +18,6 @@ fi # checker. envoy/tools/check_format.py \ --add-excluded-prefixes ./envoy/ ./envoy_build_config/extensions_build_config.bzl ./WORKSPACE ./dist/Envoy.framework/ \ - --skip_envoy_build_rule_check "$ENVOY_FORMAT_ACTION" + --skip_envoy_build_rule_check "$ENVOY_FORMAT_ACTION" \ + --namespace_check_excluded_paths ./examples/ ./library/java/ envoy/tools/format_python_tools.sh check