openmods.reflection.FieldAccess Java Examples

The following examples show how to use openmods.reflection.FieldAccess. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConfigurableClassAdapter.java    From OpenModsLib with MIT License 6 votes vote down vote up
public ConfigurableClassAdapter(Class<? extends T> cls) {
	this.cls = cls;

	ImmutableMap.Builder<String, FieldAdapter<?>> fields = ImmutableMap.builder();
	for (Field f : cls.getFields()) {
		Configurable ann = f.getAnnotation(Configurable.class);
		if (ann != null) {
			String name = ann.name();
			if (name.isEmpty()) name = f.getName();

			final FieldAccess<?> access = FieldAccess.create(f);
			final IStringSerializer<?> serializer = TypeRW.getStringSerializer(f.getType());
			Preconditions.checkState(serializer != null, "Can't find serializer for field %s", f);

			@SuppressWarnings({ "rawtypes", "unchecked" })
			final FieldAdapter<?> adapter = new FieldAdapter(serializer, access);

			fields.put(name, adapter);
		}
	}

	this.fields = fields.build();
}
 
Example #2
Source File: DropTagSerializer.java    From OpenModsLib with MIT License 6 votes vote down vote up
public void addFields(Object target) {
	Class<?> cls = target.getClass();
	while (cls != Object.class) {
		for (Field field : cls.getDeclaredFields()) {
			StoreOnDrop marker = field.getAnnotation(StoreOnDrop.class);
			if (marker == null) continue;

			Preconditions.checkArgument(ISyncableObject.class.isAssignableFrom(field.getType()),
					"Field '%s' has SyncableDrop annotation, but isn't ISyncableObject", field);

			final FieldAccess<ISyncableObject> wrappedField = FieldAccess.create(field);
			final ISyncableObject obj = wrappedField.get(target);
			Preconditions.checkNotNull(obj, "Field '%s' contains null", field);

			final String suggestedName = field.getName();
			final String name = Strings.isNullOrEmpty(marker.name())? suggestedName : marker.name();

			addObject(name, obj);
		}
		cls = cls.getSuperclass();
	}
}
 
Example #3
Source File: ClassFrontend.java    From OpenModsLib with MIT License 5 votes vote down vote up
public static void processClass(File file, final Class<?> cls) {
	ConfigProcessor processor = new ConfigProcessor();

	final Map<String, Field> fields = Maps.newHashMap();

	for (Field f : cls.getFields()) {
		Entry e = f.getAnnotation(Entry.class);
		if (e == null) continue;
		final Class<?> type = f.getType();
		if (!(type.equals(String.class))) {
			Log.warn("Field %s has @Entry annotation, but invalid type %s (should be String)", f, type);
			continue;
		}

		if (!Modifier.isStatic(f.getModifiers())) {
			Log.warn("Field %s has @Entry annotation, but isn't static", f);
			continue;
		}

		String name = e.name();
		if (name.equals(Entry.SAME_AS_FIELD)) name = f.getName();

		Field prev = fields.put(name, f);
		Preconditions.checkState(prev == null, "Duplicate field name: %s, fields: %s, %s", name, f, prev);

		final FieldAccess<String> wrap = FieldAccess.create(f);

		String defaultValue = wrap.get(null);
		Preconditions.checkNotNull(defaultValue, "Field %s has no default value", f);

		processor.addEntry(name, e.version(), defaultValue, (UpdateListener)value -> wrap.set(null, value), e.comment());
	}

	processor.process(file);
}
 
Example #4
Source File: ConfigurableClassAdapter.java    From OpenModsLib with MIT License 4 votes vote down vote up
public FieldAdapter(IStringSerializer<T> serializer, FieldAccess<T> access) {
	this.serializer = serializer;
	this.access = access;
}