Skip to content

Commit

Permalink
Fixing issue #139
Browse files Browse the repository at this point in the history
  • Loading branch information
oshoukry authored and oshoukry committed May 16, 2021
1 parent bc1ead4 commit 23678f8
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class SetterMustExistRule implements Rule {

public void evaluate(final PojoClass pojoClass) {
for (PojoField fieldEntry : pojoClass.getPojoFields()) {
if (!fieldEntry.isFinal() && !fieldEntry.hasSetter()) {
if (!fieldEntry.isFinal() && !fieldEntry.hasSetter() && !fieldEntry.isSynthetic()) {
Affirm.fail(String.format("[%s] is missing a setter", fieldEntry));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@

package com.openpojo.validation.rule.impl;

import com.openpojo.reflection.java.Java;
import com.openpojo.reflection.java.bytecode.asm.SimpleClassLoader;
import com.openpojo.validation.CommonCode;
import com.openpojo.validation.rule.Rule;
import com.openpojo.validation.rule.impl.sampleclasses.GetterDoesExistClass;
import com.openpojo.validation.rule.impl.sampleclasses.GetterDoesntExistClass;
import com.openpojo.validation.utils.AClassWithSyntheticFieldDumper;
import org.junit.Test;

import static com.openpojo.reflection.java.bytecode.asm.SubClassDefinition.GENERATED_CLASS_POSTFIX;

/**
* @author oshoukry
*/
Expand All @@ -39,10 +44,11 @@ public void testEvaluate() {
}

@Test
public void shouldIgnoreSyntheticFields() {
CommonCode.shouldPassRuleValidation(rule, AClassWithSyntheticField.class);
}
public void shouldIgnoreSyntheticFields() throws ClassNotFoundException {
final SimpleClassLoader simpleClassLoader = new SimpleClassLoader();
final String className = this.getClass().getPackage().getName() + ".AClassWithSyntheticField" + GENERATED_CLASS_POSTFIX;
final String classNameAsPath = className.replace(Java.PACKAGE_DELIMITER, Java.PATH_DELIMITER);

private class AClassWithSyntheticField {
CommonCode.shouldPassRuleValidation(rule, simpleClassLoader.loadThisClass(AClassWithSyntheticFieldDumper.dump(classNameAsPath), className));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@

package com.openpojo.validation.rule.impl;

import com.openpojo.reflection.java.Java;
import com.openpojo.reflection.java.bytecode.asm.SimpleClassLoader;
import com.openpojo.validation.CommonCode;
import com.openpojo.validation.rule.Rule;
import com.openpojo.validation.rule.impl.sampleclasses.SetterDoesExistClass;
import com.openpojo.validation.rule.impl.sampleclasses.SetterDoesntExistClass;
import com.openpojo.validation.utils.AClassWithSyntheticFieldDumper;
import org.junit.Test;

import static com.openpojo.reflection.java.bytecode.asm.SubClassDefinition.GENERATED_CLASS_POSTFIX;

/**
* @author oshoukry
*/
Expand All @@ -37,4 +42,13 @@ public void testEvaluate() {
CommonCode.shouldPassRuleValidation(rule, passClasses);
CommonCode.shouldFailRuleValidation(rule, failClasses);
}

@Test
public void shouldIgnoreSyntheticFields() throws Exception {
final SimpleClassLoader simpleClassLoader = new SimpleClassLoader();
final String className = this.getClass().getPackage().getName() + ".AClassWithSyntheticField" + GENERATED_CLASS_POSTFIX;
final String classNameAsPath = className.replace(Java.PACKAGE_DELIMITER, Java.PATH_DELIMITER);

CommonCode.shouldPassRuleValidation(rule, simpleClassLoader.loadThisClass(AClassWithSyntheticFieldDumper.dump(classNameAsPath), className));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2010-2021 Osman Shoukry
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.openpojo.validation.utils;

import jdk.internal.org.objectweb.asm.*;

/**
* this class dumps a class that looks like this:
*
* public class AClassWithSytheticStringField {
* private "synthetic" String syntheticString;
* }
*/
public class AClassWithSyntheticFieldDumper implements Opcodes {
public static byte[] dump(String className) {
ClassWriter cw = new ClassWriter(0);
FieldVisitor fv;
MethodVisitor mv;

cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, className, null, "java/lang/Object", null);

{
fv = cw.visitField(ACC_PRIVATE + ACC_SYNTHETIC, "syntheticString", "Ljava/lang/String;", null, null);
fv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
mv.visitInsn(RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
cw.visitEnd();

return cw.toByteArray();
}
}

0 comments on commit 23678f8

Please sign in to comment.