public List<MethodSource<JavaClassSource>> getConstructors(JavaClassSource javaClassSource) { List<MethodSource<JavaClassSource>> constructors = new ArrayList<MethodSource<JavaClassSource>>(); List<MethodSource<JavaClassSource>> methods = javaClassSource.getMethods(); if (methods != null) { for (MethodSource<JavaClassSource> method : methods) { if (method.isConstructor()) { constructors.add(method); } } } return constructors; }
public MethodImpl(final O parent, final String method) { init(parent); String stub = "public class Stub { " + method + " }"; JavaClassSource temp = (JavaClassSource) Roaster.parse(stub); List<MethodSource<JavaClassSource>> methods = temp.getMethods(); MethodDeclaration newMethod = (MethodDeclaration) methods.get(0).getInternal(); MethodDeclaration subtree = (MethodDeclaration) ASTNode.copySubtree(cu.getAST(), newMethod); this.method = subtree; }
public MethodImpl(final O parent, final String method) { init(parent); String stub = "public class Stub { " + method + " }"; JavaClassSource temp = (JavaClassSource) Roaster.parse(stub); List<MethodSource<JavaClassSource>> methods = temp.getMethods(); MethodDeclaration newMethod = (MethodDeclaration) methods.get(0).getInternal(); MethodDeclaration subtree = (MethodDeclaration) ASTNode.copySubtree(cu.getAST(), newMethod); this.method = subtree; }
public MethodImpl(final O parent, final String method) { init(parent); String stub = "public class Stub { " + method + " }"; JavaClassSource temp = (JavaClassSource) Roaster.parse(stub); List<MethodSource<JavaClassSource>> methods = temp.getMethods(); MethodDeclaration newMethod = (MethodDeclaration) methods.get(0).getInternal(); MethodDeclaration subtree = (MethodDeclaration) ASTNode.copySubtree(cu.getAST(), newMethod); this.method = subtree; }
public void updateMethods(JavaClassSource javaClassSource, List<org.kie.workbench.common.services.datamodeller.core.Method> methods, ClassTypeResolver classTypeResolver) throws Exception { List<MethodSource<JavaClassSource>> currentMethods = javaClassSource.getMethods(); if (currentMethods != null) { for (MethodSource<JavaClassSource> currentMethod : currentMethods) { if (isAccepted(currentMethod)) { javaClassSource.removeMethod(currentMethod); } } } if (methods != null) { for (org.kie.workbench.common.services.datamodeller.core.Method method : methods) { addMethod(javaClassSource, method, classTypeResolver); } } }
@Override @SuppressWarnings("unchecked") public MethodSource<O> setParameters(final String parameters) { String stub = "public class Stub { public void method( " + parameters + " ) {} }"; JavaClassSource temp = (JavaClassSource) Roaster.parse(stub); List<MethodSource<JavaClassSource>> methods = temp.getMethods(); List<VariableDeclaration> astParameters = ((MethodDeclaration) methods.get(0).getInternal()).parameters(); method.parameters().clear(); for (VariableDeclaration declaration : astParameters) { VariableDeclaration copy = (VariableDeclaration) ASTNode.copySubtree(method.getAST(), declaration); method.parameters().add(copy); } return this; }
@Override @SuppressWarnings("unchecked") public MethodSource<O> setParameters(final String parameters) { String stub = "public class Stub { public void method( " + parameters + " ) {} }"; JavaClassSource temp = (JavaClassSource) Roaster.parse(stub); List<MethodSource<JavaClassSource>> methods = temp.getMethods(); List<VariableDeclaration> astParameters = ((MethodDeclaration) methods.get(0).getInternal()).parameters(); method.parameters().clear(); for (VariableDeclaration declaration : astParameters) { VariableDeclaration copy = (VariableDeclaration) ASTNode.copySubtree(method.getAST(), declaration); method.parameters().add(copy); } return this; }
@Override @SuppressWarnings("unchecked") public MethodSource<O> setParameters(final String parameters) { String stub = "public class Stub { public void method( " + parameters + " ) {} }"; JavaClassSource temp = (JavaClassSource) Roaster.parse(stub); List<MethodSource<JavaClassSource>> methods = temp.getMethods(); List<VariableDeclaration> astParameters = ((MethodDeclaration) methods.get(0).getInternal()).parameters(); method.parameters().clear(); for (VariableDeclaration declaration : astParameters) { VariableDeclaration copy = (VariableDeclaration) ASTNode.copySubtree(method.getAST(), declaration); method.parameters().add(copy); } return this; }
public TypeImpl(final O origin, final Type<O> parent, final String type) { init(origin); this.parent = parent; String stub = "public class Stub { private " + type + " getType(){return null;} }"; JavaClassSource temp = (JavaClassSource) Roaster.parse(stub); List<MethodSource<JavaClassSource>> methods = temp.getMethods(); MethodDeclaration newMethod = (MethodDeclaration) methods.get(0).getInternal(); org.eclipse.jdt.core.dom.Type subtree = (org.eclipse.jdt.core.dom.Type) ASTNode.copySubtree(cu.getAST(), newMethod.getReturnType2()); this.type = subtree; }
public TypeImpl(final O origin, final Type<O> parent, final String type) { init(origin); this.parent = parent; String stub = "public class Stub { private " + type + " getType(){return null;} }"; JavaClassSource temp = (JavaClassSource) Roaster.parse(stub); List<MethodSource<JavaClassSource>> methods = temp.getMethods(); MethodDeclaration newMethod = (MethodDeclaration) methods.get(0).getInternal(); org.eclipse.jdt.core.dom.Type subtree = (org.eclipse.jdt.core.dom.Type) ASTNode.copySubtree(cu.getAST(), newMethod.getReturnType2()); this.type = subtree; }
@Override public MethodSource<O> setBody(final String body) { String stub = "public class Stub { public void method() {" + body + "} }"; JavaClassSource temp = (JavaClassSource) Roaster.parse(stub); List<MethodSource<JavaClassSource>> methods = temp.getMethods(); Block block = ((MethodDeclaration) methods.get(0).getInternal()).getBody(); block = (Block) ASTNode.copySubtree(method.getAST(), block); method.setBody(block); return this; }
public TypeImpl(final O origin, final Type<O> parent, final String type) { init(origin); this.parent = parent; String stub = "public class Stub { private " + type + " getType(){return null;} }"; JavaClassSource temp = (JavaClassSource) Roaster.parse(stub); List<MethodSource<JavaClassSource>> methods = temp.getMethods(); MethodDeclaration newMethod = (MethodDeclaration) methods.get(0).getInternal(); org.eclipse.jdt.core.dom.Type subtree = (org.eclipse.jdt.core.dom.Type) ASTNode.copySubtree(cu.getAST(), newMethod.getReturnType2()); this.type = subtree; }
@Override public MethodSource<O> setReturnType(final String typeName) { String stub = "public class Stub { public " + typeName + " method() {} }"; JavaClassSource temp = (JavaClassSource) Roaster.parse(stub); List<MethodSource<JavaClassSource>> methods = temp.getMethods(); org.eclipse.jdt.core.dom.Type returnType = ((MethodDeclaration) methods.get(0).getInternal()).getReturnType2(); returnType = (org.eclipse.jdt.core.dom.Type) ASTNode.copySubtree(method.getAST(), returnType); method.setReturnType2(returnType); return this; }
@Test public void visitMethods() { for ( MethodSource methodSource : javaClassSource.getMethods() ) { visitor.visit( methodSource ); } checkVisitor( Arrays.asList( "ref:java => int", "ref:java => java.math.BigDecimal", "ref:java => org.kie.workbench.common.screens.datamodeller.backend.server.indexing.Pojo1" ) ); }
@Override public MethodSource<O> setBody(final String body) { if (body == null) { method.setBody(null); } else { List<Problem> problems = Roaster.validateSnippet(body); if (problems.size() > 0) { throw new ParserException(problems); } String stub = "public class Stub { public void method() {" + body + "} }"; JavaClassSource temp = (JavaClassSource) Roaster.parse(stub); List<MethodSource<JavaClassSource>> methods = temp.getMethods(); Block block = ((MethodDeclaration) methods.get(0).getInternal()).getBody(); block = (Block) ASTNode.copySubtree(method.getAST(), block); method.setBody(block); } return this; }
@Override public MethodSource<O> setBody(final String body) { if (body == null) { method.setBody(null); } else { List<Problem> problems = Roaster.validateSnippet(body); if (problems.size() > 0) { throw new ParserException(problems); } String stub = "public class Stub { public void method() {" + body + "} }"; JavaClassSource temp = (JavaClassSource) Roaster.parse(stub); List<MethodSource<JavaClassSource>> methods = temp.getMethods(); Block block = ((MethodDeclaration) methods.get(0).getInternal()).getBody(); block = (Block) ASTNode.copySubtree(method.getAST(), block); method.setBody(block); } return this; }
/** * Generates a getXXX and setXXX method for the supplied field * * @param clazz * @param field */ public static void createGetterAndSetter(final JavaClassSource clazz, final FieldSource<JavaClassSource> field) { if (!clazz.hasField(field)) { throw new IllegalArgumentException("Entity did not contain the given field [" + field + "]"); } clazz.getMethods(); String fieldName = field.getName(); String methodNameSuffix = Strings.capitalize(fieldName); clazz.addMethod().setReturnType(field.getType().toString()).setName("get" + methodNameSuffix) .setPublic() .setBody("return this." + fieldName + ";"); if (!field.isFinal()) { clazz.addMethod().setReturnTypeVoid().setName("set" + methodNameSuffix).setPublic() .setParameters("final " + field.getType().toString() + " " + fieldName) .setBody("this." + fieldName + " = " + fieldName + ";"); } }
/** * Generates a getXXX and setXXX method for the supplied field * * @param clazz * @param field */ public static void createGetterAndSetter(final JavaClassSource clazz, final FieldSource<JavaClassSource> field) { if (!clazz.hasField(field)) { throw new IllegalArgumentException("Entity did not contain the given field [" + field + "]"); } clazz.getMethods(); String fieldName = field.getName(); String methodNameSuffix = Strings.capitalize(fieldName); clazz.addMethod().setReturnType(field.getType().toString()).setName("get" + methodNameSuffix) .setPublic() .setBody("return this." + fieldName + ";"); if (!field.isFinal()) { clazz.addMethod().setReturnTypeVoid().setName("set" + methodNameSuffix).setPublic() .setParameters("final " + field.getType().toString() + " " + fieldName) .setBody("this." + fieldName + " = " + fieldName + ";"); } }
@SuppressWarnings("unchecked") @Override public ParameterSource<O> addParameter(String type, String name) { String resolvedType = type; if (!hasTypeVariable(type) && getOrigin().requiresImport(type)) { Type<?> innerType = new TypeImpl<O>(getOrigin(), null, type); Import imprt = getOrigin().addImport(innerType); resolvedType = imprt != null ? Types.rebuildGenericNameWithArrays(imprt.getSimpleName(), innerType) : Types.toSimpleName(type); } String stub = "public class Stub { public void method( " + resolvedType + " " + name + " ) {} }"; JavaClassSource temp = (JavaClassSource) Roaster.parse(stub); List<MethodSource<JavaClassSource>> methods = temp.getMethods(); List<VariableDeclaration> astParameters = ((MethodDeclaration) methods.get(0).getInternal()).parameters(); ParameterSource<O> param = null; for (VariableDeclaration declaration : astParameters) { VariableDeclaration copy = (VariableDeclaration) ASTNode.copySubtree(method.getAST(), declaration); method.parameters().add(copy); param = new ParameterImpl<O>(parent, copy); } return param; }
@SuppressWarnings("unchecked") @Override public ParameterSource<O> addParameter(String type, String name) { String resolvedType = type; if (!hasTypeVariable(type) && getOrigin().requiresImport(type)) { Type<?> innerType = new TypeImpl<O>(getOrigin(), null, type); Import imprt = getOrigin().addImport(innerType); resolvedType = imprt != null ? Types.rebuildGenericNameWithArrays(imprt.getSimpleName(), innerType) : Types.toSimpleName(type); } String stub = "public class Stub { public void method( " + resolvedType + " " + name + " ) {} }"; JavaClassSource temp = (JavaClassSource) Roaster.parse(stub); List<MethodSource<JavaClassSource>> methods = temp.getMethods(); List<VariableDeclaration> astParameters = ((MethodDeclaration) methods.get(0).getInternal()).parameters(); ParameterSource<O> param = null; for (VariableDeclaration declaration : astParameters) { VariableDeclaration copy = (VariableDeclaration) ASTNode.copySubtree(method.getAST(), declaration); method.parameters().add(copy); param = new ParameterImpl<O>(parent, copy); } return param; }