This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Repository navigation
Add generics support on RestTemplate methods #330
Merged
mathieuboniface
merged 20 commits into
androidannotations:develop
from
DayS:220_rest_template_generics
Feb 4, 2013
Merged
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
2fd3ca5
Add generics support on RestTemplate methods
DayS 6aeef78
Fix a ClassCastException
DayS 5af22d6
Refactoring of Get and Post processors
DayS ea5503b
Add unit tests for generics rest methods
DayS 8b29b74
Fix EBeanHolder.parseClass() to use a buffer. A partial copy of JCode…
DayS 434b528
Refactoring of Get and Post processors
DayS 5121f83
Extract custom JCodeModel code into a new class
DayS aa80b6a
Refactoring and comment rest processors
DayS 996d4d1
Fix runtime mapping of generics
DayS c392526
Handle generics of generics
DayS 74547ca
Handle array of generics
DayS 5193249
Handle generics of class
DayS bfaf275
Add integration tests for test rest methods
DayS e7cd689
Remove temporary logs
DayS 5197bfd
Fix EBeansHolder.refClass method to handle generics. Fix rest-templat…
DayS ac2fa84
Merge branch 'develop' into 220_rest_template_generics
DayS f68ae5b
Handle wildcard on EBeansHolder.refClass(). TypeVar throws exception
DayS 9f72516
Merge branch 'develop' into 220_rest_template_generics
DayS 36a289a
Add missing license header
DayS fb6432c
Refactoring code and remove the TypeNameParser fork class from codemodel
mathieuboniface File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Extract custom JCodeModel code into a new class
- Loading branch information
commit 5121f836c56fc8292e51199637914b8f55752e3c
There are no files selected for viewing
175 changes: 175 additions & 0 deletions
175
...ations/src/main/java/com/googlecode/androidannotations/processing/BufferedJCodeModel.java
This file contains hidden or 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,175 @@ | ||
| package com.googlecode.androidannotations.processing; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import com.sun.codemodel.JClass; | ||
| import com.sun.codemodel.JCodeModel; | ||
| import com.sun.codemodel.JType; | ||
|
|
||
| /** | ||
| * This class is a decorator of {@link JCodeModel} allowing usage of | ||
| * {@link #parseType(String)} and keeping each {@link JClass} found into the | ||
| * {@link EBeansHolder} buffer. This is useful for generics definition. | ||
| * <p /> | ||
| * <b>Note</b> : It's a hard-copy of a methods subset of {@link JCodeModel} with | ||
| * just one custom line (in {@link TypeNameParser#parseTypeName()} : | ||
| * | ||
| * <pre> | ||
| * JClass clazz = eBeansHolder.uniqueRefClass(s.substring(start, idx)); | ||
| * </pre> | ||
| * | ||
| * This solution was needed because JCodeModel is a final class. | ||
| */ | ||
| public class BufferedJCodeModel { | ||
|
|
||
| private final EBeansHolder eBeansHolder; | ||
| private final JCodeModel codeModel; | ||
|
|
||
| public BufferedJCodeModel(EBeansHolder eBeansHolder, JCodeModel codeModel) { | ||
| this.eBeansHolder = eBeansHolder; | ||
| this.codeModel = codeModel; | ||
| } | ||
|
|
||
| /** | ||
| * Obtains a type object from a type name. | ||
| * | ||
| * <p> | ||
| * This method handles primitive types, arrays, and existing {@link Class} | ||
| * es. | ||
| * | ||
| * @exception ClassNotFoundException | ||
| * If the specified type is not found. | ||
| */ | ||
| JType parseType(String name) throws ClassNotFoundException { | ||
| // array | ||
| if (name.endsWith("[]")) { | ||
| return parseType(name.substring(0, name.length() - 2)).array(); | ||
| } | ||
|
|
||
| // try primitive type | ||
| try { | ||
| return JType.parse(codeModel, name); | ||
| } catch (IllegalArgumentException e) { | ||
| ; | ||
| } | ||
|
|
||
| // existing class | ||
| return new TypeNameParser(name).parseTypeName(); | ||
| } | ||
|
|
||
| private final class TypeNameParser { | ||
| private final String s; | ||
| private int idx; | ||
|
|
||
| public TypeNameParser(String s) { | ||
| this.s = s; | ||
| } | ||
|
|
||
| /** | ||
| * Parses a type name token T (which can be potentially of the form | ||
| * Tr&ly;T1,T2,...>, or "? extends/super T".) | ||
| * | ||
| * @return the index of the character next to T. | ||
| */ | ||
| JClass parseTypeName() throws ClassNotFoundException { | ||
| int start = idx; | ||
|
|
||
| if (s.charAt(idx) == '?') { | ||
| // wildcard | ||
| idx++; | ||
| ws(); | ||
| String head = s.substring(idx); | ||
| if (head.startsWith("extends")) { | ||
| idx += 7; | ||
| ws(); | ||
| return parseTypeName().wildcard(); | ||
| } else if (head.startsWith("super")) { | ||
| throw new UnsupportedOperationException("? super T not implemented"); | ||
| } else { | ||
| // not supported | ||
| throw new IllegalArgumentException("only extends/super can follow ?, but found " + s.substring(idx)); | ||
| } | ||
| } | ||
|
|
||
| while (idx < s.length()) { | ||
| char ch = s.charAt(idx); | ||
| if (Character.isJavaIdentifierStart(ch) || Character.isJavaIdentifierPart(ch) || ch == '.') { | ||
| idx++; | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| JClass clazz = eBeansHolder.uniqueRefClass(s.substring(start, idx)); | ||
|
|
||
| return parseSuffix(clazz); | ||
| } | ||
|
|
||
| /** | ||
| * Parses additional left-associative suffixes, like type arguments and | ||
| * array specifiers. | ||
| */ | ||
| private JClass parseSuffix(JClass clazz) throws ClassNotFoundException { | ||
| if (idx == s.length()) { | ||
| return clazz; // hit EOL | ||
| } | ||
|
|
||
| char ch = s.charAt(idx); | ||
|
|
||
| if (ch == '<') { | ||
| return parseSuffix(parseArguments(clazz)); | ||
| } | ||
|
|
||
| if (ch == '[') { | ||
| if (s.charAt(idx + 1) == ']') { | ||
| idx += 2; | ||
| return parseSuffix(clazz.array()); | ||
| } | ||
| throw new IllegalArgumentException("Expected ']' but found " + s.substring(idx + 1)); | ||
| } | ||
|
|
||
| return clazz; | ||
| } | ||
|
|
||
| /** | ||
| * Skips whitespaces | ||
| */ | ||
| private void ws() { | ||
| while (Character.isWhitespace(s.charAt(idx)) && idx < s.length()) { | ||
| idx++; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Parses '<T1,T2,...,Tn>' | ||
| * | ||
| * @return the index of the character next to '>' | ||
| */ | ||
| private JClass parseArguments(JClass rawType) throws ClassNotFoundException { | ||
| if (s.charAt(idx) != '<') { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| idx++; | ||
|
|
||
| List<JClass> args = new ArrayList<JClass>(); | ||
|
|
||
| while (true) { | ||
| args.add(parseTypeName()); | ||
| if (idx == s.length()) { | ||
| throw new IllegalArgumentException("Missing '>' in " + s); | ||
| } | ||
| char ch = s.charAt(idx); | ||
| if (ch == '>') { | ||
| return rawType.narrow(args.toArray(new JClass[args.size()])); | ||
| } | ||
|
|
||
| if (ch != ',') { | ||
| throw new IllegalArgumentException(s); | ||
| } | ||
| idx++; | ||
| } | ||
|
|
||
| } | ||
| } | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the code implemented in
EBeanHolder.parseClass()should rather be implemented here :) .