Preparing for tracking the object creation etc.
[jpf-core.git] / src / peers / gov / nasa / jpf / vm / JPF_java_text_SimpleDateFormat.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18
19 package gov.nasa.jpf.vm;
20
21 import gov.nasa.jpf.annotation.MJI;
22 import gov.nasa.jpf.vm.MJIEnv;
23 import gov.nasa.jpf.vm.NativePeer;
24
25 import java.text.DateFormat;
26 import java.text.Format;
27 import java.text.SimpleDateFormat;
28 import java.util.Date;
29
30 /**
31  * (incomplete) native peer for SimpleDateFormat. See Format for details
32  * about native formatter delegation
33  */
34 public class JPF_java_text_SimpleDateFormat extends NativePeer {
35
36   SimpleDateFormat getInstance (MJIEnv env, int objref) {
37     Format fmt = JPF_java_text_Format.getInstance(env,objref);
38     assert fmt instanceof SimpleDateFormat;
39
40     return (SimpleDateFormat)fmt;
41   }
42
43   @MJI
44   public void init0____V (MJIEnv env, int objref) {
45     SimpleDateFormat fmt = new SimpleDateFormat();
46     JPF_java_text_Format.putInstance(env,objref,fmt);
47   }
48
49   @MJI
50   public void init0__Ljava_lang_String_2__V (MJIEnv env, int objref, int patternref) {
51     String pattern = env.getStringObject(patternref);
52
53     SimpleDateFormat fmt = new SimpleDateFormat(pattern);
54     JPF_java_text_Format.putInstance(env,objref,fmt);
55   }
56
57   @MJI
58   public void init0__II__V (MJIEnv env, int objref, int timeStyle, int dateStyle) {
59     // we are lost here - can't call this SimpleDateFormat ctor because it's package private
60     // (this is called - and has to be intercepted - from the DateFormat.getInstance() factory)
61
62     DateFormat fmt = null;
63
64     if (timeStyle < 0) {
65       fmt = DateFormat.getDateInstance(dateStyle);
66     } else if (dateStyle < 0) {
67       fmt = DateFormat.getTimeInstance(timeStyle);
68     } else {
69       fmt = DateFormat.getDateTimeInstance(dateStyle, timeStyle);
70     }
71
72     JPF_java_text_Format.putInstance(env,objref,fmt);
73   }
74
75   @MJI
76   public int format0 (MJIEnv env, int objref, long dateTime) {
77     Date date = new Date(dateTime);
78     SimpleDateFormat f = getInstance(env,objref);
79     String s = f.format(date);
80     return env.newString(s);
81   }
82   
83   @MJI
84   public void applyPattern__Ljava_lang_String_2__V (MJIEnv env, int objRef, int patternRef) {
85     SimpleDateFormat format = getInstance (env, objRef);
86     String pattern = env.getStringObject(patternRef);
87     format.applyPattern(pattern);
88   }
89 }