Search in sources :

Example 91 with Method

use of java.lang.reflect.Method in project swagger-core by swagger-api.

the class ReflectionUtils method getParameterAnnotations.

public static Annotation[][] getParameterAnnotations(Method method) {
    Annotation[][] methodAnnotations = method.getParameterAnnotations();
    Method overriddenmethod = getOverriddenMethod(method);
    if (overriddenmethod != null) {
        Annotation[][] overriddenAnnotations = overriddenmethod.getParameterAnnotations();
        for (int i = 0; i < methodAnnotations.length; i++) {
            List<Type> types = new ArrayList<Type>();
            for (int j = 0; j < methodAnnotations[i].length; j++) {
                types.add(methodAnnotations[i][j].annotationType());
            }
            for (int j = 0; j < overriddenAnnotations[i].length; j++) {
                if (!types.contains(overriddenAnnotations[i][j].annotationType())) {
                    methodAnnotations[i] = ArrayUtils.add(methodAnnotations[i], overriddenAnnotations[i][j]);
                }
            }
        }
    }
    return methodAnnotations;
}
Also used : Type(java.lang.reflect.Type) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method)

Example 92 with Method

use of java.lang.reflect.Method in project swagger-core by swagger-api.

the class BaseReaderUtilsTest method extensionsTest.

@Test(dataProvider = "expectedData")
public void extensionsTest(String methodName, Map<String, Object> expected) throws NoSuchMethodException {
    final Method method = getClass().getDeclaredMethod(methodName);
    final Extension[] extensions = method.getAnnotation(ApiOperation.class).extensions();
    final Map<String, Object> map = BaseReaderUtils.parseExtensions(extensions);
    Assert.assertEquals(map, expected);
}
Also used : Extension(io.swagger.annotations.Extension) ApiOperation(io.swagger.annotations.ApiOperation) Method(java.lang.reflect.Method) Test(org.testng.annotations.Test)

Example 93 with Method

use of java.lang.reflect.Method in project swagger-core by swagger-api.

the class Reader method read.

private void read(ReaderContext context) {
    final SwaggerDefinition swaggerDefinition = context.getCls().getAnnotation(SwaggerDefinition.class);
    if (swaggerDefinition != null) {
        readSwaggerConfig(swaggerDefinition);
    }
    for (Method method : context.getCls().getMethods()) {
        if (ReflectionUtils.isOverriddenMethod(method, context.getCls())) {
            continue;
        }
        final Operation operation = new Operation();
        String operationPath = null;
        String httpMethod = null;
        final Type[] genericParameterTypes = method.getGenericParameterTypes();
        final Annotation[][] paramAnnotations = method.getParameterAnnotations();
        for (ReaderExtension extension : ReaderExtensions.getExtensions()) {
            if (operationPath == null) {
                operationPath = extension.getPath(context, method);
            }
            if (httpMethod == null) {
                httpMethod = extension.getHttpMethod(context, method);
            }
            if (operationPath == null || httpMethod == null) {
                continue;
            }
            if (extension.isReadable(context)) {
                extension.setDeprecated(operation, method);
                extension.applyConsumes(context, operation, method);
                extension.applyProduces(context, operation, method);
                extension.applyOperationId(operation, method);
                extension.applySummary(operation, method);
                extension.applyDescription(operation, method);
                extension.applySchemes(context, operation, method);
                extension.applySecurityRequirements(context, operation, method);
                extension.applyTags(context, operation, method);
                extension.applyResponses(context, operation, method);
                extension.applyImplicitParameters(context, operation, method);
                extension.applyExtensions(context, operation, method);
                for (int i = 0; i < genericParameterTypes.length; i++) {
                    extension.applyParameters(context, operation, genericParameterTypes[i], paramAnnotations[i]);
                }
            }
        }
        if (httpMethod != null) {
            if (operation.getResponses() == null) {
                operation.defaultResponse(new Response().description("successful operation"));
            }
            final Map<String, String> regexMap = new HashMap<String, String>();
            final String parsedPath = PathUtils.parsePath(operationPath, regexMap);
            Path path = swagger.getPath(parsedPath);
            if (path == null) {
                path = new Path();
                swagger.path(parsedPath, path);
            }
            path.set(httpMethod.toLowerCase(), operation);
        }
    }
}
Also used : Path(io.swagger.models.Path) HashMap(java.util.HashMap) Method(java.lang.reflect.Method) Operation(io.swagger.models.Operation) ReaderExtension(io.swagger.servlet.extensions.ReaderExtension) Response(io.swagger.models.Response) Type(java.lang.reflect.Type) SwaggerDefinition(io.swagger.annotations.SwaggerDefinition)

