Java Code Examples for com.sun.codemodel.JDefinedClass#methods()

The following examples show how to use com.sun.codemodel.JDefinedClass#methods() . 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: SourceGenerator.java    From mobi with GNU Affero General Public License v3.0 5 votes vote down vote up
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 2
Source File: RamlInterpreterTest.java    From springmvc-raml-plugin with Apache License 2.0 5 votes vote down vote up
private JMethod getMethod(JDefinedClass classToCheck, String methodToFind) {
	for (JMethod method : classToCheck.methods()) {
		if (methodToFind.equals(method.name())) {
			return method;
		}
	}
	return null;
}
 
Example 3
Source File: CodeModelUtils.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static JMethod getMethod(final JDefinedClass theClass,
		final String name) {
	for (JMethod method : theClass.methods()) {
		if (method.name().equals(name))
			return method;
	}
	return null;
}
 
Example 4
Source File: PMMLPlugin.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
static
private void moveBefore(JDefinedClass beanClazz, JMethod method, JMethod referenceMethod){
	List<JMethod> methods = (List<JMethod>)beanClazz.methods();

	int index = methods.indexOf(referenceMethod);
	if(index < 0){
		throw new RuntimeException();
	}

	methods.remove(method);
	methods.add(index, method);
}
 
Example 5
Source File: PMMLPlugin.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
static
private void moveAfter(JDefinedClass beanClazz, JMethod method, JMethod referenceMethod){
	List<JMethod> methods = (List<JMethod>)beanClazz.methods();

	int index = methods.indexOf(referenceMethod);
	if(index < 0){
		throw new RuntimeException();
	}

	methods.remove(method);
	methods.add(index + 1, method);
}
 
Example 6
Source File: NoJavadocPlugin.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
static
private void nullifyJavadoc(JDefinedClass clazz){
	nullifyJavadoc((JDocCommentable)clazz);

	Collection<JMethod> methods = clazz.methods();
	for(JMethod method : methods){
		nullifyJavadoc(method);
	}
}
 
Example 7
Source File: PluginImpl.java    From immutable-xjc with MIT License 5 votes vote down vote up
private void removeSetters(JDefinedClass clazz) {
    Collection<JMethod> methods = clazz.methods();
    Iterator<JMethod> it = methods.iterator();
    while (it.hasNext()) {
        JMethod method = it.next();
        String methodName = method.name();
        if (methodName.startsWith(SET_PREFIX) || methodName.startsWith(UNSET_PREFIX)) {
            it.remove();
        }
    }
}
 
Example 8
Source File: BoundPropertiesPlugin.java    From jaxb2-rich-contract-plugin with MIT License 4 votes vote down vote up
@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 9
Source File: CreateJAXBElementNameCallback.java    From jaxb-visitor with Apache License 2.0 4 votes vote down vote up
private Set<JDefinedClass> identifyCandidates(Outline outline) {

        // phase one: identify all of the candidates and update the ObjectFactories with the setter call
        // phase two: only include instances that don't have a JDefinedClass as their super
        // phase three: add all of the markings

        JClass qNameClass = outline.getCodeModel().ref(QName.class);
        Set<JDefinedClass> candidates = new LinkedHashSet<>();
        for(PackageOutline po : outline.getAllPackageContexts()) {
            // locate the object factory
            JDefinedClass of = outline.getPackageContext(po._package()).objectFactory();
            for(JMethod method : of.methods()) {
                JType retType = method.type();

                if (retType.binaryName().startsWith(JAXBElement.class.getName())) {
                    JClass clazz = (JClass) retType;
                    List<JClass> typeParameters = clazz.getTypeParameters();
                    if (typeParameters.size()==1) {
                        if (typeParameters.get(0) instanceof JDefinedClass && !typeParameters.get(0).isAbstract()) {
                            String namespace = null;
                            String localPart = null;
                            for(JAnnotationUse  au : method.annotations()) {
                                if (au.getAnnotationClass().fullName().equals(XmlElementDecl.class.getName())) {
                                    namespace = annotationValueToString(au.getAnnotationMembers().get("namespace"));
                                    localPart = annotationValueToString(au.getAnnotationMembers().get("name"));
                                    break;
                                }

                            }
                            if (namespace != null) {
                                method.body().pos(0);
                                method.body().invoke(method.params().get(0), SETTER)
                                        .arg(JExpr._new(qNameClass).arg(namespace).arg(localPart));
                            }
                            JDefinedClass dc = (JDefinedClass) typeParameters.get(0);
                            candidates.add(dc);
                        }
                    }
                }
            }
        }
        return candidates;
    }
 
Example 10
Source File: JAXBDataBinding.java    From cxf with Apache License 2.0 4 votes vote down vote up
public void fillInFields(Writer writer, String indent,
                              String path, String varName,
                              JDefinedClass tp) throws IOException {
    JClass sp = tp._extends();
    if (sp instanceof JDefinedClass) {
        fillInFields(writer, indent, path, varName, (JDefinedClass)sp);
    }

    Collection<JMethod> methods = tp.methods();
    for (JMethod m : methods) {
        if (m.name().startsWith("set")) {
            writer.write("\n");
            writer.write(indent);
            if (DEFAULT_TYPE_MAP.contains(m.listParamTypes()[0].fullName())) {
                writer.write(varName);
                writer.write(".");
                writer.write(m.name());
                writer.write("(");
                writeDefaultType(writer, m.listParamTypes()[0], path + "/" + m.name().substring(3));
                writer.write(");");
            } else {
                int idx = path.indexOf("/" + m.name().substring(3) + "/");
                if (idx > 0) {
                    idx = path.indexOf("/" + m.name().substring(3) + "/", idx + 1);
                }
                boolean hasTwo = idx > 0;
                if (!hasTwo) {
                    writeDefaultValue(writer, indent,
                                      path + "/" + m.name().substring(3),
                                      varName + m.name().substring(3),
                                      m.listParamTypes()[0]);
                    writer.write("\n");
                }
                writer.write(indent);
                writer.write(varName);
                writer.write(".");
                writer.write(m.name());
                writer.write("(");
                if (!hasTwo) {
                    writer.write(varName + m.name().substring(3));
                } else {
                    writer.write("null");
                }
                writer.write(");");
            }
        } else if (m.type().fullName().startsWith("java.util.List")) {
            writer.write("\n");
            writer.write(indent);
            writeDefaultValue(writer, indent,
                              path + "/" + m.name().substring(3),
                              varName + m.name().substring(3),
                              m.type());
            writer.write("\n");
            writer.write(indent);
            writer.write(varName);
            writer.write(".");
            writer.write(m.name());
            writer.write("().addAll(");
            writer.write(varName + m.name().substring(3));
            writer.write(");");
        }
    }
}