/** * Gets a body from a template executable with all the template parameters * substituted. * * @param targetClass * the target class * @param template * the template that holds the executable * @param executableName * the source executable template * @param parameterTypes * the parameter types of the source executable * @return the body expression of the source executable template with all * the template parameters substituted */ public static CtBlock<?> substituteMethodBody(CtClass<?> targetClass, Template<?> template, String executableName, CtTypeReference<?>... parameterTypes) { CtClass<?> sourceClass = getTemplateCtClass(targetClass, template); CtExecutable<?> sourceExecutable = executableName.equals(template.getClass().getSimpleName()) ? sourceClass.getConstructor(parameterTypes) : sourceClass.getMethod(executableName, parameterTypes); return substitute(targetClass, template, sourceExecutable.getBody()); }
/** * Generates a constructor from a template constructor by substituting all * the template parameters by their values. * * @param targetClass * the target class where to insert the generated constructor * @param template * the template instance that holds the source template * constructor and that defines the parameter values * @param sourceConstructor * the source template constructor * @return the generated constructor */ @SuppressWarnings("unchecked") public static <T> CtConstructor<T> insertConstructor(CtClass<T> targetClass, Template<?> template, CtConstructor<?> sourceConstructor) { CtConstructor<T> newConstrutor = substitute(targetClass, template, (CtConstructor<T>) sourceConstructor); // remove the implicit constructor if clashing if (newConstrutor.getParameters().isEmpty()) { CtConstructor<?> c = targetClass.getConstructor(); if (c != null && c.isImplicit()) { targetClass.getConstructors().remove(c); } } targetClass.addConstructor(newConstrutor); // newConstrutor.setParent(targetClass); return newConstrutor; }
private CtExecutable<T> getCtExecutable(CtType<?> typeDecl) { if (typeDecl == null) { return null; } CtTypeReference<?>[] arrayParameters = parameters.toArray(new CtTypeReferenceImpl<?>[0]); CtExecutable<T> method = typeDecl.getMethod(getSimpleName(), arrayParameters); if ((method == null) && (typeDecl instanceof CtClass) && this.isConstructor()) { try { CtClass<?> zeClass = (CtClass) typeDecl; CtConstructor<?> constructor = zeClass.getConstructor(arrayParameters); return (CtExecutable<T>) constructor; } catch (ClassCastException e) { Launcher.LOGGER.error(e.getMessage(), e); } } else if (method == null && getSimpleName().startsWith(CtExecutableReference.LAMBDA_NAME_PREFIX)) { final List<CtLambda> elements = typeDecl.getElements(new NamedElementFilter<>(CtLambda.class, getSimpleName())); if (elements.isEmpty()) { return null; } return elements.get(0); } return method; }
/** * Gets a statement from a template executable with all the template * parameters substituted. * * @param targetClass * the target class * @param template * the template that holds the executable * @param statementIndex * the statement index in the executable's body * @param executableName * the source executable template * @param parameterTypes * the parameter types of the source executable * @return the body expression of the source executable template with all * the template parameters substituted */ public static CtStatement substituteStatement(CtClass<?> targetClass, Template<?> template, int statementIndex, String executableName, CtTypeReference<?>... parameterTypes) { CtClass<?> sourceClass = getTemplateCtClass(targetClass, template); CtExecutable<?> sourceExecutable = executableName.equals(template.getClass().getSimpleName()) ? sourceClass.getConstructor(parameterTypes) : sourceClass.getMethod(executableName, parameterTypes); return substitute(targetClass, template, sourceExecutable.getBody().getStatement(statementIndex)); }
/** * Generates a constructor from a template constructor by substituting all * the template parameters by their values. * * @param targetClass * the target class where to insert the generated constructor * @param template * the template instance that holds the source template * constructor and that defines the parameter values * @param sourceConstructor * the source template constructor * @return the generated constructor */ @SuppressWarnings("unchecked") public static <T> CtConstructor<T> insertConstructor(CtClass<T> targetClass, Template<?> template, CtConstructor<?> sourceConstructor) { CtConstructor<T> newConstrutor = substitute(targetClass, template, (CtConstructor<T>) sourceConstructor); // remove the implicit constructor if clashing if (newConstrutor.getParameters().isEmpty()) { CtConstructor<?> c = targetClass.getConstructor(); if (c != null && c.isImplicit()) { targetClass.removeConstructor((CtConstructor<T>) c); } } targetClass.addConstructor(newConstrutor); return newConstrutor; }
private static boolean isValidConstructor(CtType<?> type) { if(type instanceof CtClass<?>) { CtClass<?> ctClass = ((CtClass<?>)type); if (ctClass.getSuperclass() == null || !ctClass.getSuperclass().getSimpleName().equals("TestCase")){ return true; } return ((CtClass<?>)type).getConstructor() != null || ((CtClass<?>)type).getConstructor(type.getFactory().Class().createReference(String.class)) != null; } return false; }
@Override @SuppressWarnings("unchecked") public CtExecutable<T> getDeclaration() { CtType<?> typeDecl = getDeclaringType().getDeclaration(); if (typeDecl == null) { return null; } CtExecutable<T> method = typeDecl .getMethod(getSimpleName(), parameters.toArray( new CtTypeReferenceImpl<?>[parameters.size()])); if ((method == null) && (typeDecl instanceof CtClass) && (getSimpleName().equals("<init>"))) { try { return (CtExecutable<T>) ((CtClass<?>) typeDecl) .getConstructor(parameters.toArray( new CtTypeReferenceImpl<?>[parameters.size()])); } catch (ClassCastException e) { Launcher.LOGGER.error(e.getMessage(), e); } } return method; }
/** * Gets a body from a template executable with all the template parameters * substituted. * * @param targetClass * the target class * @param template * the template that holds the executable * @param executableName * the source executable template * @param parameterTypes * the parameter types of the source executable * @return the body expression of the source executable template with all * the template parameters substituted */ public static CtBlock<?> substituteMethodBody(CtClass<?> targetClass, Template<?> template, String executableName, CtTypeReference<?>... parameterTypes) { CtClass<?> sourceClass = targetClass.getFactory().Class().get(template.getClass()); CtExecutable<?> sourceExecutable = executableName.equals(template.getClass().getSimpleName()) ? sourceClass.getConstructor(parameterTypes) : sourceClass.getMethod(executableName, parameterTypes); return substitute(targetClass, template, sourceExecutable.getBody()); }
/** * Gets a statement from a template executable with all the template * parameters substituted. * * @param targetClass * the target class * @param template * the template that holds the executable * @param statementIndex * the statement index in the executable's body * @param executableName * the source executable template * @param parameterTypes * the parameter types of the source executable * @return the body expression of the source executable template with all * the template parameters substituted */ public static CtStatement substituteStatement(CtClass<?> targetClass, Template<?> template, int statementIndex, String executableName, CtTypeReference<?>... parameterTypes) { CtClass<?> sourceClass = targetClass.getFactory().Class().get(template.getClass()); CtExecutable<?> sourceExecutable = executableName.equals(template.getClass().getSimpleName()) ? sourceClass.getConstructor(parameterTypes) : sourceClass.getMethod(executableName, parameterTypes); return substitute(targetClass, template, sourceExecutable.getBody().getStatement(statementIndex)); }
if (parent.getConstructor() == null) { if (parent.getConstructor(ctInvocation.getFactory().Class().STRING) != null) { invocation += "\"" + parent.getSimpleName() + "\""; } else {