Example 94 with Method

use of java.lang.reflect.Method in project swagger-core by swagger-api.

the class ConsumesProducesTest method applyConsumesProducesTest1.

@Test(dataProvider = "resourceWithAnnotations")
public void applyConsumesProducesTest1(String methodName, List<String> expected) throws NoSuchMethodException {
    final Operation operation = new Operation();
    final ReaderContext context = createDefaultContext();
    final Method method = findMethod(context, methodName);
    extension.applyConsumes(context, operation, method);
    extension.applyProduces(context, operation, method);
    Assert.assertEquals(operation.getConsumes(), expected);
    Assert.assertEquals(operation.getProduces(), expected);
}
Also used : ReaderContext(io.swagger.servlet.ReaderContext) Operation(io.swagger.models.Operation) Method(java.lang.reflect.Method) Test(org.testng.annotations.Test)

Example 95 with Method

use of java.lang.reflect.Method in project jpHolo by teusink.

the class Capture method createMediaFile.

/**
     * Creates a JSONObject that represents a File from the Uri
     *
     * @param data the Uri of the audio/image/video
     * @return a JSONObject that represents a File
     * @throws IOException
     */
private JSONObject createMediaFile(Uri data) {
    File fp = webView.getResourceApi().mapUriToFile(data);
    JSONObject obj = new JSONObject();
    Class webViewClass = webView.getClass();
    PluginManager pm = null;
    try {
        Method gpm = webViewClass.getMethod("getPluginManager");
        pm = (PluginManager) gpm.invoke(webView);
    } catch (NoSuchMethodException e) {
    } catch (IllegalAccessException e) {
    } catch (InvocationTargetException e) {
    }
    if (pm == null) {
        try {
            Field pmf = webViewClass.getField("pluginManager");
            pm = (PluginManager) pmf.get(webView);
        } catch (NoSuchFieldException e) {
        } catch (IllegalAccessException e) {
        }
    }
    FileUtils filePlugin = (FileUtils) pm.getPlugin("File");
    LocalFilesystemURL url = filePlugin.filesystemURLforLocalPath(fp.getAbsolutePath());
    try {
        // File properties
        obj.put("name", fp.getName());
        obj.put("fullPath", fp.toURI().toString());
        if (url != null) {
            obj.put("localURL", url.toString());
        }
        // is stored in the audio or video content store.
        if (fp.getAbsoluteFile().toString().endsWith(".3gp") || fp.getAbsoluteFile().toString().endsWith(".3gpp")) {
            if (data.toString().contains("/audio/")) {
                obj.put("type", AUDIO_3GPP);
            } else {
                obj.put("type", VIDEO_3GPP);
            }
        } else {
            obj.put("type", FileHelper.getMimeType(Uri.fromFile(fp), cordova));
        }
        obj.put("lastModifiedDate", fp.lastModified());
        obj.put("size", fp.length());
    } catch (JSONException e) {
        // this will never happen
        e.printStackTrace();
    }
    return obj;
}
Also used : FileUtils(org.apache.cordova.file.FileUtils) JSONException(org.json.JSONException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) PluginManager(org.apache.cordova.PluginManager) Field(java.lang.reflect.Field) JSONObject(org.json.JSONObject) File(java.io.File) LocalFilesystemURL(org.apache.cordova.file.LocalFilesystemURL)

Aggregations

Method (java.lang.reflect.Method)8797 Test (org.junit.Test)1772 InvocationTargetException (java.lang.reflect.InvocationTargetException)1084 ArrayList (java.util.ArrayList)665 Field (java.lang.reflect.Field)611 IOException (java.io.IOException)549 HashMap (java.util.HashMap)352 Map (java.util.Map)290 List (java.util.List)275 PropertyDescriptor (java.beans.PropertyDescriptor)253 Annotation (java.lang.annotation.Annotation)212 Type (java.lang.reflect.Type)202 HashSet (java.util.HashSet)199 File (java.io.File)173 IndexedPropertyDescriptor (java.beans.IndexedPropertyDescriptor)170 BeanInfo (java.beans.BeanInfo)166 ParameterizedType (java.lang.reflect.ParameterizedType)132 Constructor (java.lang.reflect.Constructor)131 SimpleBeanInfo (java.beans.SimpleBeanInfo)128 FakeFox01BeanInfo (org.apache.harmony.beans.tests.support.mock.FakeFox01BeanInfo)128