• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright 2011 See AUTHORS file.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 
17 package com.badlogic.gdx.tests;
18 
19 import com.badlogic.gdx.Gdx;
20 import com.badlogic.gdx.graphics.GL20;
21 import com.badlogic.gdx.graphics.g2d.BitmapFont;
22 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
23 import com.badlogic.gdx.math.Vector2;
24 import com.badlogic.gdx.tests.utils.GdxTest;
25 import com.badlogic.gdx.utils.Json;
26 import com.badlogic.gdx.utils.reflect.ArrayReflection;
27 import com.badlogic.gdx.utils.reflect.ClassReflection;
28 import com.badlogic.gdx.utils.reflect.Constructor;
29 import com.badlogic.gdx.utils.reflect.Field;
30 import com.badlogic.gdx.utils.reflect.Method;
31 
32 /** Performs some tests with {@link ClassReflection} and prints the results on the screen.
33  * @author hneuer */
34 public class ReflectionTest extends GdxTest {
35 	String message = "";
36 	BitmapFont font;
37 	SpriteBatch batch;
38 
39 	@Override
create()40 	public void create () {
41 		font = new BitmapFont();
42 		batch = new SpriteBatch();
43 
44 		try {
45 			Vector2 fromDefaultConstructor = ClassReflection.newInstance(Vector2.class);
46 			println("From default constructor: " + fromDefaultConstructor);
47 
48 			Method mSet = ClassReflection.getMethod(Vector2.class, "set", float.class, float.class);
49 			mSet.invoke(fromDefaultConstructor, 10, 11);
50 			println("Set to 10/11: " + fromDefaultConstructor);
51 
52 			Constructor copyConstroctor = ClassReflection.getConstructor(Vector2.class, Vector2.class);
53 			Vector2 fromCopyConstructor = (Vector2)copyConstroctor.newInstance(fromDefaultConstructor);
54 			println("From copy constructor: " + fromCopyConstructor);
55 
56 			Method mMul = ClassReflection.getMethod(Vector2.class, "scl", float.class);
57 			println("Multiplied by 2; " + mMul.invoke(fromCopyConstructor, 2));
58 
59 			Method mNor = ClassReflection.getMethod(Vector2.class, "nor");
60 			println("Normalized: " + mNor.invoke(fromCopyConstructor));
61 
62 			Vector2 fieldCopy = new Vector2();
63 			Field fx = ClassReflection.getField(Vector2.class, "x");
64 			Field fy = ClassReflection.getField(Vector2.class, "y");
65 			fx.set(fieldCopy, fx.get(fromCopyConstructor));
66 			fy.set(fieldCopy, fy.get(fromCopyConstructor));
67 			println("Copied field by field: " + fieldCopy);
68 
69 			Json json = new Json();
70 			String jsonString = json.toJson(fromCopyConstructor);
71 			Vector2 fromJson = json.fromJson(Vector2.class, jsonString);
72 			println("JSON serialized: " + jsonString);
73 			println("JSON deserialized: " + fromJson);
74 			fromJson.x += 1;
75 			fromJson.y += 1;
76 			println("JSON deserialized + 1/1: " + fromJson);
77 
78 			Object array = ArrayReflection.newInstance(int.class, 5);
79 			ArrayReflection.set(array, 0, 42);
80 			println("Array int: length=" + ArrayReflection.getLength(array) + ", access=" + ArrayReflection.get(array, 0));
81 
82 			array = ArrayReflection.newInstance(String.class, 5);
83 			ArrayReflection.set(array, 0, "test string");
84 			println("Array String: length=" + ArrayReflection.getLength(array) + ", access=" + ArrayReflection.get(array, 0));
85 		} catch (Exception e) {
86 			message = "FAILED: " + e.getMessage() + "\n";
87 			message += e.getClass();
88 		}
89 	}
90 
println(String line)91 	private void println (String line) {
92 		message += line + "\n";
93 	}
94 
95 	@Override
render()96 	public void render () {
97 		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
98 		batch.begin();
99 		font.draw(batch, message, 20, Gdx.graphics.getHeight() - 20);
100 		batch.end();
101 	}
102 
103 	@Override
dispose()104 	public void dispose () {
105 		batch.dispose();
106 		font.dispose();
107 	}
108 }
109