List of usage examples for org.objectweb.asm.commons Method getName
public String getName()
From source file:org.kjots.json.object.GenericMethod.java
License:Apache License
/** * Construct a new Generic Method.//from w w w .j ava2s.co m * * @param method The method. * @param genericReturnType The generic return type flag. * @param genericTypeIndices The indices of the generic types. */ public GenericMethod(Method method, boolean genericReturnType, Set<Integer> genericTypeIndices) { super(method.getName(), method.getDescriptor()); this.genericReturnType = genericReturnType; this.genericTypeIndices = genericTypeIndices; }
From source file:org.kjots.json.object.GenericMethod.java
License:Apache License
/** * Determine if the given method is compatible with the generic method. * * @param method The method.//from w w w . j a v a 2 s . c o m * @return <code>true</code> if the method is compatible. */ public boolean isCompatible(Method method) { if (!this.getName().equals(method.getName())) { return false; } if (this.genericReturnType) { if (!this.isCompatibleType(this.getReturnType(), method.getReturnType())) { return false; } } else if (!this.getReturnType().equals(method.getReturnType())) { return false; } Type[] argumentTypes = this.getArgumentTypes(); Type[] methodArgumentTypes = method.getArgumentTypes(); if (argumentTypes.length != methodArgumentTypes.length) { return false; } for (int i = 0; i < argumentTypes.length; i++) { Type argumentType = argumentTypes[i]; Type methodArgumentType = methodArgumentTypes[i]; if (this.genericTypeIndices.contains(i)) { if (!this.isCompatibleType(argumentType, methodArgumentType)) { return false; } } else if (!argumentType.equals(methodArgumentType)) { return false; } } return true; }
From source file:org.kjots.json.object.JsonObjectGeneratorBase.java
License:Apache License
/** * Generate the class./*from ww w. ja v a2s . c om*/ * * @param classVisitor The class visitor. * @param jsonObjectClass The class of the JSON object. * @param jsonObjectType The type of the JSON object. * @param superJsonObjectImplType The type of the super JSON object implementation. */ private void generateClass(ClassVisitor classVisitor, Class<? extends JsonObject> jsonObjectClass, Type jsonObjectType, Type superJsonObjectImplType) { Type jsonObjectImplType = Type .getObjectType(jsonObjectType.getInternalName() + "$" + this.jsonObjectImplClass.getSimpleName()); classVisitor.visit(V1_6, ACC_PUBLIC + ACC_SUPER, jsonObjectImplType, null, superJsonObjectImplType, jsonObjectType); this.generateConstructor(classVisitor, jsonObjectImplType, superJsonObjectImplType); for (java.lang.reflect.Method method : jsonObjectClass.getDeclaredMethods()) { if (method.getAnnotation(JsonFunction.class) != null) { this.generateFunctionMethod(classVisitor, jsonObjectImplType, jsonObjectClass, Method.getMethod(method), method.getAnnotation(JsonFunction.class), method.isVarArgs()); } else if (method.getAnnotation(JsonException.class) != null) { this.generateExceptionMethod(classVisitor, jsonObjectImplType, jsonObjectClass, Method.getMethod(method), method.getAnnotation(JsonException.class), method.isVarArgs()); } else if (method.getAnnotation(JsonProperty.class) != null) { this.generatePropertyMethod(classVisitor, jsonObjectImplType, method, Method.getMethod(method), method.getAnnotation(JsonProperty.class)); } else { throw new IllegalArgumentException( method.getName() + "() is not annotated with suitable annotation"); } } Set<Method> implementedMethods = new HashSet<Method>(classVisitor.getImplementedMethods()); for (GenericMethod declaredMethod : this.getExtraInterfaceMethods(jsonObjectClass)) { Method implementedMethod = null; if (declaredMethod.getGenericReturnType() || !declaredMethod.getGenericTypeIndices().isEmpty()) { implementedMethod = declaredMethod.getCompatibleMethod(implementedMethods); } else if (!declaredMethod.getGenericReturnType() && !implementedMethods.contains(declaredMethod)) { int returnTypeSort = declaredMethod.getReturnType().getSort(); if (returnTypeSort == Type.ARRAY) { returnTypeSort = declaredMethod.getReturnType().getElementType().getSort(); } if (returnTypeSort == Type.OBJECT) { GenericMethod newDeclaredMethod = new GenericMethod(declaredMethod.getName(), declaredMethod.getDescriptor(), true, declaredMethod.getGenericTypeIndices()); implementedMethod = newDeclaredMethod.getCompatibleMethod(implementedMethods); } } if (implementedMethod != null && !implementedMethod.equals(declaredMethod)) { this.generateBridgeMethod(classVisitor, jsonObjectImplType, declaredMethod, implementedMethod); } } classVisitor.visitEnd(); }
From source file:org.kjots.json.object.JsonObjectGeneratorBase.java
License:Apache License
/** * Generate a property method. /*from ww w .j a v a 2 s . com*/ * * @param classVisitor The class visitor. * @param jsonObjectImplType The type of the JSON object implementation. * @param javaMethod The java method. * @param method The method. * @param jsonPropertyAnnotation The JSON property annotation. */ private void generatePropertyMethod(ClassVisitor classVisitor, Type jsonObjectImplType, java.lang.reflect.Method javaMethod, Method method, JsonProperty jsonPropertyAnnotation) { switch (jsonPropertyAnnotation.operation()) { case HAS: this.generateHasPropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation); return; case IS_NULL: this.generateIsNullPropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation); return; case DELETE: this.generateDeletePropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation); return; case GET: this.generateGetPropertyMethod(classVisitor, jsonObjectImplType, javaMethod, method, jsonPropertyAnnotation); return; case SET: this.generateSetPropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation); return; default: throw new IllegalArgumentException(javaMethod.getName() + "() is annotated with unsupported operation type " + jsonPropertyAnnotation.operation()); } }
From source file:org.kjots.json.object.JsonObjectGeneratorBase.java
License:Apache License
/** * Generate a has property method./* w w w. ja v a 2 s .c om*/ * * @param classVisitor The class visitor. * @param jsonObjectImplType The type of the JSON object implementation. * @param method The method. * @param jsonPropertyAnnotation The JSON property annotation. */ private void generateHasPropertyMethod(ClassVisitor classVisitor, Type jsonObjectImplType, Method method, JsonProperty jsonPropertyAnnotation) { if (!method.getReturnType().equals(Type.BOOLEAN_TYPE)) { throw new IllegalArgumentException(method.getName() + "() must have a boolean return type"); } if (method.getArgumentTypes().length != 0) { throw new IllegalArgumentException(method.getName() + "() must have no parameters"); } MethodVisitor methodVisitor = classVisitor.visitMethod(ACC_PUBLIC + ACC_FINAL, method, null, null); Label start = new Label(); Label end = new Label(); methodVisitor.visitCode(); methodVisitor.visitLabel(start); methodVisitor.visitVarInsn(ALOAD, 0); methodVisitor.visitLdcInsn(jsonPropertyAnnotation.name()); methodVisitor.visitMethodInsn(INVOKEVIRTUAL, jsonObjectImplType, this.getHasJsonPropertyMethod()); methodVisitor.visitInsn(IRETURN); methodVisitor.visitLabel(end); methodVisitor.visitLocalVariable("this", jsonObjectImplType, null, start, end, 0); methodVisitor.visitMaxs(2, 1); methodVisitor.visitEnd(); }
From source file:org.kjots.json.object.JsonObjectGeneratorBase.java
License:Apache License
/** * Generate an is null property method./*from w w w. ja v a 2 s . c o m*/ * * @param classVisitor The class visitor. * @param jsonObjectImplType The type of the JSON object implementation. * @param method The method. * @param jsonPropertyAnnotation The JSON property annotation. */ private void generateIsNullPropertyMethod(ClassVisitor classVisitor, Type jsonObjectImplType, Method method, JsonProperty jsonPropertyAnnotation) { if (!method.getReturnType().equals(Type.BOOLEAN_TYPE)) { throw new IllegalArgumentException(method.getName() + "() must have a boolean return type"); } if (method.getArgumentTypes().length != 0) { throw new IllegalArgumentException(method.getName() + "() must have no parameters"); } MethodVisitor methodVisitor = classVisitor.visitMethod(ACC_PUBLIC + ACC_FINAL, method, null, null); Label start = new Label(); Label end = new Label(); methodVisitor.visitCode(); methodVisitor.visitLabel(start); methodVisitor.visitVarInsn(ALOAD, 0); methodVisitor.visitLdcInsn(jsonPropertyAnnotation.name()); methodVisitor.visitMethodInsn(INVOKEVIRTUAL, jsonObjectImplType, this.getIsNullJsonPropertyMethod()); methodVisitor.visitInsn(IRETURN); methodVisitor.visitLabel(end); methodVisitor.visitLocalVariable("this", jsonObjectImplType, null, start, end, 0); methodVisitor.visitMaxs(2, 1); methodVisitor.visitEnd(); }
From source file:org.kjots.json.object.JsonObjectGeneratorBase.java
License:Apache License
/** * Generate a delete property method./*from ww w .j av a2 s .co m*/ * * @param classVisitor The class visitor. * @param jsonObjectImplType The type of the JSON object implementation. * @param method The method. * @param jsonPropertyAnnotation The JSON property annotation. */ private void generateDeletePropertyMethod(ClassVisitor classVisitor, Type jsonObjectImplType, Method method, JsonProperty jsonPropertyAnnotation) { if (!method.getReturnType().equals(Type.BOOLEAN_TYPE)) { throw new IllegalArgumentException(method.getName() + "() must have a boolean return type"); } if (method.getArgumentTypes().length != 0) { throw new IllegalArgumentException(method.getName() + "() must have no parameters"); } MethodVisitor methodVisitor = classVisitor.visitMethod(ACC_PUBLIC + ACC_FINAL, method, null, null); Label start = new Label(); Label end = new Label(); methodVisitor.visitCode(); methodVisitor.visitLabel(start); methodVisitor.visitVarInsn(ALOAD, 0); methodVisitor.visitLdcInsn(jsonPropertyAnnotation.name()); methodVisitor.visitMethodInsn(INVOKEVIRTUAL, jsonObjectImplType, this.getDeleteJsonPropertyMethod()); methodVisitor.visitInsn(IRETURN); methodVisitor.visitLabel(end); methodVisitor.visitLocalVariable("this", jsonObjectImplType, null, start, end, 0); methodVisitor.visitMaxs(2, 1); methodVisitor.visitEnd(); }
From source file:org.kjots.json.object.JsonObjectGeneratorBase.java
License:Apache License
/** * Generate a get property method./* w w w.j a v a 2 s. c o m*/ * * @param classVisitor The class visitor. * @param jsonObjectImplType The type of the JSON object implementation. * @param javaMethod The java method. * @param method The method. * @param jsonPropertyAnnotation The JSON property annotation. */ private void generateGetPropertyMethod(ClassVisitor classVisitor, Type jsonObjectImplType, java.lang.reflect.Method javaMethod, Method method, JsonProperty jsonPropertyAnnotation) { if (method.getArgumentTypes().length != 0) { throw new IllegalArgumentException(method.getName() + "() must have no parameters"); } Type returnType = method.getReturnType(); Class<? extends JsonPropertyAdapter<?, ?>> adapterClass = jsonPropertyAnnotation.adapter(); if (adapterClass.equals(JsonProperty.NullAdapter.class) && this.isObjectType(returnType)) { JsonPropertyAdapter.AutoAdaptedType autoAdaptedTypeAnnotation = this.getClass(returnType) .getAnnotation(JsonPropertyAdapter.AutoAdaptedType.class); if (autoAdaptedTypeAnnotation != null) { adapterClass = autoAdaptedTypeAnnotation.value(); } } if (!adapterClass.equals(JsonProperty.NullAdapter.class)) { if (JsonBooleanPropertyAdapter.class.isAssignableFrom(adapterClass)) { this.generateGetAdaptedPrimitivePropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation.name(), Type.getType(Boolean.class), Type.getType(adapterClass), returnType); } else if (JsonNumberPropertyAdapter.class.isAssignableFrom(adapterClass)) { this.generateGetAdaptedPrimitivePropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation.name(), Type.getType(Number.class), Type.getType(adapterClass), returnType); } else if (JsonStringPropertyAdapter.class.isAssignableFrom(adapterClass)) { this.generateGetAdaptedPrimitivePropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation.name(), Type.getType(String.class), Type.getType(adapterClass), returnType); } else if (JsonObjectPropertyAdapter.class.isAssignableFrom(adapterClass)) { this.generateGetAdaptedObjectPropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation.name(), this.getJsonObjectType(adapterClass), Type.getType(adapterClass), returnType); } else { throw new IllegalArgumentException("Unsupported adapter type " + adapterClass.getName()); } } else { if (returnType.getClassName().equals(Boolean.class.getName())) { this.generateGetJsonPrimitivePropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation.name(), Type.getType(Boolean.class)); } else if (returnType.getClassName().equals(Number.class.getName())) { this.generateGetJsonPrimitivePropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation.name(), Type.getType(Number.class)); } else if (returnType.getClassName().equals(String.class.getName())) { this.generateGetJsonPrimitivePropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation.name(), Type.getType(String.class)); } else if (returnType.getClassName().equals(JsonObjectArray.class.getName()) || returnType.getClassName().equals(JsonObjectMap.class.getName())) { this.generateGetCompositeJsonObjectPropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation.name(), returnType, this.getCompositeJsonObjectElementType(javaMethod, method)); } else if (returnType.equals(Type.BOOLEAN_TYPE)) { this.generateGetJavaPrimitivePropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation.name(), Type.getType(Boolean.class), Type.BOOLEAN_TYPE); } else if (returnType.equals(Type.BYTE_TYPE)) { this.generateGetJavaPrimitivePropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation.name(), Type.getType(Number.class), Type.BYTE_TYPE); } else if (returnType.equals(Type.SHORT_TYPE)) { this.generateGetJavaPrimitivePropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation.name(), Type.getType(Number.class), Type.SHORT_TYPE); } else if (returnType.equals(Type.INT_TYPE)) { this.generateGetJavaPrimitivePropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation.name(), Type.getType(Number.class), Type.INT_TYPE); } else if (returnType.equals(Type.LONG_TYPE)) { this.generateGetJavaPrimitivePropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation.name(), Type.getType(Number.class), Type.LONG_TYPE); } else if (returnType.equals(Type.FLOAT_TYPE)) { this.generateGetJavaPrimitivePropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation.name(), Type.getType(Number.class), Type.FLOAT_TYPE); } else if (returnType.equals(Type.DOUBLE_TYPE)) { this.generateGetJavaPrimitivePropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation.name(), Type.getType(Number.class), Type.DOUBLE_TYPE); } else if (returnType.equals(Type.CHAR_TYPE)) { this.generateGetJavaCharacterPrimitivePropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation.name()); } else { this.generateGetJsonObjectPropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation.name(), returnType); } } }
From source file:org.kjots.json.object.JsonObjectGeneratorBase.java
License:Apache License
/** * Generate a set property method.// w ww .j av a 2 s . c o m * * @param classVisitor The class visitor. * @param jsonObjectImplType The type of the JSON object implementation. * @param method The method. * @param jsonPropertyAnnotation The JSON property annotation. */ private void generateSetPropertyMethod(ClassVisitor classVisitor, Type jsonObjectImplType, Method method, JsonProperty jsonPropertyAnnotation) { if (!method.getReturnType().equals(Type.VOID_TYPE)) { throw new IllegalArgumentException(method.getName() + "() must have a void return type"); } Type[] argumentTypes = method.getArgumentTypes(); if (argumentTypes.length != 1) { throw new IllegalArgumentException(method.getName() + "() must have exactly one parameter"); } Type argumentType = argumentTypes[0]; Class<? extends JsonPropertyAdapter<?, ?>> adapterClass = jsonPropertyAnnotation.adapter(); if (adapterClass.equals(JsonProperty.NullAdapter.class) && this.isObjectType(argumentType)) { JsonPropertyAdapter.AutoAdaptedType autoAdaptedTypeAnnotation = this.getClass(argumentType) .getAnnotation(JsonPropertyAdapter.AutoAdaptedType.class); if (autoAdaptedTypeAnnotation != null) { adapterClass = autoAdaptedTypeAnnotation.value(); } } if (!adapterClass.equals(JsonProperty.NullAdapter.class)) { if (JsonBooleanPropertyAdapter.class.isAssignableFrom(adapterClass)) { this.generateSetAdaptedPrimitivePropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation.name(), Type.getType(Boolean.class), Type.getType(adapterClass), argumentType); } else if (JsonNumberPropertyAdapter.class.isAssignableFrom(adapterClass)) { this.generateSetAdaptedPrimitivePropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation.name(), Type.getType(Number.class), Type.getType(adapterClass), argumentType); } else if (JsonStringPropertyAdapter.class.isAssignableFrom(adapterClass)) { this.generateSetAdaptedPrimitivePropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation.name(), Type.getType(String.class), Type.getType(adapterClass), argumentType); } else if (JsonObjectPropertyAdapter.class.isAssignableFrom(adapterClass)) { this.generateSetAdaptedObjectPropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation.name(), this.getJsonObjectType(adapterClass), Type.getType(adapterClass), argumentType); } else { throw new IllegalArgumentException("Unsupported adapter type " + adapterClass.getName()); } } else { if (argumentType.getClassName().equals(Boolean.class.getName())) { this.generateSetJsonPrimitivePropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation.name(), Type.getType(Boolean.class)); } else if (argumentType.getClassName().equals(Number.class.getName())) { this.generateSetJsonPrimitivePropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation.name(), Type.getType(Number.class)); } else if (argumentType.getClassName().equals(String.class.getName())) { this.generateSetJsonPrimitivePropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation.name(), Type.getType(String.class)); } else if (argumentType.equals(Type.BOOLEAN_TYPE)) { this.generateSetJavaPrimitivePropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation.name(), Type.getType(Boolean.class), Type.BOOLEAN_TYPE); } else if (argumentType.equals(Type.BYTE_TYPE)) { this.generateSetJavaPrimitivePropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation.name(), Type.getType(Number.class), Type.BYTE_TYPE); } else if (argumentType.equals(Type.SHORT_TYPE)) { this.generateSetJavaPrimitivePropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation.name(), Type.getType(Number.class), Type.SHORT_TYPE); } else if (argumentType.equals(Type.INT_TYPE)) { this.generateSetJavaPrimitivePropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation.name(), Type.getType(Number.class), Type.INT_TYPE); } else if (argumentType.equals(Type.LONG_TYPE)) { this.generateSetJavaPrimitivePropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation.name(), Type.getType(Number.class), Type.LONG_TYPE); } else if (argumentType.equals(Type.FLOAT_TYPE)) { this.generateSetJavaPrimitivePropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation.name(), Type.getType(Number.class), Type.FLOAT_TYPE); } else if (argumentType.equals(Type.DOUBLE_TYPE)) { this.generateSetJavaPrimitivePropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation.name(), Type.getType(Number.class), Type.DOUBLE_TYPE); } else if (argumentType.equals(Type.CHAR_TYPE)) { this.generateSetJavaCharacterPrimitivePropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation.name()); } else { this.generateSetJsonObjectPropertyMethod(classVisitor, jsonObjectImplType, method, jsonPropertyAnnotation.name(), argumentType); } } }
From source file:org.kjots.json.object.JsonObjectGeneratorBase.java
License:Apache License
/** * Retrieve the JSON function method with the given name for the given JSON * object method./*from www . j ava 2 s. c o m*/ * * @param jsonObjectClass The class of the JSON object. * @param jsonObjectMethod The JSON object method. * @param jsonFunctionAnnotation The JSON function annotation. * @return The JSON function method. */ private Method getJsonFunctionMethod(Class<?> jsonObjectClass, Method jsonObjectMethod, JsonFunction jsonFunctionAnnotation) { Set<Method> methods = new HashSet<Method>(); for (java.lang.reflect.Method javaMethod : jsonFunctionAnnotation.klass().getDeclaredMethods()) { if (Modifier.isStatic(javaMethod.getModifiers()) && javaMethod.getName().equals(jsonFunctionAnnotation.method())) { methods.add(Method.getMethod(javaMethod)); } } if (!methods.isEmpty()) { Type[] argumentTypes = jsonObjectMethod.getArgumentTypes(); while (jsonObjectClass != null) { Type[] jsonFunctionArgumentTypes = new Type[argumentTypes.length + 1]; jsonFunctionArgumentTypes[0] = Type.getType(jsonObjectClass); System.arraycopy(argumentTypes, 0, jsonFunctionArgumentTypes, 1, argumentTypes.length); Method method = new Method(jsonFunctionAnnotation.method(), jsonObjectMethod.getReturnType(), jsonFunctionArgumentTypes); if (methods.contains(method)) { return method; } jsonObjectClass = !jsonObjectClass.equals(JsonObject.class) ? jsonObjectClass.getInterfaces()[0] : null; } } throw new IllegalArgumentException(jsonFunctionAnnotation.klass().getName() + " does not contain a suitable method named " + jsonFunctionAnnotation.method()); }