Skip to content

Commit

Permalink
Reintroduce accesspecifier
Browse files Browse the repository at this point in the history
  • Loading branch information
matozoid committed Apr 15, 2019
1 parent c7b8304 commit eeab633
Show file tree
Hide file tree
Showing 35 changed files with 162 additions and 108 deletions.
Expand Up @@ -42,7 +42,7 @@ void canParseAllJavadocsInJavaParser() throws FileNotFoundException {

private void processFile(File file) throws FileNotFoundException {
try {
CompilationUnit cu = parse(file, StandardCharsets.UTF_8);
CompilationUnit cu = parse(file);
new VoidVisitorAdapter<Object>() {
@Override
public void visit(JavadocComment n, Object arg) {
Expand Down
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2016 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/

package com.github.javaparser.ast;

/**
* Access specifier. Represents one of the possible levels of
* access permitted by the language.
*
* @author Federico Tomassetti
* @since July 2014
*/
public enum AccessSpecifier {

PUBLIC("public"),
PRIVATE("private"),
PROTECTED("protected"),
PACKAGE_PRIVATE("");

private String codeRepresenation;

AccessSpecifier(String codeRepresentation) {
this.codeRepresenation = codeRepresentation;
}

public String asString() {
return this.codeRepresenation;
}
}
Expand Up @@ -91,28 +91,24 @@ public static Modifier transitiveModifier() {
*/
public enum Keyword {

DEFAULT("default", false),
PUBLIC("public", false),
PROTECTED("protected", false),
PRIVATE("private", false),
ABSTRACT("abstract", false),
STATIC("static", false),
FINAL("final", false),
TRANSIENT("transient", false),
VOLATILE("volatile", false),
SYNCHRONIZED("synchronized", false),
NATIVE("native", false),
STRICTFP("strictfp", false),
TRANSITIVE("transitive", false),
PACKAGE_PRIVATE("", true);
DEFAULT("default"),
PUBLIC("public"),
PROTECTED("protected"),
PRIVATE("private"),
ABSTRACT("abstract"),
STATIC("static"),
FINAL("final"),
TRANSIENT("transient"),
VOLATILE("volatile"),
SYNCHRONIZED("synchronized"),
NATIVE("native"),
STRICTFP("strictfp"),
TRANSITIVE("transitive");

private final String codeRepresentation;

private final boolean pseudoKeyword;

Keyword(String codeRepresentation, boolean pseudoKeyword) {
Keyword(String codeRepresentation) {
this.codeRepresentation = codeRepresentation;
this.pseudoKeyword = pseudoKeyword;
}

/**
Expand All @@ -121,14 +117,6 @@ public enum Keyword {
public String asString() {
return codeRepresentation;
}

/**
* @return true when this keyword is not an actual keyword in source code.
* The only case is "PACKAGE_PRIVATE."
*/
public boolean isPseudoKeyword() {
return pseudoKeyword;
}
}

private Keyword keyword;
Expand Down
Expand Up @@ -20,10 +20,7 @@
*/
package com.github.javaparser.ast.body;

import com.github.javaparser.ast.AllFieldsConstructor;
import com.github.javaparser.ast.Modifier;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.*;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.expr.SimpleName;
import com.github.javaparser.ast.nodeTypes.*;
Expand All @@ -40,10 +37,11 @@
import com.github.javaparser.TokenRange;
import com.github.javaparser.resolution.Resolvable;
import com.github.javaparser.resolution.declarations.ResolvedConstructorDeclaration;

import java.util.Optional;
import java.util.function.Consumer;

import static com.github.javaparser.utils.Utils.assertNotNull;
import com.github.javaparser.ast.Generated;

/**
* A constructor declaration: <code>class X { X() { } }</code> where X(){} is the constructor declaration.
Expand Down Expand Up @@ -160,10 +158,8 @@ public ConstructorDeclaration setTypeParameters(final NodeList<TypeParameter> ty
public String getDeclarationAsString(boolean includingModifiers, boolean includingThrows, boolean includingParameterName) {
StringBuilder sb = new StringBuilder();
if (includingModifiers) {
Modifier.Keyword accessSpecifier = getAccessSpecifier();
if (!accessSpecifier.isPseudoKeyword()) {
sb.append(accessSpecifier.asString()).append(" ");
}
AccessSpecifier accessSpecifier = getAccessSpecifier();
sb.append(accessSpecifier.asString()).append(" ");
}
sb.append(getName());
sb.append("(");
Expand Down
Expand Up @@ -20,9 +20,7 @@
*/
package com.github.javaparser.ast.body;

import com.github.javaparser.ast.AllFieldsConstructor;
import com.github.javaparser.ast.Modifier;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.*;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.expr.SimpleName;
import com.github.javaparser.ast.nodeTypes.*;
Expand All @@ -35,23 +33,25 @@
import com.github.javaparser.ast.type.TypeParameter;
import com.github.javaparser.ast.visitor.GenericVisitor;
import com.github.javaparser.ast.visitor.VoidVisitor;

import java.util.Optional;

import static com.github.javaparser.utils.Utils.assertNotNull;
import com.github.javaparser.ast.Node;

import com.github.javaparser.ast.visitor.CloneVisitor;
import com.github.javaparser.metamodel.MethodDeclarationMetaModel;
import com.github.javaparser.metamodel.JavaParserMetaModel;
import com.github.javaparser.TokenRange;
import com.github.javaparser.metamodel.OptionalProperty;
import com.github.javaparser.resolution.Resolvable;
import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;

import java.util.function.Consumer;
import com.github.javaparser.ast.Generated;

/**
* A method declaration. "public int abc() {return 1;}" in this example: <code>class X { public int abc() {return 1;}
* }</code>
*
* <p>
* <br/>All annotations preceding the return type will be set on this object, not on the return type.
* JavaParser doesn't know if it they are applicable to the method or the type.
*
Expand Down Expand Up @@ -189,10 +189,8 @@ public MethodDeclaration setTypeParameters(final NodeList<TypeParameter> typePar
public String getDeclarationAsString(boolean includingModifiers, boolean includingThrows, boolean includingParameterName) {
StringBuilder sb = new StringBuilder();
if (includingModifiers) {
Modifier.Keyword accessSpecifier = getAccessSpecifier();
if (!accessSpecifier.isPseudoKeyword()) {
sb.append(accessSpecifier.asString()).append(" ");
}
AccessSpecifier accessSpecifier = getAccessSpecifier();
sb.append(accessSpecifier.asString()).append(" ");
if (isStatic()) {
sb.append("static ");
}
Expand Down
Expand Up @@ -21,9 +21,11 @@

package com.github.javaparser.ast.nodeTypes;

import com.github.javaparser.ast.AccessSpecifier;
import com.github.javaparser.ast.Modifier;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.resolution.declarations.HasAccessSpecifier;

import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -97,18 +99,20 @@ default N setModifiers(final Modifier.Keyword... modifiers) {
}

/**
* @return Modifier.Keyword.PUBLIC, Modifier.Keyword.PROTECTED,
* Modifier.Keyword.PRIVATE, or Modifier.Keyword.PACKAGE_PRIVATE when none of the others exists.
* @return the access specifier as far as it can be derived from the modifiers.
* Does not take anything else into account (like "interface methods are implicitly public")
*/
default Modifier.Keyword getAccessSpecifier() {
default AccessSpecifier getAccessSpecifier() {
for (Modifier modifier : getModifiers()) {
switch (modifier.getKeyword()) {
case PUBLIC:
return AccessSpecifier.PUBLIC;
case PROTECTED:
return AccessSpecifier.PROTECTED;
case PRIVATE:
return modifier.getKeyword();
return AccessSpecifier.PRIVATE;
}
}
return Modifier.Keyword.PACKAGE_PRIVATE;
return AccessSpecifier.PACKAGE_PRIVATE;
}
}
Expand Up @@ -23,9 +23,9 @@
* Verifies that only allowed modifiers are used where modifiers are expected.
*/
public class ModifierValidator extends VisitorValidator {
private final Modifier.Keyword[] interfaceWithNothingSpecial = new Modifier.Keyword[]{PACKAGE_PRIVATE, PUBLIC, PROTECTED, ABSTRACT, FINAL, SYNCHRONIZED, NATIVE, STRICTFP};
private final Modifier.Keyword[] interfaceWithStaticAndDefault = new Modifier.Keyword[]{PACKAGE_PRIVATE, PUBLIC, PROTECTED, ABSTRACT, STATIC, FINAL, SYNCHRONIZED, NATIVE, STRICTFP, DEFAULT};
private final Modifier.Keyword[] interfaceWithStaticAndDefaultAndPrivate = new Modifier.Keyword[]{PACKAGE_PRIVATE, PUBLIC, PROTECTED, PRIVATE, ABSTRACT, STATIC, FINAL, SYNCHRONIZED, NATIVE, STRICTFP, DEFAULT};
private final Modifier.Keyword[] interfaceWithNothingSpecial = new Modifier.Keyword[]{PUBLIC, PROTECTED, ABSTRACT, FINAL, SYNCHRONIZED, NATIVE, STRICTFP};
private final Modifier.Keyword[] interfaceWithStaticAndDefault = new Modifier.Keyword[]{PUBLIC, PROTECTED, ABSTRACT, STATIC, FINAL, SYNCHRONIZED, NATIVE, STRICTFP, DEFAULT};
private final Modifier.Keyword[] interfaceWithStaticAndDefaultAndPrivate = new Modifier.Keyword[]{PUBLIC, PROTECTED, PRIVATE, ABSTRACT, STATIC, FINAL, SYNCHRONIZED, NATIVE, STRICTFP, DEFAULT};

private final boolean hasStrictfp;
private final boolean hasDefaultAndStaticInterfaceMethods;
Expand Down
Expand Up @@ -21,19 +21,19 @@

package com.github.javaparser.resolution.declarations;

import com.github.javaparser.ast.Modifier;
import com.github.javaparser.ast.AccessSpecifier;

/**
* Anything which can have an AccessSpecifier.
*
* @author Federico Tomassetti
* @see AccessSpecifier
*/
public interface HasAccessSpecifier {

/**
* The access specifier of this element.
* Only PUBLIC, PROTECTED, PRIVATE, or PACKAGE_PRIVATE are returned.
*/
Modifier.Keyword accessSpecifier();
AccessSpecifier accessSpecifier();

}
Expand Up @@ -21,6 +21,7 @@

package com.github.javaparser.resolution.declarations;

import com.github.javaparser.ast.AccessSpecifier;
import com.github.javaparser.ast.Modifier;
import com.github.javaparser.resolution.MethodUsage;
import com.github.javaparser.resolution.UnsolvedSymbolException;
Expand Down Expand Up @@ -165,7 +166,7 @@ default boolean hasVisibleField(String name) {
*/
default List<ResolvedFieldDeclaration> getVisibleFields() {
return getAllFields().stream()
.filter(f -> f.declaringType().equals(this) || f.accessSpecifier() != Modifier.Keyword.PRIVATE)
.filter(f -> f.declaringType().equals(this) || f.accessSpecifier() != AccessSpecifier.PRIVATE)
.collect(Collectors.toList());
}

Expand Down
Expand Up @@ -21,6 +21,7 @@

package com.github.javaparser.resolution.types;

import com.github.javaparser.ast.AccessSpecifier;
import com.github.javaparser.resolution.MethodUsage;
import com.github.javaparser.resolution.declarations.ResolvedFieldDeclaration;
import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;
Expand Down Expand Up @@ -394,7 +395,7 @@ public List<ResolvedMethodDeclaration> getAllMethods() {
*/
public List<ResolvedFieldDeclaration> getAllFieldsVisibleToInheritors() {
List<ResolvedFieldDeclaration> res = new LinkedList<>(this.getDeclaredFields().stream()
.filter(f -> f.accessSpecifier() != PRIVATE)
.filter(f -> f.accessSpecifier() != AccessSpecifier.PRIVATE)
.collect(Collectors.toList()));

getDirectAncestors().forEach(a ->
Expand All @@ -405,7 +406,7 @@ public List<ResolvedFieldDeclaration> getAllFieldsVisibleToInheritors() {

public List<ResolvedMethodDeclaration> getAllMethodsVisibleToInheritors() {
return this.getAllMethods().stream()
.filter(m -> m.accessSpecifier() != PRIVATE)
.filter(m -> m.accessSpecifier() != AccessSpecifier.PRIVATE)
.collect(Collectors.toList());
}

Expand Down
@@ -1,5 +1,6 @@
package com.github.javaparser.symbolsolver.javaparsermodel.contexts;

import com.github.javaparser.ast.AccessSpecifier;
import com.github.javaparser.ast.body.BodyDeclaration;
import com.github.javaparser.ast.nodeTypes.NodeWithTypeParameters;
import com.github.javaparser.ast.type.TypeParameter;
Expand All @@ -12,15 +13,13 @@
import com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserTypeParameter;
import com.github.javaparser.symbolsolver.model.resolution.SymbolReference;
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.reflectionmodel.*;
import com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration;
import com.github.javaparser.symbolsolver.resolution.ConstructorResolutionLogic;
import com.github.javaparser.symbolsolver.resolution.MethodResolutionLogic;

import java.util.List;
import java.util.stream.Collectors;

import static com.github.javaparser.ast.Modifier.Keyword.PRIVATE;

/**
* @author Federico Tomassetti
*/
Expand Down Expand Up @@ -87,7 +86,7 @@ private ResolvedTypeDeclaration checkAncestorsForType(String name, ResolvedRefer
if (internalTypeDeclaration instanceof ResolvedReferenceTypeDeclaration) {
ResolvedReferenceTypeDeclaration resolvedReferenceTypeDeclaration = internalTypeDeclaration.asReferenceType();
if (resolvedReferenceTypeDeclaration instanceof HasAccessSpecifier) {
visible = ((HasAccessSpecifier) resolvedReferenceTypeDeclaration).accessSpecifier() != PRIVATE;
visible = ((HasAccessSpecifier) resolvedReferenceTypeDeclaration).accessSpecifier() != AccessSpecifier.PRIVATE;
}
}
if (internalTypeDeclaration.getName().equals(name)) {
Expand Down
Expand Up @@ -16,6 +16,7 @@

package com.github.javaparser.symbolsolver.javaparsermodel.declarations;

import com.github.javaparser.ast.AccessSpecifier;
import com.github.javaparser.ast.Modifier;
import com.github.javaparser.ast.body.ConstructorDeclaration;
import com.github.javaparser.resolution.declarations.*;
Expand Down Expand Up @@ -60,8 +61,8 @@ public String getName() {
}

@Override
public Modifier.Keyword accessSpecifier() {
return Modifier.Keyword.PUBLIC;
public AccessSpecifier accessSpecifier() {
return AccessSpecifier.PUBLIC;
}

@Override
Expand Down

0 comments on commit eeab633

Please sign in to comment.