1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 package com.android.networkrecommendation; 17 18 import android.net.RecommendationRequest; 19 20 import java.lang.reflect.Method; 21 22 public class Example { 23 Example()24 public Example() {} 25 buildRecommendationRequest()26 public void buildRecommendationRequest() { 27 new RecommendationRequest.Builder().build(); 28 } 29 30 @SuppressWarnings("unchecked") reflectRecommendationRequest()31 public RecommendationRequest reflectRecommendationRequest() { 32 try { 33 Class builder = RecommendationRequest.class.getClasses()[0]; 34 Object builderObj = builder.getConstructor().newInstance(); 35 Method build = builder.getDeclaredMethod("build"); 36 return (RecommendationRequest) build.invoke(builderObj); 37 } catch (Exception e) { 38 throw new RuntimeException("Noooo", e); 39 } 40 } 41 } 42