private JavaMethod createMethod (MethodDeclaration method) { String className = classStack.peek().getName(); String name = method.getName(); boolean isStatic = ModifierSet.hasModifier(method.getModifiers(), ModifierSet.STATIC); String returnType = method.getType().toString(); ArrayList<Argument> arguments = new ArrayList<Argument>(); if (method.getParameters() != null) { for (Parameter parameter : method.getParameters()) { arguments.add(new Argument(getArgumentType(parameter), parameter.getId().getName())); } } return new JavaMethod(className, name, isStatic, returnType, null, arguments, method.getBeginLine(), method.getEndLine()); }
.filter(internalType -> internalType.getName().getId().equals(name)) .findFirst() .map(internalType -> SymbolReference.solved( JavaParserFacade.get(typeSolver).getTypeDeclaration(internalType))); if(exactMatch.isPresent()){ return exactMatch.get(); .orElse(new NodeList<>()) .stream() .filter(type -> type.toString().equals(name)) .findFirst() .map(matchingType -> SymbolReference.solved( new JavaParserTypeParameter(new TypeParameter(matchingType.toString()), typeSolver)));
@Override public void visit(MethodDeclaration method, ClassBuilder builder) { super.visit(method, builder); TypeFactory factory = new TypeFactory(packageName, imports); if (method.getParentNode().isPresent() && method.getParentNode().get() instanceof ClassOrInterfaceDeclaration && isClass((ClassOrInterfaceDeclaration) method.getParentNode().get()) && isGetter(method.getNameAsString(), method.getType().asString()) && !skipFieldOrGetter(method.getModifiers())) { builder.withGetter(new Method( method.getNameAsString(), factory.build(method.getType().asString(), method.getType() instanceof PrimitiveType) )); } }
val name = StreamSupport.stream(RelaxedPropertyNames.forCamelCase(variable.getNameAsString()).spliterator(), false) .map(Object::toString) .findFirst() .orElseGet(variable::getNameAsString); if (fieldDecl.getJavadoc().isPresent()) { val description = fieldDecl.getJavadoc().get().getDescription().toText(); prop.setDescription(description); prop.setShortDescription(StringUtils.substringBefore(description, ".")); prop.setId(indexedName); val elementType = fieldDecl.getElementType().asString(); if (elementType.equals(String.class.getSimpleName()) || elementType.equals(Integer.class.getSimpleName()) val initializer = variable.getInitializer(); if (initializer.isPresent()) { val exp = initializer.get();
/** * Process the $event variable passed on v-on. This variable must have a valid cast in front. * * @param expression The currently processed expression * @param nameExpr The variable we are processing * @param parameters The parameters this expression depends on */ private void processEventParameter(Expression expression, NameExpr nameExpr, List<VariableInfo> parameters) { Optional<Node> parentNode = nameExpr.getParentNode(); if (parentNode.isPresent() && parentNode.get() instanceof CastExpr) { CastExpr castExpr = (CastExpr) parentNode.get(); parameters.add(new VariableInfo(castExpr.getType().toString(), "$event")); } else { logger.error( "\"$event\" should always be casted to it's intended type. Example: @click=\"doSomething((Event) $event)\".", expression.toString()); } }
/** * Get the type of an expression from the cast at the beginning. (int) 12 -> 12 of type int (int) * 15 + 5 -> 15 + 5 of type int (float) (12 + 3) -> 12 + 3 of type float ((int) 12) + 3 -> ((int) * 12) + 3 of type Any ((JsArray) myArray).getAt(0) -> ((JsArray) myArray).getAt(0) of type Any * * @param expression The expression to process * @return The modified expression (where cast has been removed if necessary) */ private Expression getTypeFromCast(Expression expression) { if (expression instanceof BinaryExpr) { Expression mostLeft = getLeftmostExpression(expression); if (mostLeft instanceof CastExpr) { CastExpr castExpr = (CastExpr) mostLeft; currentExpressionReturnType = stringTypeToTypeName(castExpr.getType().toString()); BinaryExpr parent = (BinaryExpr) mostLeft.getParentNode().get(); parent.setLeft(castExpr.getExpression()); } } else if (expression instanceof CastExpr) { CastExpr castExpr = (CastExpr) expression; currentExpressionReturnType = stringTypeToTypeName(castExpr.getType().toString()); expression = castExpr.getExpression(); } return expression; }
@Override public Type visit(final ClassOrInterfaceType n, final Context ctx) { String name = name(n); Type type = ctx.resolveType(n, name) .orElseThrow(() -> new IllegalArgumentException("Type not found: " + name)); List<Type> args = n.getTypeArgs().stream() .map(it -> it.accept(new TypeCollector(), ctx)) .filter(it -> it != null) .collect(Collectors.toList()); if (args.size() > 0) { type = Types.newParameterizedType(type, args.toArray(new Type[args.size()])); } return type; }
private void getParameterNames(MethodDeclaration methodDeclaration, boolean isInterface) { final EnumSet<Modifier> modifiers = methodDeclaration.getModifiers(); if (isInterface || modifiers.contains(Modifier.PUBLIC)) { String methodName = methodDeclaration.getName().getIdentifier(); List<Parameter> parameters = methodDeclaration.getParameters(); names.className = this.className; List<List<ParameterName>> parameterNames = names.names.computeIfAbsent(methodName, k -> new ArrayList<>(4)); final List<ParameterName> temp = new ArrayList<>(); for (final Parameter parameter : parameters) { ParameterName parameterName = new ParameterName(); String type = parameter.getType().toString(); String name = parameter.getName().getIdentifier(); if (name.contains("[]")) { type = type + "[]"; name = name.replace("[]", ""); } parameterName.type = type; parameterName.name = name; temp.add(parameterName); } parameterNames.add(temp); } } }
if (methoDec.getModifiers() == ModifierSet.PUBLIC) { signature[4] = methoDec.getName(); List pars = methoDec.getParameters(); if (pars != null) { StringBuilder parsStr = new StringBuilder(); String sParCleaned = sPar.getType().toString(); if (sPar.getType().toString().contains("<")) { sParCleaned = sParCleaned.substring(0, sParCleaned.indexOf("<"));
/** * <b>Note:</b> This will not return the actual identifier but only the simple names of the types (return type & parameter types). * Doing a full type resolving with all imports adds too much complexity at this point. * This is a best-effort approach. */ private MethodIdentifier calculateMethodIdentifier(MethodDeclaration method) { String[] parameters = method.getParameters().stream() .map(p -> p.getType().asString()) .map(p -> p.replace('.', '/')) .toArray(String[]::new); String returnType = method.getType().asString().replace('.', '/'); if (method.isStatic()) { return ofStatic(className, method.getNameAsString(), returnType, parameters); } return ofNonStatic(className, method.getNameAsString(), returnType, parameters); }
List<Parameter> parameters = declaration.getParameters(); parameters = parameters == null ? new ArrayList<Parameter>(0) : parameters; Method[] methods = type.getDeclaredMethods(); for (Method m : methods) { if (!m.getName().equals(declaration.getNameAsString())) { continue; Type ptype = parameters.get(j).getType(); if (ptype == null) { continue; String paranTypeName = ptype.toString(); int index = paranTypeName.lastIndexOf("."); if (index > 0) {
private ArgumentType getArgumentType (Parameter parameter) { String[] typeTokens = parameter.getType().toString().split("\\."); String type = typeTokens[typeTokens.length - 1]; int arrayDim = 0; for (int i = 0; i < type.length(); i++) { if (type.charAt(i) == '[') arrayDim++; } type = type.replace("[", "").replace("]", ""); if (arrayDim >= 1) { if (arrayDim > 1) return ArgumentType.ObjectArray; ArgumentType arrayType = arrayTypes.get(type); if (arrayType == null) { return ArgumentType.ObjectArray; } return arrayType; } if (plainOldDataTypes.containsKey(type)) return plainOldDataTypes.get(type); if (bufferTypes.containsKey(type)) return bufferTypes.get(type); if (otherTypes.containsKey(type)) return otherTypes.get(type); return ArgumentType.Object; }
&& fdOne.getType().equals(fdTwo.getType())) { result.add(mergeField(fdOne, fdTwo)); found = true; MethodDeclaration mdTwo = (MethodDeclaration) inner; if (mdOne.getName().equals(mdTwo.getName()) && isParametersEquals(mdOne.getParameters(), mdTwo.getParameters()) && isListEquals(mdOne.getTypeParameters(), mdTwo.getTypeParameters())) { result.add(mergeMethod(mdOne, mdTwo));
/** * Visit the parameter of a Route method. * @param param Parameter of the route method. * @param route The route model that we construct. */ @Override public void visit(Parameter param, ControllerRouteModel route) { List<AnnotationExpr> annos = param.getAnnotations(); if(annos == null || annos.isEmpty()){ LOGGER.warn("[controller]The parameter " + param + "at line " + param.getBeginLine() + " " + "is for a route method but has not been annotated!"); return; } RouteParamModel routeParam = new RouteParamModel(); routeParam.setParamName(String.valueOf(param.getId())); routeParam.setName(routeParam.getParamName()); //by default, will be override if name is specified //Parsed the param (for the annotation) paramVisitor.visit(param,routeParam); if(routeParam.getParamType() == null){ //ignore if the param has not been visited (i.e no annotations) return; } //TODO some cleaning here! routeParam.setValueType(param.getType().toString()); route.addParam(routeParam); }
public void index(com.github.javaparser.ast.body.Parameter parameter, int methodId) { io.github.benas.jql.model.Parameter p = new io.github.benas.jql.model.Parameter(parameter.getNameAsString(), parameter.getType().asString(), methodId); parameterDao.save(p); } }
/** {@inheritDoc} */ @Override public void visit(FieldDeclaration field, ClassBuilder builder) { super.visit(field, builder); TypeFactory factory = new TypeFactory(packageName, imports); if (!skipFieldOrGetter(field.getModifiers())) { field.getVariables().forEach(variable -> builder.withField(new Field( isNullable(field), variable.getNameAsString(), factory.build(variable.getType().asString(), variable.getType() instanceof PrimitiveType) ))); } }
private boolean hasTypeFields(NodeList<BodyDeclaration<?>> members) { boolean hasType = false; boolean hasTypeId = false; for (BodyDeclaration<?> bd : members) { if (bd instanceof FieldDeclaration) { FieldDeclaration f = (FieldDeclaration)bd; EnumSet<Modifier> m = f.getModifiers(); if (m.contains(Modifier.PUBLIC) && m.contains(Modifier.STATIC) && m.contains(Modifier.FINAL) // && // getTypeName(f.getType()).equals("int") ) { List<VariableDeclarator> vds = f.getVariables(); for (VariableDeclarator vd : vds) { if (vd.getType().equals(intType)) { String n = vd.getNameAsString(); if (n.equals("type")) hasType = true; if (n.equals("typeIndexID")) hasTypeId = true; if (hasTypeId && hasType) { return true; } } } } } } // end of for return false; }
private Class<?> getReturnType(final MethodDeclaration md) { Class<?> clazz = void.class; final String returnType = md.getType().asString(); if (!returnType.equals("void")) { final List<String> classNames = new ArrayList<>(); if (returnType.contains(".")) { classNames.add(returnType.trim()); } else { this.getNativeJavaPackages().forEach(p -> classNames.add(p + returnType.trim())); } final List<Class<?>> classes = this.loadClasses(classNames).stream().collect(Collectors.toList()); clazz = classes.isEmpty() ? void.class : classes.get(0); } return clazz; }
@Override public void visit(FieldDeclaration fd, List<EntityField> f) { if (fd.getAnnotations().stream().anyMatch(anno -> anno.getName().getName().equals("Column"))) { Class<?> type = null; switch (fd.getType().toString()) { case "String": type = String.class; break; case "Long": type = Long.class; break; case "Integer": type = Integer.class; break; case "boolean": type = boolean.class; break; } if (type == null) return; f.add(new EntityField( fd.getVariables().get(0).getId().getName(), type, fd.getAnnotations().stream().anyMatch(anno -> anno.getName().getName().equals("Id")))); } } }, entityFields);
private Class<?> getType(final FieldDeclaration fd) { final String type = fd.getVariables().get(0).getType().asString(); final List<String> classNames = new ArrayList<>(); if (type.contains(".")) { classNames.add(type.trim()); } else { this.getNativeJavaPackages().forEach(p -> classNames.add(p + type.trim())); } final List<Class<?>> classes = this.loadClasses(classNames) .stream() .collect(Collectors.toList()); return classes.isEmpty() ? Object.class : classes.get(0); }