Created
May 4, 2022 15:05
-
-
Save patrickhuy/0392c54f52ad921bcf0152fe3cbb4d7c to your computer and use it in GitHub Desktop.
This file contains 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
package my.openpojo.tests; | |
import com.openpojo.reflection.PojoClass; | |
import com.openpojo.reflection.PojoField; | |
import com.openpojo.reflection.PojoMethod; | |
import com.openpojo.reflection.PojoPackage; | |
import java.lang.annotation.Annotation; | |
import java.util.List; | |
import java.util.Set; | |
import java.util.stream.Collectors; | |
class PojoClassExcludedFields implements PojoClass { | |
private final PojoClass clazz; | |
private final Set<String> excluded; | |
PojoClassExcludedFields(PojoClass clazz, Set<String> excluded) { | |
this.clazz = clazz; | |
this.excluded = excluded; | |
} | |
@Override | |
public boolean isInterface() { | |
return clazz.isInterface(); | |
} | |
@Override | |
public boolean isAbstract() { | |
return clazz.isAbstract(); | |
} | |
@Override | |
public boolean isConcrete() { | |
return clazz.isConcrete(); | |
} | |
@Override | |
public boolean isEnum() { | |
return clazz.isEnum(); | |
} | |
@Override | |
public boolean isArray() { | |
return clazz.isArray(); | |
} | |
@Override | |
public boolean isFinal() { | |
return clazz.isFinal(); | |
} | |
@Override | |
public boolean isSynthetic() { | |
return clazz.isSynthetic(); | |
} | |
@Override | |
public List<PojoField> getPojoFields() { | |
return clazz.getPojoFields().stream().filter(f -> !excluded.contains(f.getName())).collect(Collectors.toList()); | |
} | |
@Override | |
public List<PojoField> getPojoFieldsAnnotatedWith(Class<? extends Annotation> annotation) { | |
return clazz.getPojoFieldsAnnotatedWith(annotation); | |
} | |
@Override | |
public List<PojoMethod> getPojoMethods() { | |
return clazz.getPojoMethods(); | |
} | |
@Override | |
public List<PojoMethod> getPojoMethodsAnnotatedWith(Class<? extends Annotation> annotation) { | |
return clazz.getPojoMethodsAnnotatedWith(annotation); | |
} | |
@Override | |
public List<PojoMethod> getPojoConstructors() { | |
return clazz.getPojoConstructors(); | |
} | |
@Override | |
public PojoClass getEnclosingClass() { | |
return clazz.getEnclosingClass(); | |
} | |
@Override | |
public boolean extendz(Class<?> type) { | |
return clazz.extendz(type); | |
} | |
@Override | |
public PojoClass getSuperClass() { | |
return clazz.getSuperClass(); | |
} | |
@Override | |
public List<PojoClass> getInterfaces() { | |
return clazz.getInterfaces(); | |
} | |
@Override | |
public PojoPackage getPackage() { | |
return clazz.getPackage(); | |
} | |
@Override | |
public Class<?> getClazz() { | |
return clazz.getClazz(); | |
} | |
@Override | |
public boolean isNestedClass() { | |
return clazz.isNestedClass(); | |
} | |
@Override | |
public boolean isStatic() { | |
return clazz.isStatic(); | |
} | |
@Override | |
public void copy(Object from, Object to) { | |
clazz.copy(from, to); | |
} | |
@Override | |
public String toString(Object instance) { | |
return clazz.toString(instance); | |
} | |
@Override | |
public String getSourcePath() { | |
return clazz.getSourcePath(); | |
} | |
@Override | |
public String getName() { | |
return clazz.getName(); | |
} | |
@Override | |
public List<? extends Annotation> getAnnotations() { | |
return clazz.getAnnotations(); | |
} | |
@Override | |
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) { | |
return clazz.getAnnotation(annotationClass); | |
} | |
@Override | |
public boolean isPrivate() { | |
return clazz.isPrivate(); | |
} | |
@Override | |
public boolean isProtected() { | |
return clazz.isProtected(); | |
} | |
@Override | |
public boolean isPublic() { | |
return clazz.isPublic(); | |
} | |
@Override | |
public boolean isPackagePrivate() { | |
return clazz.isPackagePrivate(); | |
} | |
} |
This file contains 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
package my.openpojo.tests; | |
import java.util.Set; | |
import org.junit.Test; | |
import com.openpojo.reflection.impl.PojoClassFactory; | |
import com.openpojo.validation.Validator; | |
import com.openpojo.validation.ValidatorBuilder; | |
import com.openpojo.validation.rule.impl.GetterMustExistRule; | |
import com.openpojo.validation.rule.impl.SetterMustExistRule; | |
import com.openpojo.validation.test.impl.GetterTester; | |
import com.openpojo.validation.test.impl.SetterTester; | |
public class PojoExcludeDemo { | |
@Test | |
public void testPojo() { | |
Validator validator = ValidatorBuilder.create() | |
.with(new GetterMustExistRule()) | |
.with(new SetterMustExistRule()) | |
.with(new SetterTester()) | |
.with(new GetterTester()) | |
.build(); | |
validator.validate(new PojoClassExcludedFields(PojoClassFactory.getPojoClass(MyPojo.class), | |
Set.of("fieldIDontWantToInclude"))); | |
} | |
private static class MyPojo { | |
private String fieldIDontWantToInclude; | |
private String fieldIWant; | |
public String getFieldIDontWantToInclude() { | |
return fieldIDontWantToInclude; | |
} | |
public String getFieldIWant() { | |
return fieldIWant; | |
} | |
public void setFieldIWant(String fieldIWant) { | |
this.fieldIWant = fieldIWant; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment