Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change min sdk version #531

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion java-completion/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ dependencies {
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'


implementation 'androidx.annotation:annotation:1.3.0'

// used for code generation
implementation 'com.github.javaparser:javaparser-core:3.23.1'

// fuzzy search
implementation 'me.xdrop:fuzzywuzzy:1.3.1'
implementation project(path: ':common')
implementation project(path: ':completion-api')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import androidx.annotation.NonNull;

import com.github.javaparser.ast.Modifier.Keyword;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.Parameter;
import com.tyron.completion.java.util.ActionUtil;
import com.tyron.completion.model.Position;
import com.tyron.completion.model.Range;
Expand Down Expand Up @@ -54,25 +58,26 @@ public TextEdit removeTree(CompilationUnitTree root, Tree remove) {
}

/**
* Prints a given method into a String, adds {@code throws UnsupportedOperationException} to the method body
* if the source is null, it will get the parameter names from the class file which will be {@code arg1, arg2, arg3}
* @param method method to print
* Prints a given method into a String, adds {@code throws UnsupportedOperationException} to
* the method body
* if the source is null, it will get the parameter names from the class file which will be
* {@code arg1, arg2, arg3}
*
* @param method method to print
* @param parameterizedType type parameters of this method
* @param source the source method, in which the parameter names are fetched
* @param source the source method, in which the parameter names are fetched
* @return a string that represents the method
*/
public static String printMethod(ExecutableElement method, ExecutableType parameterizedType, MethodTree source) {
public static String printMethod(ExecutableElement method, ExecutableType parameterizedType,
MethodTree source) {
StringBuilder buf = new StringBuilder();
buf.append("@Override\n");
if (method.getModifiers().contains(Modifier.PUBLIC)) {
buf.append("public ");
}
if (method.getModifiers().contains(Modifier.PROTECTED)) {
buf.append("protected ");
}

buf.append(EditHelper.printType(parameterizedType.getReturnType())).append(" ");

MethodDeclaration methodDeclaration = new MethodDeclaration();
methodDeclaration.addAndGetAnnotation(Override.class);
methodDeclaration.setModifiers(method.getModifiers().stream()
.map(it -> Keyword.valueOf(it.toString()))
.toArray(Keyword[]::new));
methodDeclaration.setType(EditHelper.printType(parameterizedType.getReturnType()));
buf.append(method.getSimpleName()).append("(");
if (source == null) {
buf.append(printParameters(parameterizedType, method));
Expand All @@ -85,7 +90,8 @@ public static String printMethod(ExecutableElement method, ExecutableType parame
return buf.toString();
}

public static String printMethod(ExecutableElement method, ExecutableType parameterizedType, ExecutableElement source) {
public static String printMethod(ExecutableElement method, ExecutableType parameterizedType,
ExecutableElement source) {
StringBuilder buf = new StringBuilder();
buf.append("@Override\n");
if (method.getModifiers().contains(Modifier.PUBLIC)) {
Expand Down Expand Up @@ -117,8 +123,7 @@ public static String printMethod(ExecutableElement method, ExecutableType parame
public static String printThrows(@NonNull List<? extends TypeMirror> thrownTypes) {
StringBuilder types = new StringBuilder();
for (TypeMirror m : thrownTypes) {
types.append((types.length() == 0) ? "" : ", ")
.append(printType(m));
types.append((types.length() == 0) ? "" : ", ").append(printType(m));
}
return "throws " + types;
}
Expand All @@ -133,11 +138,9 @@ public static String printBody(ExecutableElement method, MethodTree source) {
} else {
String names;
if (source != null) {
names = source.getParameters().stream().map(VariableTree::getName)
.map(Name::toString).collect(Collectors.joining(", "));
names = source.getParameters().stream().map(VariableTree::getName).map(Name::toString).collect(Collectors.joining(", "));
} else {
names = method.getParameters().stream().map(VariableElement::getSimpleName)
.map(Name::toString).collect(Collectors.joining(", "));
names = method.getParameters().stream().map(VariableElement::getSimpleName).map(Name::toString).collect(Collectors.joining(", "));
}
body = "super." + method.getSimpleName() + "(" + names + ");";
}
Expand All @@ -146,13 +149,16 @@ public static String printBody(ExecutableElement method, MethodTree source) {
}

switch (returnType.getKind()) {
case VOID: return "";
case VOID:
return "";
case SHORT:
case CHAR:
case FLOAT:
case BYTE:
case INT: return "return 0;";
case BOOLEAN: return "return false;";
case INT:
return "return 0;";
case BOOLEAN:
return "return false;";
default:
return "return null;";
}
Expand All @@ -168,11 +174,9 @@ public static String printBody(ExecutableElement method, ExecutableElement sourc
} else {
String names;
if (source != null) {
names = source.getParameters().stream().map(VariableElement::getSimpleName)
.map(Name::toString).collect(Collectors.joining(", "));
names = source.getParameters().stream().map(VariableElement::getSimpleName).map(Name::toString).collect(Collectors.joining(", "));
} else {
names = method.getParameters().stream().map(VariableElement::getSimpleName)
.map(Name::toString).collect(Collectors.joining(", "));
names = method.getParameters().stream().map(VariableElement::getSimpleName).map(Name::toString).collect(Collectors.joining(", "));
}
body = "super." + method.getSimpleName() + "(" + names + ");";
}
Expand All @@ -181,20 +185,24 @@ public static String printBody(ExecutableElement method, ExecutableElement sourc
}

switch (returnType.getKind()) {
case VOID: return "";
case VOID:
return "";
case SHORT:
case CHAR:
case FLOAT:
case BYTE:
case INT: return "return 0;";
case BOOLEAN: return "return false;";
case INT:
return "return 0;";
case BOOLEAN:
return "return false;";
default:
return "return null;";
}
}

/**
* Prints parameters given the source method that contains parameter names
*
* @param method element from the .class file
* @param source element from the .java file
* @return Formatted string that represents the methods parameters with proper names
Expand All @@ -213,6 +221,7 @@ public static String printParameters(ExecutableType method, MethodTree source) {
/**
* Prints parameters with the default names eg. {@code arg0, arg1}
* this is used when the source file of the class isn't found
*
* @param method element to print
* @param source the class file of the method
* @return Formatted String that represents the parameters of this method
Expand Down Expand Up @@ -313,7 +322,8 @@ public static Position insertAfter(JavacTask task, CompilationUnitTree root, Tre
return new Position(line, 0);
}

public static Position insertAtEndOfClass(JavacTask task, CompilationUnitTree root, ClassTree leaf) {
public static Position insertAtEndOfClass(JavacTask task, CompilationUnitTree root,
ClassTree leaf) {
SourcePositions pos = Trees.instance(task).getSourcePositions();
LineMap lines = root.getLineMap();
long end = pos.getEndPosition(root, leaf);
Expand Down