Skip to content
Snippets Groups Projects
Commit d8afdcc3 authored by felmer's avatar felmer
Browse files

LMS-641 MethodUtils with tests introduced

SVN: 9087
parent b0087ee4
No related branches found
No related tags found
No related merge requests found
/*
* Copyright 2008 ETH Zuerich, CISD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.systemsx.cisd.common.utilities;
import java.lang.reflect.Method;
/**
* Functions around {@link Method}.
*
* @author Franz-Josef Elmer
*/
public class MethodUtils
{
private MethodUtils()
{
}
/**
* Returns the specified method as string which shows method name and parameter types
* in a way which more compact than {@link Method#toString()}.
*/
public static String toString(Method method)
{
StringBuilder builder = new StringBuilder(method.getName()).append('(');
Class<?>[] parameterTypes = method.getParameterTypes();
for (int i = 0; i < parameterTypes.length; i++)
{
builder.append(parameterTypes[i].getSimpleName());
if (i < parameterTypes.length - 1)
{
builder.append(", ");
}
}
return builder.append(')').toString();
}
}
/*
* Copyright 2008 ETH Zuerich, CISD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.systemsx.cisd.common.utilities;
import org.testng.AssertJUnit;
import org.testng.annotations.Test;
/**
*
*
* @author Franz-Josef Elmer
*/
public class MethodUtilsTest extends AssertJUnit
{
@Test
public void testMethodWithNoParameters() throws Exception
{
assertEquals("toString()", MethodUtils.toString(String.class.getMethod("toString")));
}
@Test
public void testMethodWithOneParameter() throws Exception
{
assertEquals("startsWith(String)", MethodUtils.toString(String.class.getMethod(
"startsWith", String.class)));
}
@Test
public void testMethodWithTwoParameters() throws Exception
{
assertEquals("split(String, int)", MethodUtils.toString(String.class.getMethod(
"split", String.class, Integer.TYPE)));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment