-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
C10NTranslations refactoring (refs #12)
* You can now get the evaluated translation value for each locale (`getValue()`) * All declared annotations with their values are also accessible (`getAnnotations()`) * Translation evaluation is powered by the `DummyInstanceProvider` responsible for providing parameter instances for parameterized interface methods * Test suite rewrite
- Loading branch information
rodionmoiseev
committed
Dec 25, 2012
1 parent
7a9af7c
commit 46df654
Showing
18 changed files
with
775 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
tools/src/main/java/c10n/tools/inspector/DefaultDummyInstanceProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 c10n.tools.inspector; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* <p>Default dummy instance provide that can generate dummy values for | ||
* for all primitive types and strings.</p> | ||
* | ||
* @author rodion | ||
*/ | ||
class DefaultDummyInstanceProvider implements DummyInstanceProvider { | ||
@Override | ||
public Object getInstance(Class<?> c10nInterface, Method method, Class<?> paramType, int paramIndex) { | ||
//Replace String and Object type args with their respective index | ||
if (paramType.isAssignableFrom(String.class) || | ||
paramType.isAssignableFrom(CharSequence.class)) { | ||
return String.format("{%d}", paramIndex); | ||
} else if (paramType.equals(byte.class)) { | ||
return (byte) paramIndex; | ||
} else if (paramType.equals(short.class)) { | ||
return (short) paramIndex; | ||
} else if (paramType.equals(int.class)) { | ||
return paramIndex; | ||
} else if (paramType.equals(long.class)) { | ||
return (long) paramIndex; | ||
} else if (paramType.equals(float.class)) { | ||
return (float) paramIndex; | ||
} else if (paramType.equals(double.class)) { | ||
return (double) paramIndex; | ||
} else if (paramType.equals(boolean.class)) { | ||
return false; | ||
} else if (paramType.equals(char.class)) { | ||
return Character.forDigit(paramIndex, 10); | ||
} else { | ||
//give up | ||
return null; | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
tools/src/main/java/c10n/tools/inspector/DummyInstanceProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 c10n.tools.inspector; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* @author rodion | ||
*/ | ||
public interface DummyInstanceProvider { | ||
/** | ||
* <p>Generate a dummy parameter instance for parameterized c10n methods.</p> | ||
* <p>Consider the interface below: | ||
* <code><pre> | ||
* public interface Messages{ | ||
* @En("Message {0}, {1}") | ||
* String msg(String arg1, int arg2); | ||
* } | ||
* </pre></code> | ||
* <p/> | ||
* During inspection, translated value for the <code>msg(String,int)</code> | ||
* method will be evaluated by invoking it for each locale. Since invokation | ||
* requires a list of instaces of <code>String</code> and <code>int</code>, | ||
* dummy instances will have to be generated at runtime. | ||
* </p> | ||
* <p>This method must make sure it always returns an instance of type | ||
* specified by the <code>paramType</code> argument, or null if it cannot | ||
* be correctly generated. If the generated instance is of other type, or null, | ||
* the value for {@link c10n.tools.inspector.C10NTranslations#getValue()} after | ||
* inspection will also be <code>null</code>.</p> | ||
* | ||
* @param c10nInterface c10n interface declaring the corresponding method (not null) | ||
* @param method c10n method for which the translation is being provided (not null) | ||
* @param paramType type of the parameter for which to generate the dummy instance (not null) | ||
* @param paramIndex position of the parameter in the argument list | ||
* @return instance of given parameter type (<code>paramType</code>) or <code>null</code> | ||
* if instance cannot be generated. | ||
*/ | ||
Object getInstance(Class<?> c10nInterface, Method method, Class<?> paramType, int paramIndex); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.