📄 statemachinefactory.html
字号:
<a name="247" href="#247">247</a> <strong class="jxr_keyword">if</strong> (!f.isAccessible()) {<a name="248" href="#248">248</a> f.setAccessible(<strong class="jxr_keyword">true</strong>);<a name="249" href="#249">249</a> }<a name="250" href="#250">250</a> <a name="251" href="#251">251</a> fields.add(f);<a name="252" href="#252">252</a> }<a name="253" href="#253">253</a> <a name="254" href="#254">254</a> <strong class="jxr_keyword">return</strong> fields;<a name="255" href="#255">255</a> }<a name="256" href="#256">256</a> <a name="257" href="#257">257</a> <strong class="jxr_keyword">static</strong> <a href="../../../../org/apache/mina/statemachine/annotation/State.html">State</a>[] createStates(List<Field> fields) {<a name="258" href="#258">258</a> LinkedHashMap<String, State> states = <strong class="jxr_keyword">new</strong> LinkedHashMap<String, State>();<a name="259" href="#259">259</a> <a name="260" href="#260">260</a> <strong class="jxr_keyword">while</strong> (!fields.isEmpty()) {<a name="261" href="#261">261</a> <strong class="jxr_keyword">int</strong> size = fields.size();<a name="262" href="#262">262</a> <strong class="jxr_keyword">int</strong> numStates = states.size();<a name="263" href="#263">263</a> <strong class="jxr_keyword">for</strong> (<strong class="jxr_keyword">int</strong> i = 0; i < size; i++) {<a name="264" href="#264">264</a> Field f = fields.remove(0);<a name="265" href="#265">265</a> <a name="266" href="#266">266</a> String value = <strong class="jxr_keyword">null</strong>;<a name="267" href="#267">267</a> <strong class="jxr_keyword">try</strong> {<a name="268" href="#268">268</a> value = (String) f.get(<strong class="jxr_keyword">null</strong>);<a name="269" href="#269">269</a> } <strong class="jxr_keyword">catch</strong> (IllegalAccessException iae) {<a name="270" href="#270">270</a> <strong class="jxr_keyword">throw</strong> <strong class="jxr_keyword">new</strong> <a href="../../../../org/apache/mina/statemachine/StateMachineCreationException.html">StateMachineCreationException</a>(<span class="jxr_string">"Error encountered when "</span> <a name="271" href="#271">271</a> + <span class="jxr_string">"processing field "</span> + f + <span class="jxr_string">"."</span>, iae);<a name="272" href="#272">272</a> }<a name="273" href="#273">273</a> <a name="274" href="#274">274</a> org.apache.mina.statemachine.annotation.State stateAnnotation = f<a name="275" href="#275">275</a> .getAnnotation(org.apache.mina.statemachine.annotation.State.<strong class="jxr_keyword">class</strong>);<a name="276" href="#276">276</a> <strong class="jxr_keyword">if</strong> (stateAnnotation.value().equals(org.apache.mina.statemachine.annotation.State.ROOT)) {<a name="277" href="#277">277</a> states.put(value, <strong class="jxr_keyword">new</strong> <a href="../../../../org/apache/mina/statemachine/annotation/State.html">State</a>(value));<a name="278" href="#278">278</a> } <strong class="jxr_keyword">else</strong> <strong class="jxr_keyword">if</strong> (states.containsKey(stateAnnotation.value())) {<a name="279" href="#279">279</a> states.put(value, <strong class="jxr_keyword">new</strong> <a href="../../../../org/apache/mina/statemachine/annotation/State.html">State</a>(value, states.get(stateAnnotation.value())));<a name="280" href="#280">280</a> } <strong class="jxr_keyword">else</strong> {<a name="281" href="#281">281</a> <em class="jxr_comment">// Move to the back of the list of fields for later</em><a name="282" href="#282">282</a> <em class="jxr_comment">// processing</em><a name="283" href="#283">283</a> fields.add(f);<a name="284" href="#284">284</a> }<a name="285" href="#285">285</a> }<a name="286" href="#286">286</a> <a name="287" href="#287">287</a> <em class="jxr_comment">/*</em><a name="288" href="#288">288</a> <em class="jxr_comment"> * If no new states were added to states during this iteration it </em><a name="289" href="#289">289</a> <em class="jxr_comment"> * means that all fields in fields specify non-existent parents.</em><a name="290" href="#290">290</a> <em class="jxr_comment"> */</em><a name="291" href="#291">291</a> <strong class="jxr_keyword">if</strong> (states.size() == numStates) {<a name="292" href="#292">292</a> <strong class="jxr_keyword">throw</strong> <strong class="jxr_keyword">new</strong> <a href="../../../../org/apache/mina/statemachine/StateMachineCreationException.html">StateMachineCreationException</a>(<span class="jxr_string">"Error encountered while creating "</span><a name="293" href="#293">293</a> + <span class="jxr_string">"FSM. The following fields specify non-existing "</span> <a name="294" href="#294">294</a> + <span class="jxr_string">"parent states: "</span> + fields);<a name="295" href="#295">295</a> }<a name="296" href="#296">296</a> }<a name="297" href="#297">297</a> <a name="298" href="#298">298</a> <strong class="jxr_keyword">return</strong> states.values().toArray(<strong class="jxr_keyword">new</strong> State[0]);<a name="299" href="#299">299</a> }<a name="300" href="#300">300</a> <a name="301" href="#301">301</a> <strong class="jxr_keyword">private</strong> <strong class="jxr_keyword">static</strong> <strong class="jxr_keyword">class</strong> <a href="../../../../org/apache/mina/statemachine/StateMachineFactory.html">TransitionWrapper</a> {<a name="302" href="#302">302</a> <strong class="jxr_keyword">private</strong> <strong class="jxr_keyword">final</strong> Class<? <strong class="jxr_keyword">extends</strong> Annotation> transitionClazz;<a name="303" href="#303">303</a> <strong class="jxr_keyword">private</strong> <strong class="jxr_keyword">final</strong> Annotation annotation;<a name="304" href="#304">304</a> <strong class="jxr_keyword">public</strong> <a href="../../../../org/apache/mina/statemachine/StateMachineFactory.html">TransitionWrapper</a>(Class<? <strong class="jxr_keyword">extends</strong> Annotation> transitionClazz, Annotation annotation) {<a name="305" href="#305">305</a> <strong class="jxr_keyword">this</strong>.transitionClazz = transitionClazz;<a name="306" href="#306">306</a> <strong class="jxr_keyword">this</strong>.annotation = annotation;<a name="307" href="#307">307</a> }<a name="308" href="#308">308</a> Object[] on() {<a name="309" href="#309">309</a> <strong class="jxr_keyword">return</strong> getParameter(<span class="jxr_string">"on"</span>, Object[].<strong class="jxr_keyword">class</strong>);<a name="310" href="#310">310</a> }<a name="311" href="#311">311</a> String[] in() {<a name="312" href="#312">312</a> <strong class="jxr_keyword">return</strong> getParameter(<span class="jxr_string">"in"</span>, String[].<strong class="jxr_keyword">class</strong>);<a name="313" href="#313">313</a> }<a name="314" href="#314">314</a> String next() {<a name="315" href="#315">315</a> <strong class="jxr_keyword">return</strong> getParameter(<span class="jxr_string">"next"</span>, String.<strong class="jxr_keyword">class</strong>);<a name="316" href="#316">316</a> }<a name="317" href="#317">317</a> <strong class="jxr_keyword">int</strong> weight() {<a name="318" href="#318">318</a> <strong class="jxr_keyword">return</strong> getParameter(<span class="jxr_string">"weight"</span>, Integer.TYPE);<a name="319" href="#319">319</a> }<a name="320" href="#320">320</a> @SuppressWarnings(<span class="jxr_string">"unchecked"</span>)<a name="321" href="#321">321</a> <strong class="jxr_keyword">private</strong> <T> T getParameter(String name, Class<T> returnType) {<a name="322" href="#322">322</a> <strong class="jxr_keyword">try</strong> {<a name="323" href="#323">323</a> Method m = transitionClazz.getMethod(name);<a name="324" href="#324">324</a> <strong class="jxr_keyword">if</strong> (!returnType.isAssignableFrom(m.getReturnType())) {<a name="325" href="#325">325</a> <strong class="jxr_keyword">throw</strong> <strong class="jxr_keyword">new</strong> <a href="../../../../org/apache/mina/statemachine/transition/NoSuchMethodException.html">NoSuchMethodException</a>();<a name="326" href="#326">326</a> }<a name="327" href="#327">327</a> <strong class="jxr_keyword">return</strong> (T) m.invoke(annotation);<a name="328" href="#328">328</a> } <strong class="jxr_keyword">catch</strong> (Throwable t) {<a name="329" href="#329">329</a> <strong class="jxr_keyword">throw</strong> <strong class="jxr_keyword">new</strong> <a href="../../../../org/apache/mina/statemachine/StateMachineCreationException.html">StateMachineCreationException</a>(<span class="jxr_string">"Could not get parameter '"</span> <a name="330" href="#330">330</a> + name + <span class="jxr_string">"' from Transition annotation "</span> + transitionClazz);<a name="331" href="#331">331</a> }<a name="332" href="#332">332</a> }<a name="333" href="#333">333</a> }<a name="334" href="#334">334</a> <a name="335" href="#335">335</a> <strong class="jxr_keyword">private</strong> <strong class="jxr_keyword">static</strong> <strong class="jxr_keyword">class</strong> <a href="../../../../org/apache/mina/statemachine/StateMachineFactory.html">TransitionsWrapper</a> {<a name="336" href="#336">336</a> <strong class="jxr_keyword">private</strong> <strong class="jxr_keyword">final</strong> Class<? <strong class="jxr_keyword">extends</strong> Annotation> transitionsclazz;<a name="337" href="#337">337</a> <strong class="jxr_keyword">private</strong> <strong class="jxr_keyword">final</strong> Class<? <strong class="jxr_keyword">extends</strong> Annotation> transitionClazz;<a name="338" href="#338">338</a> <strong class="jxr_keyword">private</strong> <strong class="jxr_keyword">final</strong> Annotation annotation;<a name="339" href="#339">339</a> <strong class="jxr_keyword">public</strong> <a href="../../../../org/apache/mina/statemachine/StateMachineFactory.html">TransitionsWrapper</a>(Class<? <strong class="jxr_keyword">extends</strong> Annotation> transitionClazz, <a name="340" href="#340">340</a> Class<? <strong class="jxr_keyword">extends</strong> Annotation> transitionsclazz, Annotation annotation) {<a name="341" href="#341">341</a> <strong class="jxr_keyword">this</strong>.transitionClazz = transitionClazz;<a name="342" href="#342">342</a> <strong class="jxr_keyword">this</strong>.transitionsclazz = transitionsclazz;<a name="343" href="#343">343</a> <strong class="jxr_keyword">this</strong>.annotation = annotation;<a name="344" href="#344">344</a> }<a name="345" href="#345">345</a> <a href="../../../../org/apache/mina/statemachine/StateMachineFactory.html">TransitionWrapper</a>[] value() {<a name="346" href="#346">346</a> Annotation[] annos = getParameter(<span class="jxr_string">"value"</span>, Annotation[].<strong class="jxr_keyword">class</strong>);<a name="347" href="#347">347</a> <a href="../../../../org/apache/mina/statemachine/StateMachineFactory.html">TransitionWrapper</a>[] wrappers = <strong class="jxr_keyword">new</strong> <a href="../../../../org/apache/mina/statemachine/StateMachineFactory.html">TransitionWrapper</a>[annos.length];<a name="348" href="#348">348</a> <strong class="jxr_keyword">for</strong> (<strong class="jxr_keyword">int</strong> i = 0; i < annos.length; i++) {<a name="349" href="#349">349</a> wrappers[i] = <strong class="jxr_keyword">new</strong> <a href="../../../../org/apache/mina/statemachine/StateMachineFactory.html">TransitionWrapper</a>(transitionClazz, annos[i]);<a name="350" href="#350">350</a> }<a name="351" href="#351">351</a> <strong class="jxr_keyword">return</strong> wrappers;<a name="352" href="#352">352</a> }<a name="353" href="#353">353</a> @SuppressWarnings(<span class="jxr_string">"unchecked"</span>)<a name="354" href="#354">354</a> <strong class="jxr_keyword">private</strong> <T> T getParameter(String name, Class<T> returnType) {<a name="355" href="#355">355</a> <strong class="jxr_keyword">try</strong> {<a name="356" href="#356">356</a> Method m = transitionsclazz.getMethod(name);<a name="357" href="#357">357</a> <strong class="jxr_keyword">if</strong> (!returnType.isAssignableFrom(m.getReturnType())) {<a name="358" href="#358">358</a> <strong class="jxr_keyword">throw</strong> <strong class="jxr_keyword">new</strong> <a href="../../../../org/apache/mina/statemachine/transition/NoSuchMethodException.html">NoSuchMethodException</a>();<a name="359" href="#359">359</a> }<a name="360" href="#360">360</a> <strong class="jxr_keyword">return</strong> (T) m.invoke(annotation);<a name="361" href="#361">361</a> } <strong class="jxr_keyword">catch</strong> (Throwable t) {<a name="362" href="#362">362</a> <strong class="jxr_keyword">throw</strong> <strong class="jxr_keyword">new</strong> <a href="../../../../org/apache/mina/statemachine/StateMachineCreationException.html">StateMachineCreationException</a>(<span class="jxr_string">"Could not get parameter '"</span> <a name="363" href="#363">363</a> + name + <span class="jxr_string">"' from Transitions annotation "</span> + transitionsclazz);<a name="364" href="#364">364</a> }<a name="365" href="#365">365</a> }<a name="366" href="#366">366</a> }<a name="367" href="#367">367</a> }</pre><hr/><div id="footer">This page was automatically generated by <a href="http://maven.apache.org/">Maven</a></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -