Java Code Examples for com.sun.codemodel.JDefinedClass#getMethod()
The following examples show how to use
com.sun.codemodel.JDefinedClass#getMethod() .
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: ClassDiscoverer.java From jaxb-visitor with Apache License 2.0 | 6 votes |
/** * Borrowed this code from jaxb-commons project * * @param fieldOutline reference to a field * @return Getter for the given field or null */ static JMethod getter(FieldOutline fieldOutline) { final JDefinedClass theClass = fieldOutline.parent().implClass; final String publicName = fieldOutline.getPropertyInfo().getName(true); final JMethod getgetter = theClass.getMethod("get" + publicName, NONE); if (getgetter != null) { return getgetter; } else { final JMethod isgetter = theClass .getMethod("is" + publicName, NONE); if (isgetter != null) { return isgetter; } else { return null; } } }
Example 2
Source File: CodeModelUtils.java From jaxb2-basics with BSD 2-Clause "Simplified" License | 6 votes |
public static JMethod getMethod(JDefinedClass theClass, String name, JType[] arguments) { final JMethod method = theClass.getMethod(name, arguments); if (method != null) { return method; } else { final JClass draftSuperClass = theClass._extends(); if (draftSuperClass == null || !(draftSuperClass instanceof JDefinedClass)) { return null; } else { final JDefinedClass superClass = (JDefinedClass) draftSuperClass; return getMethod(superClass, name, arguments); } } }
Example 3
Source File: FieldAccessorUtils.java From jaxb2-basics with BSD 2-Clause "Simplified" License | 6 votes |
public static JMethod getter(FieldOutline fieldOutline) { final JDefinedClass theClass = fieldOutline.parent().implClass; final String publicName = fieldOutline.getPropertyInfo().getName(true); final JMethod getgetter = theClass.getMethod("get" + publicName, NONE); if (getgetter != null) { return getgetter; } else { final JMethod isgetter = theClass .getMethod("is" + publicName, NONE); if (isgetter != null) { return isgetter; } else { return null; } } }
Example 4
Source File: BoundPropertiesPlugin.java From jaxb2-rich-contract-plugin with MIT License | 6 votes |
private JMethod generateLazyProxyInitGetter(final ClassOutline classOutline, final FieldOutline fieldOutline) { final JCodeModel m = classOutline.parent().getCodeModel(); final JDefinedClass definedClass = classOutline.implClass; final String fieldName = fieldOutline.getPropertyInfo().getName(false); final String getterName = "get" + fieldOutline.getPropertyInfo().getName(true); final JFieldVar collectionField = definedClass.fields().get(fieldName); final JClass elementType = ((JClass) collectionField.type()).getTypeParameters().get(0); final JClass proxyFieldType = m.ref(BoundList.class).narrow(elementType); final JFieldRef collectionFieldRef = JExpr._this().ref(collectionField); final JFieldRef proxyField = JExpr._this().ref(collectionField.name() + BoundPropertiesPlugin.PROXY_SUFFIX); final JMethod oldGetter = definedClass.getMethod(getterName, new JType[0]); definedClass.methods().remove(oldGetter); final JMethod newGetter = definedClass.method(JMod.PUBLIC, proxyFieldType, getterName); newGetter.body()._if(collectionFieldRef.eq(JExpr._null()))._then().assign(collectionFieldRef, JExpr._new(m.ref(ArrayList.class).narrow(elementType))); final JBlock ifProxyNull = newGetter.body()._if(proxyField.eq(JExpr._null()))._then(); ifProxyNull.assign(proxyField, JExpr._new(m.ref(BoundListProxy.class).narrow(elementType)).arg(collectionFieldRef)); newGetter.body()._return(proxyField); return newGetter; }
Example 5
Source File: MergeablePlugin.java From jaxb2-basics with BSD 2-Clause "Simplified" License | 6 votes |
protected JMethod generateMergeFrom$createNewInstance( final ClassOutline classOutline, final JDefinedClass theClass) { final JMethod existingMethod = theClass.getMethod("createNewInstance", new JType[0]); if (existingMethod == null) { final JMethod newMethod = theClass.method(JMod.PUBLIC, theClass .owner().ref(Object.class), "createNewInstance"); newMethod.annotate(Override.class); { final JBlock body = newMethod.body(); body._return(JExpr._new(theClass)); } return newMethod; } else { return existingMethod; } }
Example 6
Source File: CopyablePlugin.java From jaxb2-basics with BSD 2-Clause "Simplified" License | 6 votes |
protected JMethod generateCopyTo$createNewInstance( final ClassOutline classOutline, final JDefinedClass theClass) { final JMethod existingMethod = theClass.getMethod("createNewInstance", new JType[0]); if (existingMethod == null) { final JMethod newMethod = theClass.method(JMod.PUBLIC, theClass .owner().ref(Object.class), "createNewInstance"); newMethod.annotate(Override.class); { final JBlock body = newMethod.body(); body._return(JExpr._new(theClass)); } return newMethod; } else { return existingMethod; } }
Example 7
Source File: PluginImpl.java From immutable-xjc with MIT License | 5 votes |
private JMethod getGetterProperty(final JFieldVar field, final JDefinedClass clazz) { JMethod getter = clazz.getMethod("get" + StringUtils.capitalize(field.name()), NO_ARGS); if (getter == null) { getter = clazz.getMethod("is" + StringUtils.capitalize(field.name()), NO_ARGS); } if (getter == null) { List<JDefinedClass> superClasses = getSuperClasses(clazz); for (JDefinedClass definedClass : superClasses) { getter = getGetterProperty(field, definedClass); if (getter != null) { break; } } } if (getter == null) { //XJC does not work conform Introspector.decapitalize when multiple upper-case letter are in field name Optional<JAnnotationUse> xmlElementAnnotation = getAnnotation(field.annotations(), javax.xml.bind.annotation.XmlElement.class.getCanonicalName()); if (xmlElementAnnotation.isPresent()) { JAnnotationValue annotationValue = xmlElementAnnotation.get().getAnnotationMembers().get("name"); if (annotationValue != null) { StringWriter sw = new StringWriter(); JFormatter f = new JFormatter(sw); annotationValue.generate(f); getter = clazz.getMethod("get" + sw.toString().replaceAll("\"", ""), NO_ARGS); } } } return getter; }
Example 8
Source File: SourceGenerator.java From mobi with GNU Affero General Public License v3.0 | 5 votes |
private boolean alreadyHasMethod(final JDefinedClass impl, final JDefinedClass interfaceClass, final JMethod interfaceMethod) { boolean alreadyHas = false; if (impl.getMethod(interfaceMethod.name(), interfaceMethod.listParamTypes()) == null) { for (JMethod method : impl.methods()) { if (interfaceMethod.name().equals(method.name()) && variablesOverlap(method, interfaceMethod)) { alreadyHas = true; break; } } } else { alreadyHas = true; } return alreadyHas; }
Example 9
Source File: PMMLPlugin.java From jpmml-model with BSD 3-Clause "New" or "Revised" License | 5 votes |
static private void createGetterProxy(JDefinedClass beanClazz, JType type, String name, String getterName){ JMethod getterMethod = beanClazz.getMethod(getterName, new JType[0]); JMethod method = beanClazz.method(JMod.PUBLIC, type, name); method.annotate(Override.class); method.body()._return(JExpr.invoke(getterMethod)); moveBefore(beanClazz, method, getterMethod); }
Example 10
Source File: SettersPlugin.java From jaxb2-basics with BSD 2-Clause "Simplified" License | 5 votes |
private void generateSetters(ClassOutline classOutline, JDefinedClass theClass) { final FieldOutline[] declaredFields = FieldOutlineUtils.filter( classOutline.getDeclaredFields(), getIgnoring()); for (final FieldOutline fieldOutline : declaredFields) { final String publicName = fieldOutline.getPropertyInfo().getName( true); final String getterName = "get" + publicName; final JMethod getter = theClass.getMethod(getterName, ABSENT); if (getter != null) { final JType type = getter.type(); final JType rawType = fieldOutline.getRawType(); final String setterName = "set" + publicName; final JMethod boxifiedSetter = theClass.getMethod(setterName, new JType[] { rawType.boxify() }); final JMethod unboxifiedSetter = theClass.getMethod(setterName, new JType[] { rawType.unboxify() }); final JMethod setter = boxifiedSetter != null ? boxifiedSetter : unboxifiedSetter; if (setter == null) { final JMethod generatedSetter = theClass.method( JMod.PUBLIC, theClass.owner().VOID, setterName); final JVar value = generatedSetter.param(type, "value"); mode.generateSetter(fieldOutline, theClass, generatedSetter, value); } } } }
Example 11
Source File: FieldAccessorUtils.java From jaxb2-basics with BSD 2-Clause "Simplified" License | 5 votes |
/** * Returns the <code>setProperty(...)</code> method for the given field * outline or <code>null</code> if no such method exists. * * @param fieldOutline * field outline. * @return The <code>setProperty(...)</code> method for the given field * outline or <code>null</code> if no such method exists. */ public static JMethod setter(FieldOutline fieldOutline) { final JMethod getter = getter(fieldOutline); final JType type = getter != null ? getter.type() : fieldOutline .getRawType(); final JDefinedClass theClass = fieldOutline.parent().implClass; final String publicName = fieldOutline.getPropertyInfo().getName(true); final String name = "set" + publicName; return theClass.getMethod(name, new JType[] { type }); }
Example 12
Source File: SourceGenerator.java From mobi with GNU Affero General Public License v3.0 | 5 votes |
private void generateFieldGetterForImpl(final JDefinedClass impl, final JMethod interfaceMethod, final JDefinedClass interfaceClass) { if (impl.getMethod(interfaceMethod.name(), interfaceMethod.listParamTypes()) == null) { final JMethod method = impl.method(JMod.PUBLIC, interfaceMethod.type(), interfaceMethod.name()); method._throws(OrmException.class); method.annotate(Override.class); // TODO - add javadoc. // JDocComment jdoc = method.javadoc(); // jdoc.add(""); convertValueBody(interfaceClass, interfaceMethod, impl, method); } else { LOG.warn("Avoided duplicate getter method: " + interfaceMethod.name() + " on class: " + impl.name()); } }
Example 13
Source File: SourceGenerator.java From mobi with GNU Affero General Public License v3.0 | 5 votes |
private void generateFieldSetterForImpl(final JDefinedClass impl, final JMethod interfaceMethod, final JDefinedClass interfaceClass) { if (impl.getMethod(interfaceMethod.name(), interfaceMethod.listParamTypes()) == null) { final JMethod method = impl.method(JMod.PUBLIC, interfaceMethod.type(), interfaceMethod.name()); method.param(interfaceMethod.params().get(0).type(), "arg"); method._throws(OrmException.class); method.annotate(Override.class); if (interfaceMethod.params().get(0).type().fullName().startsWith("java.util.Set")) { method.body().invoke("setProperties") .arg(JExpr.ref("valueConverterRegistry").invoke("convertTypes") .arg(interfaceMethod.params().get(0)).arg(JExpr._this())) .arg(JExpr.ref("valueFactory").invoke("createIRI") .arg(interfaceClass.staticRef(classMethodIriMap.get(interfaceClass).get(interfaceMethod)))); } else { method.body().invoke("setProperty") .arg(JExpr.ref("valueConverterRegistry").invoke("convertType") .arg(interfaceMethod.params().get(0)).arg(JExpr._this())) .arg(JExpr.ref("valueFactory").invoke("createIRI") .arg(interfaceClass.staticRef(classMethodIriMap.get(interfaceClass).get(interfaceMethod)))); } // TODO - add javadoc. // JDocComment jdoc = method.javadoc(); // jdoc.add(""); } else { LOG.warn("Avoided duplicate setter method: " + interfaceMethod.name() + " on class: " + impl.name()); } }
Example 14
Source File: SourceGenerator.java From mobi with GNU Affero General Public License v3.0 | 5 votes |
private void generateFieldAdderRemoverForImpl(final JDefinedClass impl, final JMethod interfaceMethod, final JDefinedClass interfaceClass, final boolean add) { if (impl.getMethod(interfaceMethod.name(), interfaceMethod.listParamTypes()) == null) { final JMethod method = impl.method(JMod.PUBLIC, interfaceMethod.type(), interfaceMethod.name()); method.param(interfaceMethod.params().get(0).type(), "arg"); method._throws(OrmException.class); method.annotate(Override.class); method.body()._return(JExpr.ref("this").invoke(add ? "addProperty" : "removeProperty").arg(JExpr.ref("valueConverterRegistry").invoke("convertType") .arg(interfaceMethod.params().get(0)).arg(JExpr._this())) .arg(JExpr.ref("valueFactory").invoke("createIRI") .arg(interfaceClass.staticRef(classMethodIriMap.get(interfaceClass).get(interfaceMethod))))); } else { LOG.warn("Avoided duplicate adder method: " + interfaceMethod.name() + " on class: " + impl.name()); } }
Example 15
Source File: BoundPropertiesPlugin.java From jaxb2-rich-contract-plugin with MIT License | 4 votes |
@Override public boolean run(final Outline outline, final Options opt, final ErrorHandler errorHandler) throws SAXException { if (!this.constrained && !this.bound) { return true; } final PluginContext pluginContext = PluginContext.get(outline, opt, errorHandler); final JCodeModel m = outline.getCodeModel(); if (this.generateTools) { // generate bound collection helper classes pluginContext.writeSourceFile(BoundList.class); pluginContext.writeSourceFile(BoundListProxy.class); pluginContext.writeSourceFile(CollectionChangeEventType.class); pluginContext.writeSourceFile(CollectionChangeEvent.class); pluginContext.writeSourceFile(CollectionChangeListener.class); pluginContext.writeSourceFile(VetoableCollectionChangeListener.class); } if(pluginContext.hasPlugin(ImmutablePlugin.class)) { errorHandler.error(new SAXParseException(getMessage("error.immutableAndConstrainedProperties"), outline.getModel().getLocator())); } final int setterAccess = JMod.PUBLIC; for (final ClassOutline classOutline : outline.getClasses()) { final JDefinedClass definedClass = classOutline.implClass; // Create bound collection proxies for (final FieldOutline fieldOutline : classOutline.getDeclaredFields()) { if (fieldOutline.getPropertyInfo().isCollection() && !definedClass.fields().get(fieldOutline.getPropertyInfo().getName(false)).type().isArray()) { generateProxyField(classOutline, fieldOutline); generateLazyProxyInitGetter(classOutline, fieldOutline); } } if (this.constrained && this.setterThrows) { for (final JMethod method : definedClass.methods()) { if (method.name().startsWith("with") && !"withVetoableChangeListener".equals(method.name()) && !"withPropertyChangeListener".equals(method.name()) ) { method._throws(PropertyVetoException.class); } } } if (this.constrained) createSupportProperty(outline, classOutline, VetoableChangeSupport.class, VetoableChangeListener.class, "vetoableChange"); if (this.bound) createSupportProperty(outline, classOutline, PropertyChangeSupport.class, PropertyChangeListener.class, "propertyChange"); for (final JFieldVar field : definedClass.fields().values()) { //final JFieldVar field = definedClass.fields().get(fieldOutline.getPropertyInfo().getName(false)); final JMethod oldSetter = definedClass.getMethod("set" + outline.getModel().getNameConverter().toPropertyName(field.name()), new JType[]{field.type()}); if (oldSetter != null && !field.type().isArray()) { definedClass.methods().remove(oldSetter); final JMethod setter = definedClass.method(setterAccess, m.VOID, "set" + outline.getModel().getNameConverter().toPropertyName(field.name())); final JVar setterArg = setter.param(JMod.FINAL, field.type(), "value"); final JBlock body = setter.body(); final JVar oldValueVar = body.decl(JMod.FINAL, field.type(), BoundPropertiesPlugin.OLD_VALUE_VAR_NAME, JExpr._this().ref(field)); if (this.constrained) { final JTryBlock tryBlock; final JBlock block; if (this.setterThrows) { block = body; setter._throws(PropertyVetoException.class); } else { tryBlock = body._try(); block = tryBlock.body(); final JCatchBlock catchBlock = tryBlock._catch(m.ref(PropertyVetoException.class)); final JVar exceptionVar = catchBlock.param("x"); catchBlock.body()._throw(JExpr._new(m.ref(RuntimeException.class)).arg(exceptionVar)); } invokeListener(block, field, oldValueVar, setterArg, "vetoableChange"); } body.assign(JExpr._this().ref(field), setterArg); if (this.bound) { invokeListener(body, field, oldValueVar, setterArg, "propertyChange"); } } } } return true; }
Example 16
Source File: FieldAccessorUtils.java From jaxb2-basics with BSD 2-Clause "Simplified" License | 3 votes |
/** * Returns the <code>isSetProperty()</code> method for the given field * outline or <code>null</code> if no such method exists. * * @param fieldOutline * field outline. * @return The <code>isSetProperty()</code> method for the given field * outline or <code>null</code> if no such method exists. */ public static JMethod issetter(FieldOutline fieldOutline) { final JDefinedClass theClass = fieldOutline.parent().implClass; final String publicName = fieldOutline.getPropertyInfo().getName(true); final String name = "isSet" + publicName; return theClass.getMethod(name, NONE); }
Example 17
Source File: PMMLPlugin.java From jpmml-model with BSD 3-Clause "New" or "Revised" License | 3 votes |
static public void createSetterProxy(JDefinedClass beanClazz, JType type, String parameterName, String name, String setterName){ JMethod getterMethod = beanClazz.getMethod(setterName.replace("set", "get"), new JType[0]); JMethod method = beanClazz.method(JMod.PUBLIC, beanClazz, name); method.annotate(Override.class); JVar nameParameter = method.param(type, parameterName); method.body()._return(JExpr.invoke(setterName).arg(nameParameter)); moveBefore(beanClazz, method, getterMethod); }