public MethodNode newMethod(final String name, final ClassNode returnType) { MethodNode node = new MethodNode(name, Opcodes.ACC_PUBLIC, returnType, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, EmptyStatement.INSTANCE); generatedMethods.add(node); return node; }
private void addDummyConstructor(ClassNode node) { // constructors should not be treated as errors (they have no real meaning for interfaces anyway) node.addMethod(new MethodNode("<clinit>", ACC_PUBLIC | ACC_STATIC, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null)); }
private static MethodNode createBuildMethod(ClassNode builder, String buildMethodName, List<FieldNode> fields) { ClassNode returnType = makeClassSafeWithGenerics(builder, unsetGenTypes(fields.size())); return new MethodNode(buildMethodName, PUBLIC_STATIC, returnType, NO_PARAMS, NO_EXCEPTIONS, block(returnS(ctorX(returnType)))); }
private void visitGetter(PropertyNode node, Statement getterBlock, int getterModifiers, String secondGetterName) { MethodNode secondGetter = new MethodNode(secondGetterName, getterModifiers, node.getType(), Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, getterBlock); secondGetter.setSynthetic(true); addPropertyMethod(secondGetter); visitMethod(secondGetter); }
private static MethodNode createBuilderMethod(AnnotationNode anno, ClassNode builder) { String builderMethodName = getMemberStringValue(anno, "builderMethodName", "builder"); final BlockStatement body = new BlockStatement(); body.addStatement(returnS(ctorX(builder))); return new MethodNode(builderMethodName, PUBLIC_STATIC, builder, NO_PARAMS, NO_EXCEPTIONS, body); }
private static MethodNode createBuildMethod(BuilderASTTransformation transform, AnnotationNode anno, ClassNode sourceClass, List<PropertyInfo> fields) { String buildMethodName = transform.getMemberStringValue(anno, "buildMethodName", "build"); final BlockStatement body = new BlockStatement(); Expression sourceClassInstance = initializeInstance(sourceClass, fields, body); body.addStatement(returnS(sourceClassInstance)); return new MethodNode(buildMethodName, ACC_PUBLIC, sourceClass, NO_PARAMS, NO_EXCEPTIONS, body); }
public void testDetectsCorrectMethodModifiersInClass() throws Exception { // can't check volatile here as it doubles up with bridge ClassNode node = new ClassNode("zzz", ACC_PUBLIC, ClassHelper.OBJECT_TYPE); node.addMethod(new MethodNode("st", ACC_STRICT, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null)); node.addMethod(new MethodNode("na", ACC_NATIVE, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null)); node.addMethod(new MethodNode("sy", ACC_SYNCHRONIZED, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null)); addDummyConstructor(node); verifier.visitClass(node); checkErrorCount(0); }
private static MethodNode createBuilderMethod(String buildMethodName, ClassNode builder, int numFields, String builderMethodName) { final BlockStatement body = new BlockStatement(); body.addStatement(returnS(callX(builder, buildMethodName))); ClassNode returnType = makeClassSafeWithGenerics(builder, unsetGenTypes(numFields)); return new MethodNode(builderMethodName, PUBLIC_STATIC, returnType, NO_PARAMS, NO_EXCEPTIONS, body); }
private static MethodNode createBuildMethod(AnnotationNode anno, ClassNode buildee, List<PropertyInfo> props) { String buildMethodName = getMemberStringValue(anno, "buildMethodName", "build"); final BlockStatement body = new BlockStatement(); body.addStatement(returnS(initializeInstance(buildee, props, body))); return new MethodNode(buildMethodName, ACC_PUBLIC, newClass(buildee), NO_PARAMS, NO_EXCEPTIONS, body); }
private void checkDetectsDuplicateMethods(int modifiers, String expectedErrorMessage, Parameter[] params) { ClassNode node = new ClassNode("zzz", modifiers, ClassHelper.OBJECT_TYPE); node.addMethod(new MethodNode("xxx", ACC_PUBLIC, ClassHelper.OBJECT_TYPE, params, ClassNode.EMPTY_ARRAY, null)); node.addMethod(new MethodNode("xxx", ACC_PUBLIC, ClassHelper.OBJECT_TYPE, params, ClassNode.EMPTY_ARRAY, null)); verifier.visitClass(node); checkErrorCount(2); checkErrorMessage(expectedErrorMessage); }
public void testDetectsIncorrectMethodModifiersInInterface() throws Exception { // can't check volatile here as it doubles up with bridge ClassNode node = new ClassNode("zzz", ACC_ABSTRACT | ACC_INTERFACE, ClassHelper.OBJECT_TYPE); node.addMethod(new MethodNode("st", ACC_STRICT, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null)); node.addMethod(new MethodNode("na", ACC_NATIVE, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null)); node.addMethod(new MethodNode("sy", ACC_SYNCHRONIZED, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null)); addDummyConstructor(node); verifier.visitClass(node); checkErrorCount(3); checkErrorMessage(EXPECTED_STRICT_METHOD_ERROR_MESSAGE); checkErrorMessage(EXPECTED_NATIVE_METHOD_ERROR_MESSAGE); checkErrorMessage(EXPECTED_SYNCHRONIZED_METHOD_ERROR_MESSAGE); }
public void testDetectsFinalAndStaticMethodsInInterface() throws Exception { ClassNode node = new ClassNode("zzz", ACC_ABSTRACT | ACC_INTERFACE, ClassHelper.OBJECT_TYPE); node.addMethod(new MethodNode("xxx", ACC_PUBLIC | ACC_FINAL, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null)); node.addMethod(new MethodNode("yyy", ACC_PUBLIC | ACC_STATIC, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null)); addDummyConstructor(node); verifier.visitClass(node); checkErrorCount(2); checkErrorMessage(EXPECTED_INTERFACE_FINAL_METHOD_ERROR_MESSAGE); checkErrorMessage(EXPECTED_INTERFACE_STATIC_METHOD_ERROR_MESSAGE); }
private FixtureMethod getInitializerMethod() { if (spec.getInitializerMethod() == null) { // method is private s.t. multiple initializer methods along hierarchy can be called independently MethodNode gMethod = new MethodNode(InternalIdentifiers.INITIALIZER_METHOD, Opcodes.ACC_PRIVATE | Opcodes.ACC_SYNTHETIC, ClassHelper.DYNAMIC_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, new BlockStatement()); spec.getAst().addMethod(gMethod); FixtureMethod method = new FixtureMethod(spec, gMethod); method.addBlock(new AnonymousBlock(method)); spec.setInitializerMethod(method); } return spec.getInitializerMethod(); }
private FixtureMethod getSharedInitializerMethod() { if (spec.getSharedInitializerMethod() == null) { // method is private s.t. multiple initializer methods along hierarchy can be called independently MethodNode gMethod = new MethodNode(InternalIdentifiers.SHARED_INITIALIZER_METHOD, Opcodes.ACC_PRIVATE | Opcodes.ACC_SYNTHETIC, ClassHelper.DYNAMIC_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, new BlockStatement()); spec.getAst().addMethod(gMethod); FixtureMethod method = new FixtureMethod(spec, gMethod); method.addBlock(new AnonymousBlock(method)); spec.setSharedInitializerMethod(method); } return spec.getSharedInitializerMethod(); }
public static MethodNode correctToGenericsSpec(Map<String, ClassNode> genericsSpec, MethodNode mn) { ClassNode correctedType = correctToGenericsSpecRecurse(genericsSpec, mn.getReturnType()); Parameter[] origParameters = mn.getParameters(); Parameter[] newParameters = new Parameter[origParameters.length]; for (int i = 0; i < origParameters.length; i++) { Parameter origParameter = origParameters[i]; newParameters[i] = new Parameter(correctToGenericsSpecRecurse(genericsSpec, origParameter.getType()), origParameter.getName(), origParameter.getInitialExpression()); } return new MethodNode(mn.getName(), mn.getModifiers(), correctedType, newParameters, mn.getExceptions(), mn.getCode()); }
private MethodNode createBuilderMethodForField(ClassNode builderClass, PropertyInfo prop, String prefix) { String propName = prop.getName().equals("class") ? "clazz" : prop.getName(); String setterName = getSetterName(prefix, prop.getName()); return new MethodNode(setterName, ACC_PUBLIC, newClass(builderClass), params(param(newClass(prop.getType()), propName)), NO_EXCEPTIONS, block( stmt(assignX(propX(varX("this"), constX(propName)), varX(propName))), returnS(varX("this", newClass(builderClass))) )); }
public void testDetectsIncorrectMemberVisibilityInInterface() throws Exception { ClassNode node = new ClassNode("zzz", ACC_ABSTRACT | ACC_INTERFACE, ClassHelper.OBJECT_TYPE); node.addMethod(new MethodNode("prim", ACC_PRIVATE, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null)); node.addMethod(new MethodNode("prom", ACC_PROTECTED, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null)); node.addField("prif", ACC_PRIVATE, ClassHelper.OBJECT_TYPE, null); node.addField("prof", ACC_PROTECTED, ClassHelper.OBJECT_TYPE, null); addDummyConstructor(node); verifier.visitClass(node); checkErrorCount(4); checkErrorMessage(EXPECTED_PROTECTED_FIELD_ERROR_MESSAGE); checkErrorMessage(EXPECTED_PRIVATE_FIELD_ERROR_MESSAGE); checkErrorMessage(EXPECTED_PROTECTED_METHOD_ERROR_MESSAGE); checkErrorMessage(EXPECTED_PRIVATE_METHOD_ERROR_MESSAGE); }
private MethodNode createBuilderMethodForProp(ClassNode builder, PropertyInfo pinfo, String prefix) { ClassNode fieldType = pinfo.getType(); String fieldName = pinfo.getName(); String setterName = getSetterName(prefix, fieldName); return new MethodNode(setterName, ACC_PUBLIC, newClass(builder), params(param(fieldType, fieldName)), NO_EXCEPTIONS, block( stmt(assignX(propX(varX("this"), constX(fieldName)), varX(fieldName, fieldType))), returnS(varX("this", builder)) )); }
private MethodNode copyMethod(MethodNode method, String newName) { // can't hurt to set return type to void MethodNode newMethod = new MethodNode(newName, method.getModifiers(), ClassHelper.VOID_TYPE, method.getParameters(), method.getExceptions(), method.getCode()); newMethod.addAnnotations(method.getAnnotations()); newMethod.setSynthetic(method.isSynthetic()); newMethod.setDeclaringClass(method.getDeclaringClass()); newMethod.setSourcePosition(method); newMethod.setVariableScope(method.getVariableScope()); newMethod.setGenericsTypes(method.getGenericsTypes()); newMethod.setAnnotationDefault(method.hasAnnotationDefault()); return newMethod; }
private static MethodNode createBuildMethodForMethod(AnnotationNode anno, ClassNode buildee, MethodNode mNode, Parameter[] params) { String buildMethodName = getMemberStringValue(anno, "buildMethodName", "build"); final BlockStatement body = new BlockStatement(); ClassNode returnType; if (mNode instanceof ConstructorNode) { returnType = newClass(buildee); body.addStatement(returnS(ctorX(newClass(mNode.getDeclaringClass()), args(params)))); } else { body.addStatement(returnS(callX(newClass(mNode.getDeclaringClass()), mNode.getName(), args(params)))); returnType = newClass(mNode.getReturnType()); } return new MethodNode(buildMethodName, ACC_PUBLIC, returnType, NO_PARAMS, NO_EXCEPTIONS, body); }